1 | /* |
2 | ** IR CALL* instruction definitions. |
3 | ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h |
4 | */ |
5 | |
6 | #ifndef _LJ_IRCALL_H |
7 | #define _LJ_IRCALL_H |
8 | |
9 | #include "lj_obj.h" |
10 | #include "lj_ir.h" |
11 | #include "lj_jit.h" |
12 | |
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; |
18 | |
19 | #define CCI_NARGS(ci) ((ci)->flags & 0xff) /* # of args. */ |
20 | #define CCI_NARGS_MAX 32 /* Max. # of args. */ |
21 | |
22 | #define CCI_OTSHIFT 16 |
23 | #define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */ |
24 | #define CCI_TYPE(ci) (((ci)->flags>>CCI_OTSHIFT) & IRT_TYPE) |
25 | #define CCI_OPSHIFT 24 |
26 | #define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */ |
27 | |
28 | #define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT) |
29 | #define CCI_CALL_A (IR_CALLA << CCI_OPSHIFT) |
30 | #define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT) |
31 | #define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT) |
32 | #define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL) |
33 | #define CCI_CALL_FA (CCI_CALL_A|CCI_CC_FASTCALL) |
34 | #define CCI_CALL_FL (CCI_CALL_L|CCI_CC_FASTCALL) |
35 | #define CCI_CALL_FS (CCI_CALL_S|CCI_CC_FASTCALL) |
36 | |
37 | /* C call info flags. */ |
38 | #define CCI_T (IRT_GUARD << CCI_OTSHIFT) /* May throw. */ |
39 | #define CCI_L 0x0100 /* Implicit L arg. */ |
40 | #define CCI_CASTU64 0x0200 /* Cast u64 result to number. */ |
41 | #define CCI_NOFPRCLOBBER 0x0400 /* Does not clobber any FPRs. */ |
42 | #define CCI_VARARG 0x0800 /* Vararg function. */ |
43 | |
44 | #define CCI_CC_MASK 0x3000 /* Calling convention mask. */ |
45 | #define CCI_CC_SHIFT 12 |
46 | /* ORDER CC */ |
47 | #define CCI_CC_CDECL 0x0000 /* Default cdecl calling convention. */ |
48 | #define CCI_CC_THISCALL 0x1000 /* Thiscall calling convention. */ |
49 | #define CCI_CC_FASTCALL 0x2000 /* Fastcall calling convention. */ |
50 | #define CCI_CC_STDCALL 0x3000 /* Stdcall calling convention. */ |
51 | |
52 | /* Extra args for SOFTFP, SPLIT 64 bit. */ |
53 | #define CCI_XARGS_SHIFT 14 |
54 | #define CCI_XARGS(ci) (((ci)->flags >> CCI_XARGS_SHIFT) & 3) |
55 | #define CCI_XA (1u << CCI_XARGS_SHIFT) |
56 | |
57 | #if LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI) |
58 | #define CCI_XNARGS(ci) (CCI_NARGS((ci)) + CCI_XARGS((ci))) |
59 | #else |
60 | #define CCI_XNARGS(ci) CCI_NARGS((ci)) |
61 | #endif |
62 | |
63 | /* Helpers for conditional function definitions. */ |
64 | #define IRCALLCOND_ANY(x) x |
65 | |
66 | #if LJ_TARGET_X86ORX64 |
67 | #define IRCALLCOND_FPMATH(x) NULL |
68 | #else |
69 | #define IRCALLCOND_FPMATH(x) x |
70 | #endif |
71 | |
72 | #if LJ_SOFTFP |
73 | #define IRCALLCOND_SOFTFP(x) x |
74 | #if LJ_HASFFI |
75 | #define IRCALLCOND_SOFTFP_FFI(x) x |
76 | #else |
77 | #define IRCALLCOND_SOFTFP_FFI(x) NULL |
78 | #endif |
79 | #else |
80 | #define IRCALLCOND_SOFTFP(x) NULL |
81 | #define IRCALLCOND_SOFTFP_FFI(x) NULL |
82 | #endif |
83 | |
84 | #if LJ_SOFTFP && LJ_TARGET_MIPS |
85 | #define IRCALLCOND_SOFTFP_MIPS(x) x |
86 | #else |
87 | #define IRCALLCOND_SOFTFP_MIPS(x) NULL |
88 | #endif |
89 | |
90 | #if LJ_SOFTFP && LJ_TARGET_MIPS64 |
91 | #define IRCALLCOND_SOFTFP_MIPS64(x) x |
92 | #else |
93 | #define IRCALLCOND_SOFTFP_MIPS64(x) NULL |
94 | #endif |
95 | |
96 | #define LJ_NEED_FP64 (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS) |
97 | |
98 | #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) |
99 | #define IRCALLCOND_FP64_FFI(x) x |
100 | #else |
101 | #define IRCALLCOND_FP64_FFI(x) NULL |
102 | #endif |
103 | |
104 | #if LJ_HASFFI |
105 | #define IRCALLCOND_FFI(x) x |
106 | #if LJ_32 |
107 | #define IRCALLCOND_FFI32(x) x |
108 | #else |
109 | #define IRCALLCOND_FFI32(x) NULL |
110 | #endif |
111 | #else |
112 | #define IRCALLCOND_FFI(x) NULL |
113 | #define IRCALLCOND_FFI32(x) NULL |
114 | #endif |
115 | |
116 | #if LJ_SOFTFP |
117 | #define XA_FP CCI_XA |
118 | #define XA2_FP (CCI_XA+CCI_XA) |
119 | #else |
120 | #define XA_FP 0 |
121 | #define XA2_FP 0 |
122 | #endif |
123 | |
124 | #if LJ_SOFTFP32 |
125 | #define XA_FP32 CCI_XA |
126 | #define XA2_FP32 (CCI_XA+CCI_XA) |
127 | #else |
128 | #define XA_FP32 0 |
129 | #define XA2_FP32 0 |
130 | #endif |
131 | |
132 | #if LJ_32 |
133 | #define XA_64 CCI_XA |
134 | #define XA2_64 (CCI_XA+CCI_XA) |
135 | #else |
136 | #define XA_64 0 |
137 | #define XA2_64 0 |
138 | #endif |
139 | |
140 | /* Function definitions for CALL* instructions. */ |
141 | #define IRCALLDEF(_) \ |
142 | _(ANY, lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \ |
143 | _(ANY, lj_str_find, 4, N, PGC, 0) \ |
144 | _(ANY, lj_str_new, 3, S, STR, CCI_L|CCI_T) \ |
145 | _(ANY, lj_strscan_num, 2, FN, INT, 0) \ |
146 | _(ANY, lj_strfmt_int, 2, FN, STR, CCI_L|CCI_T) \ |
147 | _(ANY, lj_strfmt_num, 2, FN, STR, CCI_L|CCI_T) \ |
148 | _(ANY, lj_strfmt_char, 2, FN, STR, CCI_L|CCI_T) \ |
149 | _(ANY, lj_strfmt_putint, 2, FL, PGC, CCI_T) \ |
150 | _(ANY, lj_strfmt_putnum, 2, FL, PGC, CCI_T) \ |
151 | _(ANY, lj_strfmt_putquoted, 2, FL, PGC, CCI_T) \ |
152 | _(ANY, lj_strfmt_putfxint, 3, L, PGC, XA_64|CCI_T) \ |
153 | _(ANY, lj_strfmt_putfnum_int, 3, L, PGC, XA_FP|CCI_T) \ |
154 | _(ANY, lj_strfmt_putfnum_uint, 3, L, PGC, XA_FP|CCI_T) \ |
155 | _(ANY, lj_strfmt_putfnum, 3, L, PGC, XA_FP|CCI_T) \ |
156 | _(ANY, lj_strfmt_putfstr, 3, L, PGC, CCI_T) \ |
157 | _(ANY, lj_strfmt_putfchar, 3, L, PGC, CCI_T) \ |
158 | _(ANY, lj_buf_putmem, 3, S, PGC, CCI_T) \ |
159 | _(ANY, lj_buf_putstr, 2, FL, PGC, CCI_T) \ |
160 | _(ANY, lj_buf_putchar, 2, FL, PGC, CCI_T) \ |
161 | _(ANY, lj_buf_putstr_reverse, 2, FL, PGC, CCI_T) \ |
162 | _(ANY, lj_buf_putstr_lower, 2, FL, PGC, CCI_T) \ |
163 | _(ANY, lj_buf_putstr_upper, 2, FL, PGC, CCI_T) \ |
164 | _(ANY, lj_buf_putstr_rep, 3, L, PGC, CCI_T) \ |
165 | _(ANY, lj_buf_puttab, 5, L, PGC, CCI_T) \ |
166 | _(ANY, lj_buf_tostr, 1, FL, STR, CCI_T) \ |
167 | _(ANY, lj_tab_new_ah, 3, A, TAB, CCI_L|CCI_T) \ |
168 | _(ANY, lj_tab_new1, 2, FA, TAB, CCI_L|CCI_T) \ |
169 | _(ANY, lj_tab_dup, 2, FA, TAB, CCI_L|CCI_T) \ |
170 | _(ANY, lj_tab_clear, 1, FS, NIL, 0) \ |
171 | _(ANY, lj_tab_newkey, 3, S, PGC, CCI_L|CCI_T) \ |
172 | _(ANY, lj_tab_len, 1, FL, INT, 0) \ |
173 | _(ANY, lj_tab_len_hint, 2, FL, INT, 0) \ |
174 | _(ANY, lj_gc_step_jit, 2, FS, NIL, CCI_L) \ |
175 | _(ANY, lj_gc_barrieruv, 2, FS, NIL, 0) \ |
176 | _(ANY, lj_mem_newgco, 2, FA, PGC, CCI_L|CCI_T) \ |
177 | _(ANY, lj_prng_u64d, 1, FS, NUM, CCI_CASTU64) \ |
178 | _(ANY, lj_vm_modi, 2, FN, INT, 0) \ |
179 | _(ANY, log10, 1, N, NUM, XA_FP) \ |
180 | _(ANY, exp, 1, N, NUM, XA_FP) \ |
181 | _(ANY, sin, 1, N, NUM, XA_FP) \ |
182 | _(ANY, cos, 1, N, NUM, XA_FP) \ |
183 | _(ANY, tan, 1, N, NUM, XA_FP) \ |
184 | _(ANY, asin, 1, N, NUM, XA_FP) \ |
185 | _(ANY, acos, 1, N, NUM, XA_FP) \ |
186 | _(ANY, atan, 1, N, NUM, XA_FP) \ |
187 | _(ANY, sinh, 1, N, NUM, XA_FP) \ |
188 | _(ANY, cosh, 1, N, NUM, XA_FP) \ |
189 | _(ANY, tanh, 1, N, NUM, XA_FP) \ |
190 | _(ANY, fputc, 2, S, INT, 0) \ |
191 | _(ANY, fwrite, 4, S, INT, 0) \ |
192 | _(ANY, fflush, 1, S, INT, 0) \ |
193 | /* ORDER FPM */ \ |
194 | _(FPMATH, lj_vm_floor, 1, N, NUM, XA_FP) \ |
195 | _(FPMATH, lj_vm_ceil, 1, N, NUM, XA_FP) \ |
196 | _(FPMATH, lj_vm_trunc, 1, N, NUM, XA_FP) \ |
197 | _(FPMATH, sqrt, 1, N, NUM, XA_FP) \ |
198 | _(ANY, log, 1, N, NUM, XA_FP) \ |
199 | _(ANY, lj_vm_log2, 1, N, NUM, XA_FP) \ |
200 | _(ANY, lj_vm_powi, 2, N, NUM, XA_FP) \ |
201 | _(ANY, pow, 2, N, NUM, XA2_FP) \ |
202 | _(ANY, atan2, 2, N, NUM, XA2_FP) \ |
203 | _(ANY, ldexp, 2, N, NUM, XA_FP) \ |
204 | _(SOFTFP, lj_vm_tobit, 1, N, INT, XA_FP32) \ |
205 | _(SOFTFP, softfp_add, 2, N, NUM, XA2_FP32) \ |
206 | _(SOFTFP, softfp_sub, 2, N, NUM, XA2_FP32) \ |
207 | _(SOFTFP, softfp_mul, 2, N, NUM, XA2_FP32) \ |
208 | _(SOFTFP, softfp_div, 2, N, NUM, XA2_FP32) \ |
209 | _(SOFTFP, softfp_cmp, 2, N, NIL, XA2_FP32) \ |
210 | _(SOFTFP, softfp_i2d, 1, N, NUM, 0) \ |
211 | _(SOFTFP, softfp_d2i, 1, N, INT, XA_FP32) \ |
212 | _(SOFTFP_MIPS, lj_vm_sfmin, 2, N, NUM, XA2_FP32) \ |
213 | _(SOFTFP_MIPS, lj_vm_sfmax, 2, N, NUM, XA2_FP32) \ |
214 | _(SOFTFP_MIPS64, lj_vm_tointg, 1, N, INT, 0) \ |
215 | _(SOFTFP_FFI, softfp_ui2d, 1, N, NUM, 0) \ |
216 | _(SOFTFP_FFI, softfp_f2d, 1, N, NUM, 0) \ |
217 | _(SOFTFP_FFI, softfp_d2ui, 1, N, INT, XA_FP32) \ |
218 | _(SOFTFP_FFI, softfp_d2f, 1, N, FLOAT, XA_FP32) \ |
219 | _(SOFTFP_FFI, softfp_i2f, 1, N, FLOAT, 0) \ |
220 | _(SOFTFP_FFI, softfp_ui2f, 1, N, FLOAT, 0) \ |
221 | _(SOFTFP_FFI, softfp_f2i, 1, N, INT, 0) \ |
222 | _(SOFTFP_FFI, softfp_f2ui, 1, N, INT, 0) \ |
223 | _(FP64_FFI, fp64_l2d, 1, N, NUM, XA_64) \ |
224 | _(FP64_FFI, fp64_ul2d, 1, N, NUM, XA_64) \ |
225 | _(FP64_FFI, fp64_l2f, 1, N, FLOAT, XA_64) \ |
226 | _(FP64_FFI, fp64_ul2f, 1, N, FLOAT, XA_64) \ |
227 | _(FP64_FFI, fp64_d2l, 1, N, I64, XA_FP) \ |
228 | _(FP64_FFI, fp64_d2ul, 1, N, U64, XA_FP) \ |
229 | _(FP64_FFI, fp64_f2l, 1, N, I64, 0) \ |
230 | _(FP64_FFI, fp64_f2ul, 1, N, U64, 0) \ |
231 | _(FFI, lj_carith_divi64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ |
232 | _(FFI, lj_carith_divu64, 2, N, U64, XA2_64|CCI_NOFPRCLOBBER) \ |
233 | _(FFI, lj_carith_modi64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ |
234 | _(FFI, lj_carith_modu64, 2, N, U64, XA2_64|CCI_NOFPRCLOBBER) \ |
235 | _(FFI, lj_carith_powi64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ |
236 | _(FFI, lj_carith_powu64, 2, N, U64, XA2_64|CCI_NOFPRCLOBBER) \ |
237 | _(FFI, lj_cdata_newv, 4, S, CDATA, CCI_L) \ |
238 | _(FFI, lj_cdata_setfin, 4, S, NIL, CCI_L) \ |
239 | _(FFI, strlen, 1, L, INTP, 0) \ |
240 | _(FFI, memcpy, 3, S, PTR, 0) \ |
241 | _(FFI, memset, 3, S, PTR, 0) \ |
242 | _(FFI, lj_vm_errno, 0, S, INT, CCI_NOFPRCLOBBER) \ |
243 | _(FFI32, lj_carith_mul64, 2, N, I64, XA2_64|CCI_NOFPRCLOBBER) \ |
244 | _(FFI32, lj_carith_shl64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ |
245 | _(FFI32, lj_carith_shr64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ |
246 | _(FFI32, lj_carith_sar64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ |
247 | _(FFI32, lj_carith_rol64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ |
248 | _(FFI32, lj_carith_ror64, 2, N, U64, XA_64|CCI_NOFPRCLOBBER) \ |
249 | \ |
250 | /* End of list. */ |
251 | |
252 | typedef enum { |
253 | #define IRCALLENUM(cond, name, nargs, kind, type, flags) IRCALL_##name, |
254 | IRCALLDEF(IRCALLENUM) |
255 | #undef IRCALLENUM |
256 | IRCALL__MAX |
257 | } IRCallID; |
258 | |
259 | LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...); |
260 | |
261 | LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1]; |
262 | |
263 | /* Soft-float declarations. */ |
264 | #if LJ_SOFTFP |
265 | #if LJ_TARGET_ARM |
266 | #define softfp_add __aeabi_dadd |
267 | #define softfp_sub __aeabi_dsub |
268 | #define softfp_mul __aeabi_dmul |
269 | #define softfp_div __aeabi_ddiv |
270 | #define softfp_cmp __aeabi_cdcmple |
271 | #define softfp_i2d __aeabi_i2d |
272 | #define softfp_d2i __aeabi_d2iz |
273 | #define softfp_ui2d __aeabi_ui2d |
274 | #define softfp_f2d __aeabi_f2d |
275 | #define softfp_d2ui __aeabi_d2uiz |
276 | #define softfp_d2f __aeabi_d2f |
277 | #define softfp_i2f __aeabi_i2f |
278 | #define softfp_ui2f __aeabi_ui2f |
279 | #define softfp_f2i __aeabi_f2iz |
280 | #define softfp_f2ui __aeabi_f2uiz |
281 | #define fp64_l2d __aeabi_l2d |
282 | #define fp64_ul2d __aeabi_ul2d |
283 | #define fp64_l2f __aeabi_l2f |
284 | #define fp64_ul2f __aeabi_ul2f |
285 | #if LJ_TARGET_IOS |
286 | #define fp64_d2l __fixdfdi |
287 | #define fp64_d2ul __fixunsdfdi |
288 | #define fp64_f2l __fixsfdi |
289 | #define fp64_f2ul __fixunssfdi |
290 | #else |
291 | #define fp64_d2l __aeabi_d2lz |
292 | #define fp64_d2ul __aeabi_d2ulz |
293 | #define fp64_f2l __aeabi_f2lz |
294 | #define fp64_f2ul __aeabi_f2ulz |
295 | #endif |
296 | #elif LJ_TARGET_MIPS || LJ_TARGET_PPC |
297 | #define softfp_add __adddf3 |
298 | #define softfp_sub __subdf3 |
299 | #define softfp_mul __muldf3 |
300 | #define softfp_div __divdf3 |
301 | #define softfp_cmp __ledf2 |
302 | #define softfp_i2d __floatsidf |
303 | #define softfp_d2i __fixdfsi |
304 | #define softfp_ui2d __floatunsidf |
305 | #define softfp_f2d __extendsfdf2 |
306 | #define softfp_d2ui __fixunsdfsi |
307 | #define softfp_d2f __truncdfsf2 |
308 | #define softfp_i2f __floatsisf |
309 | #define softfp_ui2f __floatunsisf |
310 | #define softfp_f2i __fixsfsi |
311 | #define softfp_f2ui __fixunssfsi |
312 | #else |
313 | #error "Missing soft-float definitions for target architecture" |
314 | #endif |
315 | extern double softfp_add(double a, double b); |
316 | extern double softfp_sub(double a, double b); |
317 | extern double softfp_mul(double a, double b); |
318 | extern double softfp_div(double a, double b); |
319 | extern void softfp_cmp(double a, double b); |
320 | extern double softfp_i2d(int32_t a); |
321 | extern int32_t softfp_d2i(double a); |
322 | #if LJ_HASFFI |
323 | extern double softfp_ui2d(uint32_t a); |
324 | extern double softfp_f2d(float a); |
325 | extern uint32_t softfp_d2ui(double a); |
326 | extern float softfp_d2f(double a); |
327 | extern float softfp_i2f(int32_t a); |
328 | extern float softfp_ui2f(uint32_t a); |
329 | extern int32_t softfp_f2i(float a); |
330 | extern uint32_t softfp_f2ui(float a); |
331 | #endif |
332 | #if LJ_TARGET_MIPS |
333 | extern double lj_vm_sfmin(double a, double b); |
334 | extern double lj_vm_sfmax(double a, double b); |
335 | #endif |
336 | #endif |
337 | |
338 | #if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP) |
339 | #if defined(__GNUC__) || defined(__clang__) |
340 | #define fp64_l2d __floatdidf |
341 | #define fp64_ul2d __floatundidf |
342 | #define fp64_l2f __floatdisf |
343 | #define fp64_ul2f __floatundisf |
344 | #define fp64_d2l __fixdfdi |
345 | #define fp64_d2ul __fixunsdfdi |
346 | #define fp64_f2l __fixsfdi |
347 | #define fp64_f2ul __fixunssfdi |
348 | #else |
349 | #error "Missing fp64 helper definitions for this compiler" |
350 | #endif |
351 | #endif |
352 | |
353 | #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) |
354 | extern double fp64_l2d(int64_t a); |
355 | extern double fp64_ul2d(uint64_t a); |
356 | extern float fp64_l2f(int64_t a); |
357 | extern float fp64_ul2f(uint64_t a); |
358 | extern int64_t fp64_d2l(double a); |
359 | extern uint64_t fp64_d2ul(double a); |
360 | extern int64_t fp64_f2l(float a); |
361 | extern uint64_t fp64_f2ul(float a); |
362 | #endif |
363 | |
364 | #endif |
365 | |