ljx

FORK: LuaJIT with native 5.2 and 5.3 support
git clone https://git.neptards.moe/neptards/ljx.git
Log | Files | Refs | README

lj_ircall.h (11479B)


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