lj_dispatch.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** Instruction dispatch handling.
  3. ** Copyright (C) 2005-2014 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. #if LJ_HASJIT
  15. #define JITGOTDEF(_) _(lj_trace_exit) _(lj_trace_hot)
  16. #else
  17. #define JITGOTDEF(_)
  18. #endif
  19. #if LJ_HASFFI
  20. #define FFIGOTDEF(_) \
  21. _(lj_meta_equal_cd) _(lj_ccallback_enter) _(lj_ccallback_leave)
  22. #else
  23. #define FFIGOTDEF(_)
  24. #endif
  25. #define GOTDEF(_) \
  26. _(floor) _(ceil) _(trunc) _(log) _(log10) _(exp) _(sin) _(cos) _(tan) \
  27. _(asin) _(acos) _(atan) _(sinh) _(cosh) _(tanh) _(frexp) _(modf) _(atan2) \
  28. _(pow) _(fmod) _(ldexp) \
  29. _(lj_dispatch_call) _(lj_dispatch_ins) _(lj_err_throw) \
  30. _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \
  31. _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \
  32. _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \
  33. _(lj_meta_for) _(lj_meta_len) _(lj_meta_tget) _(lj_meta_tset) \
  34. _(lj_state_growstack) _(lj_str_fromnum) _(lj_str_fromnumber) _(lj_str_new) \
  35. _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) _(lj_tab_new) \
  36. _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \
  37. JITGOTDEF(_) FFIGOTDEF(_)
  38. enum {
  39. #define GOTENUM(name) LJ_GOT_##name,
  40. GOTDEF(GOTENUM)
  41. #undef GOTENUM
  42. LJ_GOT__MAX
  43. };
  44. #endif
  45. /* Type of hot counter. Must match the code in the assembler VM. */
  46. /* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */
  47. typedef uint16_t HotCount;
  48. /* Number of hot counter hash table entries (must be a power of two). */
  49. #define HOTCOUNT_SIZE 64
  50. #define HOTCOUNT_PCMASK ((HOTCOUNT_SIZE-1)*sizeof(HotCount))
  51. /* Hotcount decrements. */
  52. #define HOTCOUNT_LOOP 2
  53. #define HOTCOUNT_CALL 1
  54. /* This solves a circular dependency problem -- bump as needed. Sigh. */
  55. #define GG_NUM_ASMFF 62
  56. #define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF)
  57. #define GG_LEN_SDISP BC_FUNCF
  58. #define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP)
  59. /* Global state, main thread and extra fields are allocated together. */
  60. typedef struct GG_State {
  61. lua_State L; /* Main thread. */
  62. global_State g; /* Global state. */
  63. #if LJ_TARGET_MIPS
  64. ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */
  65. #endif
  66. #if LJ_HASJIT
  67. jit_State J; /* JIT state. */
  68. HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */
  69. #endif
  70. ASMFunction dispatch[GG_LEN_DISP]; /* Instruction dispatch tables. */
  71. BCIns bcff[GG_NUM_ASMFF]; /* Bytecode for ASM fast functions. */
  72. } GG_State;
  73. #define GG_OFS(field) ((int)offsetof(GG_State, field))
  74. #define G2GG(gl) ((GG_State *)((char *)(gl) - GG_OFS(g)))
  75. #define J2GG(j) ((GG_State *)((char *)(j) - GG_OFS(J)))
  76. #define L2GG(L) (G2GG(G(L)))
  77. #define J2G(J) (&J2GG(J)->g)
  78. #define G2J(gl) (&G2GG(gl)->J)
  79. #define L2J(L) (&L2GG(L)->J)
  80. #define GG_G2DISP (GG_OFS(dispatch) - GG_OFS(g))
  81. #define GG_DISP2G (GG_OFS(g) - GG_OFS(dispatch))
  82. #define GG_DISP2J (GG_OFS(J) - GG_OFS(dispatch))
  83. #define GG_DISP2HOT (GG_OFS(hotcount) - GG_OFS(dispatch))
  84. #define GG_DISP2STATIC (GG_LEN_DDISP*(int)sizeof(ASMFunction))
  85. #define hotcount_get(gg, pc) \
  86. (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)]
  87. #define hotcount_set(gg, pc, val) \
  88. (hotcount_get((gg), (pc)) = (HotCount)(val))
  89. /* Dispatch table management. */
  90. LJ_FUNC void lj_dispatch_init(GG_State *GG);
  91. #if LJ_HASJIT
  92. LJ_FUNC void lj_dispatch_init_hotcount(global_State *g);
  93. #endif
  94. LJ_FUNC void lj_dispatch_update(global_State *g);
  95. /* Instruction dispatch callback for hooks or when recording. */
  96. LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc);
  97. LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc);
  98. LJ_FUNCA void LJ_FASTCALL lj_dispatch_return(lua_State *L, const BCIns *pc);
  99. #if LJ_HASFFI && !defined(_BUILDVM_H)
  100. /* Save/restore errno and GetLastError() around hooks, exits and recording. */
  101. #include <errno.h>
  102. #if LJ_TARGET_WINDOWS
  103. #define WIN32_LEAN_AND_MEAN
  104. #include <windows.h>
  105. #define ERRNO_SAVE int olderr = errno; DWORD oldwerr = GetLastError();
  106. #define ERRNO_RESTORE errno = olderr; SetLastError(oldwerr);
  107. #else
  108. #define ERRNO_SAVE int olderr = errno;
  109. #define ERRNO_RESTORE errno = olderr;
  110. #endif
  111. #else
  112. #define ERRNO_SAVE
  113. #define ERRNO_RESTORE
  114. #endif
  115. #endif