PPC: Add machine-specific part of FFI.
[luajit-2.0.git] / src / lj_ircall.h
bloba1f0b05298247791ec8f866638a01f9b2a9f830a
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 /* Function definitions for CALL* instructions. */
41 #if LJ_SOFTFP
42 #if LJ_HASFFI
43 #define IRCALLDEF_SOFTFP_FFI(_) \
44 _(softfp_ui2d, 1, N, NUM, 0) \
45 _(softfp_l2d, 2, N, NUM, 0) \
46 _(softfp_ul2d, 2, N, NUM, 0) \
47 _(softfp_f2d, 1, N, NUM, 0) \
48 _(softfp_d2ui, 2, N, INT, 0) \
49 _(softfp_d2l, 2, N, I64, 0) \
50 _(softfp_d2ul, 2, N, U64, 0) \
51 _(softfp_d2f, 2, N, FLOAT, 0) \
52 _(softfp_i2f, 1, N, FLOAT, 0) \
53 _(softfp_ui2f, 1, N, FLOAT, 0) \
54 _(softfp_l2f, 2, N, FLOAT, 0) \
55 _(softfp_ul2f, 2, N, FLOAT, 0) \
56 _(softfp_f2i, 1, N, INT, 0) \
57 _(softfp_f2ui, 1, N, INT, 0) \
58 _(softfp_f2l, 1, N, I64, 0) \
59 _(softfp_f2ul, 1, N, U64, 0)
60 #else
61 #define IRCALLDEF_SOFTFP_FFI(_)
62 #endif
63 #define IRCALLDEF_SOFTFP(_) \
64 _(lj_vm_tobit, 2, N, INT, 0) \
65 _(softfp_add, 4, N, NUM, 0) \
66 _(softfp_sub, 4, N, NUM, 0) \
67 _(softfp_mul, 4, N, NUM, 0) \
68 _(softfp_div, 4, N, NUM, 0) \
69 _(softfp_cmp, 4, N, NIL, 0) \
70 _(softfp_i2d, 1, N, NUM, 0) \
71 _(softfp_d2i, 2, N, INT, 0) \
72 IRCALLDEF_SOFTFP_FFI(_)
73 #else
74 #define IRCALLDEF_SOFTFP(_)
75 #endif
77 #if LJ_TARGET_X86ORX64
78 /* Use lj_vm_* helpers and x87 ops. */
79 #define IRCALLDEF_FPMATH(_)
80 #else
81 /* Use standard math library calls. */
82 #if LJ_SOFTFP
83 #define ARG1_FP 2 /* Treat as 2 32 bit arguments. */
84 #else
85 #define ARG1_FP 1
86 #endif
87 /* ORDER FPM */
88 #define IRCALLDEF_FPMATH(_) \
89 _(lj_vm_floor, ARG1_FP, N, NUM, 0) \
90 _(lj_vm_ceil, ARG1_FP, N, NUM, 0) \
91 _(lj_vm_trunc, ARG1_FP, N, NUM, 0) \
92 _(sqrt, ARG1_FP, N, NUM, 0) \
93 _(exp, ARG1_FP, N, NUM, 0) \
94 _(lj_vm_exp2, ARG1_FP, N, NUM, 0) \
95 _(log, ARG1_FP, N, NUM, 0) \
96 _(lj_vm_log2, ARG1_FP, N, NUM, 0) \
97 _(log10, ARG1_FP, N, NUM, 0) \
98 _(sin, ARG1_FP, N, NUM, 0) \
99 _(cos, ARG1_FP, N, NUM, 0) \
100 _(tan, ARG1_FP, N, NUM, 0) \
101 _(lj_vm_powi, ARG1_FP+1, N, NUM, 0) \
102 _(pow, ARG1_FP*2, N, NUM, 0) \
103 _(atan2, ARG1_FP*2, N, NUM, 0) \
104 _(ldexp, ARG1_FP+1, N, NUM, 0)
105 #endif
107 #if LJ_HASFFI
108 #if LJ_32
109 #define ARG2_64 4 /* Treat as 4 32 bit arguments. */
110 #define IRCALLDEF_FFI32(_) \
111 _(lj_carith_mul64, ARG2_64, N, I64, CCI_NOFPRCLOBBER)
112 #else
113 #define ARG2_64 2
114 #define IRCALLDEF_FFI32(_)
115 #endif
116 #define IRCALLDEF_FFI(_) \
117 IRCALLDEF_FFI32(_) \
118 _(lj_carith_divi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
119 _(lj_carith_divu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
120 _(lj_carith_modi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
121 _(lj_carith_modu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
122 _(lj_carith_powi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \
123 _(lj_carith_powu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \
124 _(lj_cdata_setfin, 2, FN, P32, CCI_L) \
125 _(strlen, 1, N, INTP, 0) \
126 _(memcpy, 3, S, PTR, 0) \
127 _(memset, 3, S, PTR, 0)
128 #else
129 #define IRCALLDEF_FFI(_)
130 #endif
132 #define IRCALLDEF(_) \
133 _(lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \
134 _(lj_str_new, 3, S, STR, CCI_L) \
135 _(lj_str_tonum, 2, FN, INT, 0) \
136 _(lj_str_fromint, 2, FN, STR, CCI_L) \
137 _(lj_str_fromnum, 2, FN, STR, CCI_L) \
138 _(lj_tab_new1, 2, FS, TAB, CCI_L) \
139 _(lj_tab_dup, 2, FS, TAB, CCI_L) \
140 _(lj_tab_newkey, 3, S, P32, CCI_L) \
141 _(lj_tab_len, 1, FL, INT, 0) \
142 _(lj_gc_step_jit, 2, FS, NIL, CCI_L) \
143 _(lj_gc_barrieruv, 2, FS, NIL, 0) \
144 _(lj_mem_newgco, 2, FS, P32, CCI_L) \
145 _(lj_math_random_step, 1, FS, NUM, CCI_CASTU64|CCI_NOFPRCLOBBER) \
146 _(lj_vm_modi, 2, FN, INT, 0) \
147 IRCALLDEF_SOFTFP(_) \
148 IRCALLDEF_FPMATH(_) \
149 IRCALLDEF_FFI(_) \
150 _(sinh, 1, N, NUM, 0) \
151 _(cosh, 1, N, NUM, 0) \
152 _(tanh, 1, N, NUM, 0) \
153 _(fputc, 2, S, INT, 0) \
154 _(fwrite, 4, S, INT, 0) \
155 _(fflush, 1, S, INT, 0) \
157 /* End of list. */
159 typedef enum {
160 #define IRCALLENUM(name, nargs, kind, type, flags) IRCALL_##name,
161 IRCALLDEF(IRCALLENUM)
162 #undef IRCALLENUM
163 IRCALL__MAX
164 } IRCallID;
166 LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...);
168 LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1];
170 /* Soft-float declarations. */
171 #if LJ_SOFTFP
172 #if LJ_TARGET_ARM
173 #define softfp_add __aeabi_dadd
174 #define softfp_sub __aeabi_dsub
175 #define softfp_mul __aeabi_dmul
176 #define softfp_div __aeabi_ddiv
177 #define softfp_cmp __aeabi_cdcmple
178 #define softfp_i2d __aeabi_i2d
179 #define softfp_ui2d __aeabi_ui2d
180 #define softfp_l2d __aeabi_l2d
181 #define softfp_ul2d __aeabi_ul2d
182 #define softfp_f2d __aeabi_f2d
183 #define softfp_d2i __aeabi_d2iz
184 #define softfp_d2ui __aeabi_d2uiz
185 #define softfp_d2f __aeabi_d2f
186 #define softfp_i2f __aeabi_i2f
187 #define softfp_ui2f __aeabi_ui2f
188 #define softfp_l2f __aeabi_l2f
189 #define softfp_ul2f __aeabi_ul2f
190 #define softfp_f2i __aeabi_f2iz
191 #define softfp_f2ui __aeabi_f2uiz
192 #if LJ_TARGET_OSX
193 #define softfp_d2l __fixdfdi
194 #define softfp_d2ul __fixunsdfdi
195 #define softfp_f2l __fixsfdi
196 #define softfp_f2ul __fixunssfdi
197 #else
198 #define softfp_d2l __aeabi_d2lz
199 #define softfp_d2ul __aeabi_d2ulz
200 #define softfp_f2l __aeabi_f2lz
201 #define softfp_f2ul __aeabi_f2ulz
202 #endif
203 #else
204 #error "Missing soft-float definitions for target architecture"
205 #endif
206 extern double softfp_add(double a, double b);
207 extern double softfp_sub(double a, double b);
208 extern double softfp_mul(double a, double b);
209 extern double softfp_div(double a, double b);
210 extern void softfp_cmp(double a, double b);
211 extern double softfp_i2d(int32_t a);
212 extern double softfp_ui2d(uint32_t a);
213 extern double softfp_l2d(int64_t a);
214 extern double softfp_ul2d(uint64_t a);
215 extern double softfp_f2d(float a);
216 extern int32_t softfp_d2i(double a);
217 extern uint32_t softfp_d2ui(double a);
218 extern int64_t softfp_d2l(double a);
219 extern uint64_t softfp_d2ul(double a);
220 extern float softfp_d2f(double a);
221 extern float softfp_i2f(int32_t a);
222 extern float softfp_ui2f(uint32_t a);
223 extern float softfp_l2f(int64_t a);
224 extern float softfp_ul2f(uint64_t a);
225 extern int32_t softfp_f2i(float a);
226 extern uint32_t softfp_f2ui(float a);
227 extern int64_t softfp_f2l(float a);
228 extern uint64_t softfp_f2ul(float a);
229 #endif
231 #endif