src/lj_ircall.h - luajit-2.0-src

Global variables defined

Data types defined

Macros defined

Source code

  1. /*
  2. ** IR CALL* instruction definitions.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #ifndef _LJ_IRCALL_H
  6. #define _LJ_IRCALL_H

  7. #include "lj_obj.h"
  8. #include "lj_ir.h"
  9. #include "lj_jit.h"

  10. /* C call info for CALL* instructions. */
  11. typedef struct CCallInfo {
  12.   ASMFunction func;                /* Function pointer. */
  13.   uint32_t flags;                /* Number of arguments and flags. */
  14. } CCallInfo;

  15. #define CCI_NARGS(ci)                ((ci)->flags & 0xff)        /* # of args. */
  16. #define CCI_NARGS_MAX                32                        /* Max. # of args. */

  17. #define CCI_OTSHIFT                16
  18. #define CCI_OPTYPE(ci)                ((ci)->flags >> CCI_OTSHIFT/* Get op/type. */
  19. #define CCI_OPSHIFT                24
  20. #define CCI_OP(ci)                ((ci)->flags >> CCI_OPSHIFT/* Get op. */

  21. #define CCI_CALL_N                (IR_CALLN << CCI_OPSHIFT)
  22. #define CCI_CALL_A                (IR_CALLA << CCI_OPSHIFT)
  23. #define CCI_CALL_L                (IR_CALLL << CCI_OPSHIFT)
  24. #define CCI_CALL_S                (IR_CALLS << CCI_OPSHIFT)
  25. #define CCI_CALL_FN                (CCI_CALL_N|CCI_CC_FASTCALL)
  26. #define CCI_CALL_FL                (CCI_CALL_L|CCI_CC_FASTCALL)
  27. #define CCI_CALL_FS                (CCI_CALL_S|CCI_CC_FASTCALL)

  28. /* C call info flags. */
  29. #define CCI_L                        0x0100        /* Implicit L arg. */
  30. #define CCI_CASTU64                0x0200        /* Cast u64 result to number. */
  31. #define CCI_NOFPRCLOBBER        0x0400        /* Does not clobber any FPRs. */
  32. #define CCI_VARARG                0x0800        /* Vararg function. */

  33. #define CCI_CC_MASK                0x3000        /* Calling convention mask. */
  34. #define CCI_CC_SHIFT                12
  35. /* ORDER CC */
  36. #define CCI_CC_CDECL                0x0000        /* Default cdecl calling convention. */
  37. #define CCI_CC_THISCALL                0x1000        /* Thiscall calling convention. */
  38. #define CCI_CC_FASTCALL                0x2000        /* Fastcall calling convention. */
  39. #define CCI_CC_STDCALL                0x3000        /* Stdcall calling convention. */

  40. /* Extra args for SOFTFP, SPLIT 64 bit. */
  41. #define CCI_XARGS_SHIFT                14
  42. #define CCI_XARGS(ci)                (((ci)->flags >> CCI_XARGS_SHIFT) & 3)
  43. #define CCI_XA                        (1u << CCI_XARGS_SHIFT)

  44. #if LJ_SOFTFP || (LJ_32 && LJ_HASFFI)
  45. #define CCI_XNARGS(ci)                (CCI_NARGS((ci)) + CCI_XARGS((ci)))
  46. #else
  47. #define CCI_XNARGS(ci)                CCI_NARGS((ci))
  48. #endif

  49. /* Helpers for conditional function definitions. */
  50. #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

  56. #if LJ_SOFTFP
  57. #define IRCALLCOND_SOFTFP(x)                x
  58. #if LJ_HASFFI
  59. #define IRCALLCOND_SOFTFP_FFI(x)        x
  60. #else
  61. #define IRCALLCOND_SOFTFP_FFI(x)        NULL
  62. #endif
  63. #else
  64. #define IRCALLCOND_SOFTFP(x)                NULL
  65. #define IRCALLCOND_SOFTFP_FFI(x)        NULL
  66. #endif

  67. #define LJ_NEED_FP64        (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS)

  68. #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)
  69. #define IRCALLCOND_FP64_FFI(x)                x
  70. #else
  71. #define IRCALLCOND_FP64_FFI(x)                NULL
  72. #endif

  73. #if LJ_HASFFI
  74. #define IRCALLCOND_FFI(x)                x
  75. #if LJ_32
  76. #define IRCALLCOND_FFI32(x)                x
  77. #else
  78. #define IRCALLCOND_FFI32(x)                NULL
  79. #endif
  80. #else
  81. #define IRCALLCOND_FFI(x)                NULL
  82. #define IRCALLCOND_FFI32(x)                NULL
  83. #endif

  84. #if LJ_TARGET_X86
  85. #define CCI_RANDFPR        0        /* Clang on OSX/x86 is overzealous. */
  86. #else
  87. #define CCI_RANDFPR        CCI_NOFPRCLOBBER
  88. #endif

  89. #if LJ_SOFTFP
  90. #define XA_FP                CCI_XA
  91. #define XA2_FP                (CCI_XA+CCI_XA)
  92. #else
  93. #define XA_FP                0
  94. #define XA2_FP                0
  95. #endif

  96. #if LJ_32
  97. #define XA_64                CCI_XA
  98. #define XA2_64                (CCI_XA+CCI_XA)
  99. #else
  100. #define XA_64                0
  101. #define XA2_64                0
  102. #endif

  103. /* Function definitions for CALL* instructions. */
  104. #define IRCALLDEF(_) \
  105.   _(ANY,        lj_str_cmp,                2,  FN, INT, CCI_NOFPRCLOBBER) \
  106.   _(ANY,        lj_str_find,                4,   N, P32, 0) \
  107.   _(ANY,        lj_str_new,                3,   S, STR, CCI_L) \
  108.   _(ANY,        lj_strscan_num,                2,  FN, INT, 0) \
  109.   _(ANY,        lj_strfmt_int,                2,  FN, STR, CCI_L) \
  110.   _(ANY,        lj_strfmt_num,                2,  FN, STR, CCI_L) \
  111.   _(ANY,        lj_strfmt_char,                2,  FN, STR, CCI_L) \
  112.   _(ANY,        lj_strfmt_putint,        2,  FL, P32, 0) \
  113.   _(ANY,        lj_strfmt_putnum,        2,  FL, P32, 0) \
  114.   _(ANY,        lj_strfmt_putquoted,        2,  FL, P32, 0) \
  115.   _(ANY,        lj_strfmt_putfxint,        3,   L, P32, XA_64) \
  116.   _(ANY,        lj_strfmt_putfnum_int,        3,   L, P32, XA_FP) \
  117.   _(ANY,        lj_strfmt_putfnum_uint,        3,   L, P32, XA_FP) \
  118.   _(ANY,        lj_strfmt_putfnum,        3,   L, P32, XA_FP) \
  119.   _(ANY,        lj_strfmt_putfstr,        3,   L, P32, 0) \
  120.   _(ANY,        lj_strfmt_putfchar,        3,   L, P32, 0) \
  121.   _(ANY,        lj_buf_putmem,                3,   S, P32, 0) \
  122.   _(ANY,        lj_buf_putstr,                2,  FL, P32, 0) \
  123.   _(ANY,        lj_buf_putchar,                2,  FL, P32, 0) \
  124.   _(ANY,        lj_buf_putstr_reverse,        2,  FL, P32, 0) \
  125.   _(ANY,        lj_buf_putstr_lower,        2,  FL, P32, 0) \
  126.   _(ANY,        lj_buf_putstr_upper,        2,  FL, P32, 0) \
  127.   _(ANY,        lj_buf_putstr_rep,        3,   L, P32, 0) \
  128.   _(ANY,        lj_buf_puttab,                5,   L, P32, 0) \
  129.   _(ANY,        lj_buf_tostr,                1,  FL, STR, 0) \
  130.   _(ANY,        lj_tab_new_ah,                3,   A, TAB, CCI_L) \
  131.   _(ANY,        lj_tab_new1,                2,  FS, TAB, CCI_L) \
  132.   _(ANY,        lj_tab_dup,                2,  FS, TAB, CCI_L) \
  133.   _(ANY,        lj_tab_clear,                1,  FS, NIL, 0) \
  134.   _(ANY,        lj_tab_newkey,                3,   S, P32, CCI_L) \
  135.   _(ANY,        lj_tab_len,                1,  FL, INT, 0) \
  136.   _(ANY,        lj_gc_step_jit,                2,  FS, NIL, CCI_L) \
  137.   _(ANY,        lj_gc_barrieruv,        2,  FS, NIL, 0) \
  138.   _(ANY,        lj_mem_newgco,                2,  FS, P32, CCI_L) \
  139.   _(ANY,        lj_math_random_step, 1, FS, NUM, CCI_CASTU64|CCI_RANDFPR)\
  140.   _(ANY,        lj_vm_modi,                2,  FN, INT, 0) \
  141.   _(ANY,        sinh,                        1,   N, NUM, XA_FP) \
  142.   _(ANY,        cosh,                        1,   N, NUM, XA_FP) \
  143.   _(ANY,        tanh,                        1,   N, NUM, XA_FP) \
  144.   _(ANY,        fputc,                        2,   S, INT, 0) \
  145.   _(ANY,        fwrite,                        4,   S, INT, 0) \
  146.   _(ANY,        fflush,                        1,   S, INT, 0) \
  147.   /* ORDER FPM */ \
  148.   _(FPMATH,        lj_vm_floor,                1,   N, NUM, XA_FP) \
  149.   _(FPMATH,        lj_vm_ceil,                1,   N, NUM, XA_FP) \
  150.   _(FPMATH,        lj_vm_trunc,                1,   N, NUM, XA_FP) \
  151.   _(FPMATH,        sqrt,                        1,   N, NUM, XA_FP) \
  152.   _(ANY,        exp,                        1,   N, NUM, XA_FP) \
  153.   _(ANY,        lj_vm_exp2,                1,   N, NUM, XA_FP) \
  154.   _(ANY,        log,                        1,   N, NUM, XA_FP) \
  155.   _(ANY,        lj_vm_log2,                1,   N, NUM, XA_FP) \
  156.   _(ANY,        log10,                        1,   N, NUM, XA_FP) \
  157.   _(ANY,        sin,                        1,   N, NUM, XA_FP) \
  158.   _(ANY,        cos,                        1,   N, NUM, XA_FP) \
  159.   _(ANY,        tan,                        1,   N, NUM, XA_FP) \
  160.   _(ANY,        lj_vm_powi,                2,   N, NUM, XA_FP) \
  161.   _(ANY,        pow,                        2,   N, NUM, XA2_FP) \
  162.   _(ANY,        atan2,                        2,   N, NUM, XA2_FP) \
  163.   _(ANY,        ldexp,                        2,   N, NUM, XA_FP) \
  164.   _(SOFTFP,        lj_vm_tobit,                2,   N, INT, 0) \
  165.   _(SOFTFP,        softfp_add,                4,   N, NUM, 0) \
  166.   _(SOFTFP,        softfp_sub,                4,   N, NUM, 0) \
  167.   _(SOFTFP,        softfp_mul,                4,   N, NUM, 0) \
  168.   _(SOFTFP,        softfp_div,                4,   N, NUM, 0) \
  169.   _(SOFTFP,        softfp_cmp,                4,   N, NIL, 0) \
  170.   _(SOFTFP,        softfp_i2d,                1,   N, NUM, 0) \
  171.   _(SOFTFP,        softfp_d2i,                2,   N, INT, 0) \
  172.   _(SOFTFP_FFI,        softfp_ui2d,                1,   N, NUM, 0) \
  173.   _(SOFTFP_FFI,        softfp_f2d,                1,   N, NUM, 0) \
  174.   _(SOFTFP_FFI,        softfp_d2ui,                2,   N, INT, 0) \
  175.   _(SOFTFP_FFI,        softfp_d2f,                2,   N, FLOAT, 0) \
  176.   _(SOFTFP_FFI,        softfp_i2f,                1,   N, FLOAT, 0) \
  177.   _(SOFTFP_FFI,        softfp_ui2f,                1,   N, FLOAT, 0) \
  178.   _(SOFTFP_FFI,        softfp_f2i,                1,   N, INT, 0) \
  179.   _(SOFTFP_FFI,        softfp_f2ui,                1,   N, INT, 0) \
  180.   _(FP64_FFI,        fp64_l2d,                1,   N, NUM, XA_64) \
  181.   _(FP64_FFI,        fp64_ul2d,                1,   N, NUM, XA_64) \
  182.   _(FP64_FFI,        fp64_l2f,                1,   N, FLOAT, XA_64) \
  183.   _(FP64_FFI,        fp64_ul2f,                1,   N, FLOAT, XA_64) \
  184.   _(FP64_FFI,        fp64_d2l,                1,   N, I64, XA_FP) \
  185.   _(FP64_FFI,        fp64_d2ul,                1,   N, U64, XA_FP) \
  186.   _(FP64_FFI,        fp64_f2l,                1,   N, I64, 0) \
  187.   _(FP64_FFI,        fp64_f2ul,                1,   N, U64, 0) \
  188.   _(FFI,        lj_carith_divi64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \
  189.   _(FFI,        lj_carith_divu64,        2,   N, U64, XA2_64|CCI_NOFPRCLOBBER) \
  190.   _(FFI,        lj_carith_modi64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \
  191.   _(FFI,        lj_carith_modu64,        2,   N, U64, XA2_64|CCI_NOFPRCLOBBER) \
  192.   _(FFI,        lj_carith_powi64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \
  193.   _(FFI,        lj_carith_powu64,        2,   N, U64, XA2_64|CCI_NOFPRCLOBBER) \
  194.   _(FFI,        lj_cdata_newv,                4,   S, CDATA, CCI_L) \
  195.   _(FFI,        lj_cdata_setfin,        4,   S, NIL, CCI_L) \
  196.   _(FFI,        strlen,                        1,   L, INTP, 0) \
  197.   _(FFI,        memcpy,                        3,   S, PTR, 0) \
  198.   _(FFI,        memset,                        3,   S, PTR, 0) \
  199.   _(FFI,        lj_vm_errno,                0,   S, INT, CCI_NOFPRCLOBBER) \
  200.   _(FFI32,        lj_carith_mul64,        2,   N, I64, XA2_64|CCI_NOFPRCLOBBER) \
  201.   _(FFI32,        lj_carith_shl64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \
  202.   _(FFI32,        lj_carith_shr64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \
  203.   _(FFI32,        lj_carith_sar64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \
  204.   _(FFI32,        lj_carith_rol64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \
  205.   _(FFI32,        lj_carith_ror64,        2,   N, U64, XA_64|CCI_NOFPRCLOBBER) \
  206.   \
  207.   /* End of list. */

  208. typedef enum {
  209. #define IRCALLENUM(cond, name, nargs, kind, type, flags)        IRCALL_##name,
  210. IRCALLDEF(IRCALLENUM)
  211. #undef IRCALLENUM
  212.   IRCALL__MAX
  213. } IRCallID;

  214. LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...);

  215. LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1];

  216. /* Soft-float declarations. */
  217. #if LJ_SOFTFP
  218. #if LJ_TARGET_ARM
  219. #define softfp_add __aeabi_dadd
  220. #define softfp_sub __aeabi_dsub
  221. #define softfp_mul __aeabi_dmul
  222. #define softfp_div __aeabi_ddiv
  223. #define softfp_cmp __aeabi_cdcmple
  224. #define softfp_i2d __aeabi_i2d
  225. #define softfp_d2i __aeabi_d2iz
  226. #define softfp_ui2d __aeabi_ui2d
  227. #define softfp_f2d __aeabi_f2d
  228. #define softfp_d2ui __aeabi_d2uiz
  229. #define softfp_d2f __aeabi_d2f
  230. #define softfp_i2f __aeabi_i2f
  231. #define softfp_ui2f __aeabi_ui2f
  232. #define softfp_f2i __aeabi_f2iz
  233. #define softfp_f2ui __aeabi_f2uiz
  234. #define fp64_l2d __aeabi_l2d
  235. #define fp64_ul2d __aeabi_ul2d
  236. #define fp64_l2f __aeabi_l2f
  237. #define fp64_ul2f __aeabi_ul2f
  238. #if LJ_TARGET_IOS
  239. #define fp64_d2l __fixdfdi
  240. #define fp64_d2ul __fixunsdfdi
  241. #define fp64_f2l __fixsfdi
  242. #define fp64_f2ul __fixunssfdi
  243. #else
  244. #define fp64_d2l __aeabi_d2lz
  245. #define fp64_d2ul __aeabi_d2ulz
  246. #define fp64_f2l __aeabi_f2lz
  247. #define fp64_f2ul __aeabi_f2ulz
  248. #endif
  249. #else
  250. #error "Missing soft-float definitions for target architecture"
  251. #endif
  252. extern double softfp_add(double a, double b);
  253. extern double softfp_sub(double a, double b);
  254. extern double softfp_mul(double a, double b);
  255. extern double softfp_div(double a, double b);
  256. extern void softfp_cmp(double a, double b);
  257. extern double softfp_i2d(int32_t a);
  258. extern int32_t softfp_d2i(double a);
  259. #if LJ_HASFFI
  260. extern double softfp_ui2d(uint32_t a);
  261. extern double softfp_f2d(float a);
  262. extern uint32_t softfp_d2ui(double a);
  263. extern float softfp_d2f(double a);
  264. extern float softfp_i2f(int32_t a);
  265. extern float softfp_ui2f(uint32_t a);
  266. extern int32_t softfp_f2i(float a);
  267. extern uint32_t softfp_f2ui(float a);
  268. #endif
  269. #endif

  270. #if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP)
  271. #ifdef __GNUC__
  272. #define fp64_l2d __floatdidf
  273. #define fp64_ul2d __floatundidf
  274. #define fp64_l2f __floatdisf
  275. #define fp64_ul2f __floatundisf
  276. #define fp64_d2l __fixdfdi
  277. #define fp64_d2ul __fixunsdfdi
  278. #define fp64_f2l __fixsfdi
  279. #define fp64_f2ul __fixunssfdi
  280. #else
  281. #error "Missing fp64 helper definitions for this compiler"
  282. #endif
  283. #endif

  284. #if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64)
  285. extern double fp64_l2d(int64_t a);
  286. extern double fp64_ul2d(uint64_t a);
  287. extern float fp64_l2f(int64_t a);
  288. extern float fp64_ul2f(uint64_t a);
  289. extern int64_t fp64_d2l(double a);
  290. extern uint64_t fp64_d2ul(double a);
  291. extern int64_t fp64_f2l(float a);
  292. extern uint64_t fp64_f2ul(float a);
  293. #endif

  294. #endif