lj_dispatch.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. ** Instruction dispatch handling.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_DISPATCH_H
  6. #define _LJ_DISPATCH_H
  7. #include "lj_obj.h"
  8. #include "lj_bc.h"
  9. #if LJ_HASJIT
  10. #include "lj_jit.h"
  11. #endif
  12. #if LJ_TARGET_MIPS
  13. /* Need our own global offset table for the dreaded MIPS calling conventions. */
  14. #ifndef _LJ_VM_H
  15. LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b);
  16. #endif
  17. #if LJ_SOFTFP
  18. #ifndef _LJ_IRCALL_H
  19. extern double __adddf3(double a, double b);
  20. extern double __subdf3(double a, double b);
  21. extern double __muldf3(double a, double b);
  22. extern double __divdf3(double a, double b);
  23. #endif
  24. #define SFGOTDEF(_) _(sqrt) _(__adddf3) _(__subdf3) _(__muldf3) _(__divdf3)
  25. #else
  26. #define SFGOTDEF(_)
  27. #endif
  28. #if LJ_HASJIT
  29. #define JITGOTDEF(_) _(lj_err_trace) _(lj_trace_exit) _(lj_trace_hot)
  30. #else
  31. #define JITGOTDEF(_)
  32. #endif
  33. #if LJ_HASFFI
  34. #define FFIGOTDEF(_) \
  35. _(lj_meta_equal_cd) _(lj_ccallback_enter) _(lj_ccallback_leave)
  36. #else
  37. #define FFIGOTDEF(_)
  38. #endif
  39. #define GOTDEF(_) \
  40. _(floor) _(ceil) _(trunc) _(log) _(log10) _(exp) _(sin) _(cos) _(tan) \
  41. _(asin) _(acos) _(atan) _(sinh) _(cosh) _(tanh) _(frexp) _(modf) _(atan2) \
  42. _(pow) _(fmod) _(ldexp) _(lj_vm_modi) \
  43. _(lj_dispatch_call) _(lj_dispatch_ins) _(lj_dispatch_stitch) \
  44. _(lj_dispatch_profile) _(lj_err_throw) \
  45. _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \
  46. _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \
  47. _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \
  48. _(lj_meta_for) _(lj_meta_istype) _(lj_meta_len) _(lj_meta_tget) \
  49. _(lj_meta_tset) _(lj_state_growstack) _(lj_strfmt_number) \
  50. _(lj_str_new) _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) \
  51. _(lj_tab_new) _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \
  52. _(lj_tab_setinth) _(lj_buf_putstr_reverse) _(lj_buf_putstr_lower) \
  53. _(lj_buf_putstr_upper) _(lj_buf_tostr) \
  54. JITGOTDEF(_) FFIGOTDEF(_) SFGOTDEF(_)
  55. enum {
  56. #define GOTENUM(name) LJ_GOT_##name,
  57. GOTDEF(GOTENUM)
  58. #undef GOTENUM
  59. LJ_GOT__MAX
  60. };
  61. #endif
  62. /* Type of hot counter. Must match the code in the assembler VM. */
  63. /* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */
  64. typedef uint16_t HotCount;
  65. /* Number of hot counter hash table entries (must be a power of two). */
  66. #define HOTCOUNT_SIZE 64
  67. #define HOTCOUNT_PCMASK ((HOTCOUNT_SIZE-1)*sizeof(HotCount))
  68. /* Hotcount decrements. */
  69. #define HOTCOUNT_LOOP 2
  70. #define HOTCOUNT_CALL 1
  71. /* This solves a circular dependency problem -- bump as needed. Sigh. */
  72. #define GG_NUM_ASMFF 57
  73. #define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF)
  74. #define GG_LEN_SDISP BC_FUNCF
  75. #define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP)
  76. /* Global state, main thread and extra fields are allocated together. */
  77. typedef struct GG_State {
  78. lua_State L; /* Main thread. */
  79. global_State g; /* Global state. */
  80. #if LJ_TARGET_ARM && !LJ_TARGET_NX
  81. /* Make g reachable via K12 encoded DISPATCH-relative addressing. */
  82. uint8_t align1[(16-sizeof(global_State))&15];
  83. #endif
  84. #if LJ_TARGET_MIPS
  85. ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */
  86. #endif
  87. #if LJ_HASJIT
  88. jit_State J; /* JIT state. */
  89. HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */
  90. #if LJ_TARGET_ARM && !LJ_TARGET_NX
  91. /* Ditto for J. */
  92. uint8_t align2[(16-sizeof(jit_State)-sizeof(HotCount)*HOTCOUNT_SIZE)&15];
  93. #endif
  94. #endif
  95. ASMFunction dispatch[GG_LEN_DISP]; /* Instruction dispatch tables. */
  96. BCIns bcff[GG_NUM_ASMFF]; /* Bytecode for ASM fast functions. */
  97. } GG_State;
  98. #define GG_OFS(field) ((int)offsetof(GG_State, field))
  99. #define G2GG(gl) ((GG_State *)((char *)(gl) - GG_OFS(g)))
  100. #define J2GG(j) ((GG_State *)((char *)(j) - GG_OFS(J)))
  101. #define L2GG(L) (G2GG(G(L)))
  102. #define J2G(J) (&J2GG(J)->g)
  103. #define G2J(gl) (&G2GG(gl)->J)
  104. #define L2J(L) (&L2GG(L)->J)
  105. #define GG_G2J (GG_OFS(J) - GG_OFS(g))
  106. #define GG_G2DISP (GG_OFS(dispatch) - GG_OFS(g))
  107. #define GG_DISP2G (GG_OFS(g) - GG_OFS(dispatch))
  108. #define GG_DISP2J (GG_OFS(J) - GG_OFS(dispatch))
  109. #define GG_DISP2HOT (GG_OFS(hotcount) - GG_OFS(dispatch))
  110. #define GG_DISP2STATIC (GG_LEN_DDISP*(int)sizeof(ASMFunction))
  111. #define hotcount_get(gg, pc) \
  112. (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)]
  113. #define hotcount_set(gg, pc, val) \
  114. (hotcount_get((gg), (pc)) = (HotCount)(val))
  115. /* Dispatch table management. */
  116. LJ_FUNC void lj_dispatch_init(GG_State *GG);
  117. #if LJ_HASJIT
  118. LJ_FUNC void lj_dispatch_init_hotcount(global_State *g);
  119. #endif
  120. LJ_FUNC void lj_dispatch_update(global_State *g);
  121. /* Instruction dispatch callback for hooks or when recording. */
  122. LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc);
  123. LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc);
  124. #if LJ_HASJIT
  125. LJ_FUNCA void LJ_FASTCALL lj_dispatch_stitch(jit_State *J, const BCIns *pc);
  126. #endif
  127. #if LJ_HASPROFILE
  128. LJ_FUNCA void LJ_FASTCALL lj_dispatch_profile(lua_State *L, const BCIns *pc);
  129. #endif
  130. #if LJ_HASFFI && !defined(_BUILDVM_H)
  131. /* Save/restore errno and GetLastError() around hooks, exits and recording. */
  132. #include <errno.h>
  133. #if LJ_TARGET_WINDOWS
  134. #define WIN32_LEAN_AND_MEAN
  135. #include <windows.h>
  136. #define ERRNO_SAVE int olderr = errno; DWORD oldwerr = GetLastError();
  137. #define ERRNO_RESTORE errno = olderr; SetLastError(oldwerr);
  138. #else
  139. #define ERRNO_SAVE int olderr = errno;
  140. #define ERRNO_RESTORE errno = olderr;
  141. #endif
  142. #else
  143. #define ERRNO_SAVE
  144. #define ERRNO_RESTORE
  145. #endif
  146. #endif