PPC: Fuse BSWAP with XLOAD/XSTORE to lwbrx/stwbrx.
[luajit-2.0.git] / src / lj_ircall.h
blobb1e0e446d8751f252e383f4c2b7bbce3b5223c45
1 /*
2 ** IR CALL* instruction definitions.
3 ** Copyright (C) 2005-2011 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_FASTCALL)
31 #define CCI_CALL_FL (CCI_CALL_L|CCI_FASTCALL)
32 #define CCI_CALL_FS (CCI_CALL_S|CCI_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_FASTCALL 0x0800 /* Fastcall convention. */
40 /* Helpers for conditional function definitions. */
41 #define IRCALLCOND_ANY(x) x
43 #if LJ_TARGET_X86ORX64
44 #define IRCALLCOND_FPMATH(x) NULL
45 #else
46 #define IRCALLCOND_FPMATH(x) x
47 #endif
49 #if LJ_SOFTFP
50 #define IRCALLCOND_SOFTFP(x) x
51 #if LJ_HASFFI
52 #define IRCALLCOND_SOFTFP_FFI(x) x
53 #else
54 #define IRCALLCOND_SOFTFP_FFI(x) NULL
55 #endif
56 #else
57 #define IRCALLCOND_SOFTFP(x) NULL
58 #define IRCALLCOND_SOFTFP_FFI(x) NULL
59 #endif
61 #define LJ_NEED_FP64 LJ_TARGET_PPC
63 #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)
64 #define IRCALLCOND_FP64_FFI(x) x
65 #else
66 #define IRCALLCOND_FP64_FFI(x) NULL
67 #endif
69 #if LJ_HASFFI
70 #define IRCALLCOND_FFI(x) x
71 #if LJ_32
72 #define IRCALLCOND_FFI32(x) x
73 #else
74 #define IRCALLCOND_FFI32(x) NULL
75 #endif
76 #else
77 #define IRCALLCOND_FFI(x) NULL
78 #define IRCALLCOND_FFI32(x) NULL
79 #endif
81 #if LJ_SOFTFP
82 #define ARG1_FP 2 /* Treat as 2 32 bit arguments. */
83 #else
84 #define ARG1_FP 1
85 #endif
87 #if LJ_32
88 #define ARG2_64 4 /* Treat as 4 32 bit arguments. */
89 #else
90 #define ARG2_64 2
91 #endif
93 /* Function definitions for CALL* instructions. */
94 #define IRCALLDEF(_) \
95 _(ANY, lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \
96 _(ANY, lj_str_new, 3, S, STR, CCI_L) \
97 _(ANY, lj_str_tonum, 2, FN, INT, 0) \
98 _(ANY, lj_str_fromint, 2, FN, STR, CCI_L) \
99 _(ANY, lj_str_fromnum, 2, FN, STR, CCI_L) \
100 _(ANY, lj_tab_new1, 2, FS, TAB, CCI_L) \
101 _(ANY, lj_tab_dup, 2, FS, TAB, CCI_L) \
102 _(ANY, lj_tab_newkey, 3, S, P32, CCI_L) \
103 _(ANY, lj_tab_len, 1, FL, INT, 0) \
104 _(ANY, lj_gc_step_jit, 2, FS, NIL, CCI_L) \
105 _(ANY, lj_gc_barrieruv, 2, FS, NIL, 0) \
106 _(ANY, lj_mem_newgco, 2, FS, P32, CCI_L) \
107 _(ANY, lj_math_random_step, 1, FS, NUM, CCI_CASTU64|CCI_NOFPRCLOBBER) \
108 _(ANY, lj_vm_modi, 2, FN, INT, 0) \
109 _(ANY, sinh, 1, N, NUM, 0) \
110 _(ANY, cosh, 1, N, NUM, 0) \
111 _(ANY, tanh, 1, N, NUM, 0) \
112 _(ANY, fputc, 2, S, INT, 0) \
113 _(ANY, fwrite, 4, S, INT, 0) \
114 _(ANY, fflush, 1, S, INT, 0) \
115 /* ORDER FPM */ \
116 _(FPMATH, lj_vm_floor, ARG1_FP, N, NUM, 0) \
117 _(FPMATH, lj_vm_ceil, ARG1_FP, N, NUM, 0) \
118 _(FPMATH, lj_vm_trunc, ARG1_FP, N, NUM, 0) \
119 _(FPMATH, sqrt, ARG1_FP, N, NUM, 0) \
120 _(FPMATH, exp, ARG1_FP, N, NUM, 0) \
121 _(FPMATH, lj_vm_exp2, ARG1_FP, N, NUM, 0) \
122 _(FPMATH, log, ARG1_FP, N, NUM, 0) \
123 _(FPMATH, lj_vm_log2, ARG1_FP, N, NUM, 0) \
124 _(FPMATH, log10, ARG1_FP, N, NUM, 0) \
125 _(FPMATH, sin, ARG1_FP, N, NUM, 0) \
126 _(FPMATH, cos, ARG1_FP, N, NUM, 0) \
127 _(FPMATH, tan, ARG1_FP, N, NUM, 0) \
128 _(FPMATH, lj_vm_powi, ARG1_FP+1, N, NUM, 0) \
129 _(FPMATH, pow, ARG1_FP*2, N, NUM, 0) \
130 _(FPMATH, atan2, ARG1_FP*2, N, NUM, 0) \
131 _(FPMATH, ldexp, ARG1_FP+1, N, NUM, 0) \
132 _(SOFTFP, lj_vm_tobit, 2, N, INT, 0) \
133 _(SOFTFP, softfp_add, 4, N, NUM, 0) \
134 _(SOFTFP, softfp_sub, 4, N, NUM, 0) \
135 _(SOFTFP, softfp_mul, 4, N, NUM, 0) \
136 _(SOFTFP, softfp_div, 4, N, NUM, 0) \
137 _(SOFTFP, softfp_cmp, 4, N, NIL, 0) \
138 _(SOFTFP, softfp_i2d, 1, N, NUM, 0) \
139 _(SOFTFP, softfp_d2i, 2, N, INT, 0) \
140 _(SOFTFP_FFI, softfp_ui2d, 1, N, NUM, 0) \
141 _(SOFTFP_FFI, softfp_f2d, 1, N, NUM, 0) \
142 _(SOFTFP_FFI, softfp_d2ui, 2, N, INT, 0) \
143 _(SOFTFP_FFI, softfp_d2f, 2, N, FLOAT, 0) \
144 _(SOFTFP_FFI, softfp_i2f, 1, N, FLOAT, 0) \
145 _(SOFTFP_FFI, softfp_ui2f, 1, N, FLOAT, 0) \
146 _(SOFTFP_FFI, softfp_f2i, 1, N, INT, 0) \
147 _(SOFTFP_FFI, softfp_f2ui, 1, N, INT, 0) \
148 _(FP64_FFI, fp64_l2d, 2, N, NUM, 0) \
149 _(FP64_FFI, fp64_ul2d, 2, N, NUM, 0) \
150 _(FP64_FFI, fp64_l2f, 2, N, FLOAT, 0) \
151 _(FP64_FFI, fp64_ul2f, 2, N, FLOAT, 0) \
152 _(FP64_FFI, fp64_d2l, 2, N, I64, 0) \
153 _(FP64_FFI, fp64_d2ul, 2, N, U64, 0) \
154 _(FP64_FFI, fp64_f2l, 1, N, I64, 0) \
155 _(FP64_FFI, fp64_f2ul, 1, N, U64, 0) \
156 _(FFI, lj_carith_divi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
157 _(FFI, lj_carith_divu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
158 _(FFI, lj_carith_modi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
159 _(FFI, lj_carith_modu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
160 _(FFI, lj_carith_powi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
161 _(FFI, lj_carith_powu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
162 _(FFI, lj_cdata_setfin, 2, FN, P32, CCI_L) \
163 _(FFI, strlen, 1, N, INTP, 0) \
164 _(FFI, memcpy, 3, S, PTR, 0) \
165 _(FFI, memset, 3, S, PTR, 0) \
166 _(FFI32, lj_carith_mul64, ARG2_64, N, I64, CCI_NOFPRCLOBBER)
168 /* End of list. */
170 typedef enum {
171 #define IRCALLENUM(cond, name, nargs, kind, type, flags) IRCALL_##name,
172 IRCALLDEF(IRCALLENUM)
173 #undef IRCALLENUM
174 IRCALL__MAX
175 } IRCallID;
177 LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...);
179 LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1];
181 /* Soft-float declarations. */
182 #if LJ_SOFTFP
183 #if LJ_TARGET_ARM
184 #define softfp_add __aeabi_dadd
185 #define softfp_sub __aeabi_dsub
186 #define softfp_mul __aeabi_dmul
187 #define softfp_div __aeabi_ddiv
188 #define softfp_cmp __aeabi_cdcmple
189 #define softfp_i2d __aeabi_i2d
190 #define softfp_d2i __aeabi_d2iz
191 #define softfp_ui2d __aeabi_ui2d
192 #define softfp_f2d __aeabi_f2d
193 #define softfp_d2ui __aeabi_d2uiz
194 #define softfp_d2f __aeabi_d2f
195 #define softfp_i2f __aeabi_i2f
196 #define softfp_ui2f __aeabi_ui2f
197 #define softfp_f2i __aeabi_f2iz
198 #define softfp_f2ui __aeabi_f2uiz
199 #define fp64_l2d __aeabi_l2d
200 #define fp64_ul2d __aeabi_ul2d
201 #define fp64_l2f __aeabi_l2f
202 #define fp64_ul2f __aeabi_ul2f
203 #if LJ_TARGET_OSX
204 #define fp64_d2l __fixdfdi
205 #define fp64_d2ul __fixunsdfdi
206 #define fp64_f2l __fixsfdi
207 #define fp64_f2ul __fixunssfdi
208 #else
209 #define fp64_d2l __aeabi_d2lz
210 #define fp64_d2ul __aeabi_d2ulz
211 #define fp64_f2l __aeabi_f2lz
212 #define fp64_f2ul __aeabi_f2ulz
213 #endif
214 #else
215 #error "Missing soft-float definitions for target architecture"
216 #endif
217 extern double softfp_add(double a, double b);
218 extern double softfp_sub(double a, double b);
219 extern double softfp_mul(double a, double b);
220 extern double softfp_div(double a, double b);
221 extern void softfp_cmp(double a, double b);
222 extern double softfp_i2d(int32_t a);
223 extern int32_t softfp_d2i(double a);
224 #if LJ_HASFFI
225 extern double softfp_ui2d(uint32_t a);
226 extern double softfp_f2d(float a);
227 extern uint32_t softfp_d2ui(double a);
228 extern float softfp_d2f(double a);
229 extern float softfp_i2f(int32_t a);
230 extern float softfp_ui2f(uint32_t a);
231 extern int32_t softfp_f2i(float a);
232 extern uint32_t softfp_f2ui(float a);
233 #endif
234 #endif
236 #if LJ_HASFFI && LJ_NEED_FP64
237 #ifdef __GNUC__
238 #define fp64_l2d __floatdidf
239 #define fp64_ul2d __floatundidf
240 #define fp64_l2f __floatdisf
241 #define fp64_ul2f __floatundisf
242 #define fp64_d2l __fixdfdi
243 #define fp64_d2ul __fixunsdfdi
244 #define fp64_f2l __fixsfdi
245 #define fp64_f2ul __fixunssfdi
246 #else
247 #error "Missing fp64 helper definitions for this compiler"
248 #endif
249 #endif
251 #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)
252 extern double fp64_l2d(int64_t a);
253 extern double fp64_ul2d(uint64_t a);
254 extern float fp64_l2f(int64_t a);
255 extern float fp64_ul2f(uint64_t a);
256 extern int64_t fp64_d2l(double a);
257 extern uint64_t fp64_d2ul(double a);
258 extern int64_t fp64_f2l(float a);
259 extern uint64_t fp64_f2ul(float a);
260 #endif
262 #endif