master
ブランチ master
ShaderPlayground / deps / luajit / include / lj_dispatch.h
4.5 KB、139行 生 Blame 履歴
1/*
2** Instruction dispatch handling.
3** Copyright (C) 2005-2026 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#ifndef _LJ_DISPATCH_H
7#define _LJ_DISPATCH_H
8
9#include "lj_obj.h"
10#include "lj_bc.h"
11#if LJ_HASJIT
12#include "lj_jit.h"
13#endif
14
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_err_throw) _(lj_err_run) \
33 _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \
34 _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \
35 _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \
36 _(lj_meta_for) _(lj_meta_len) _(lj_meta_tget) _(lj_meta_tset) \
37 _(lj_state_growstack) _(lj_str_fromnum) _(lj_str_fromnumber) _(lj_str_new) \
38 _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) _(lj_tab_new) \
39 _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \
40 JITGOTDEF(_) FFIGOTDEF(_)
41
42enum {
43#define GOTENUM(name) LJ_GOT_##name,
44GOTDEF(GOTENUM)
45#undef GOTENUM
46 LJ_GOT__MAX
47};
48#endif
49
50/* Type of hot counter. Must match the code in the assembler VM. */
51/* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */
52typedef uint16_t HotCount;
53
54/* Number of hot counter hash table entries (must be a power of two). */
55#define HOTCOUNT_SIZE 64
56#define HOTCOUNT_PCMASK ((HOTCOUNT_SIZE-1)*sizeof(HotCount))
57
58/* Hotcount decrements. */
59#define HOTCOUNT_LOOP 2
60#define HOTCOUNT_CALL 1
61
62/* This solves a circular dependency problem -- bump as needed. Sigh. */
63#define GG_NUM_ASMFF 62
64
65#define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF)
66#define GG_LEN_SDISP BC_FUNCF
67#define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP)
68
69/* Global state, main thread and extra fields are allocated together. */
70typedef struct GG_State {
71 lua_State L; /* Main thread. */
72 global_State g; /* Global state. */
73#if LJ_TARGET_ARM
74 /* Make g reachable via K12 encoded DISPATCH-relative addressing. */
75 uint8_t align1[(16-sizeof(global_State))&15];
76#endif
77#if LJ_TARGET_MIPS
78 ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */
79#endif
80#if LJ_HASJIT
81 jit_State J; /* JIT state. */
82 HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */
83#if LJ_TARGET_ARM
84 /* Ditto for J. */
85 uint8_t align2[(16-sizeof(jit_State)-sizeof(HotCount)*HOTCOUNT_SIZE)&15];
86#endif
87#endif
88 ASMFunction dispatch[GG_LEN_DISP]; /* Instruction dispatch tables. */
89 BCIns bcff[GG_NUM_ASMFF]; /* Bytecode for ASM fast functions. */
90} GG_State;
91
92#define GG_OFS(field) ((int)offsetof(GG_State, field))
93#define G2GG(gl) ((GG_State *)((char *)(gl) - GG_OFS(g)))
94#define J2GG(j) ((GG_State *)((char *)(j) - GG_OFS(J)))
95#define L2GG(L) (G2GG(G(L)))
96#define J2G(J) (&J2GG(J)->g)
97#define G2J(gl) (&G2GG(gl)->J)
98#define L2J(L) (&L2GG(L)->J)
99#define GG_G2DISP (GG_OFS(dispatch) - GG_OFS(g))
100#define GG_DISP2G (GG_OFS(g) - GG_OFS(dispatch))
101#define GG_DISP2J (GG_OFS(J) - GG_OFS(dispatch))
102#define GG_DISP2HOT (GG_OFS(hotcount) - GG_OFS(dispatch))
103#define GG_DISP2STATIC (GG_LEN_DDISP*(int)sizeof(ASMFunction))
104
105#define hotcount_get(gg, pc) \
106 (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)]
107#define hotcount_set(gg, pc, val) \
108 (hotcount_get((gg), (pc)) = (HotCount)(val))
109
110/* Dispatch table management. */
111LJ_FUNC void lj_dispatch_init(GG_State *GG);
112#if LJ_HASJIT
113LJ_FUNC void lj_dispatch_init_hotcount(global_State *g);
114#endif
115LJ_FUNC void lj_dispatch_update(global_State *g);
116
117/* Instruction dispatch callback for hooks or when recording. */
118LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc);
119LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc);
120LJ_FUNCA void LJ_FASTCALL lj_dispatch_return(lua_State *L, const BCIns *pc);
121
122#if LJ_HASFFI && !defined(_BUILDVM_H)
123/* Save/restore errno and GetLastError() around hooks, exits and recording. */
124#include <errno.h>
125#if LJ_TARGET_WINDOWS
126#define WIN32_LEAN_AND_MEAN
127#include <windows.h>
128#define ERRNO_SAVE int olderr = errno; DWORD oldwerr = GetLastError();
129#define ERRNO_RESTORE errno = olderr; SetLastError(oldwerr);
130#else
131#define ERRNO_SAVE int olderr = errno;
132#define ERRNO_RESTORE errno = olderr;
133#endif
134#else
135#define ERRNO_SAVE
136#define ERRNO_RESTORE
137#endif
138
139#endif