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