Coverage Report

Created: 2025-08-03 06:43

/src/tarantool/third_party/luajit/src/lj_dispatch.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** Instruction dispatch handling.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
*/
5
6
#define lj_dispatch_c
7
#define LUA_CORE
8
9
#include "lj_obj.h"
10
#include "lj_err.h"
11
#include "lj_buf.h"
12
#include "lj_func.h"
13
#include "lj_str.h"
14
#include "lj_tab.h"
15
#include "lj_meta.h"
16
#include "lj_debug.h"
17
#include "lj_state.h"
18
#include "lj_frame.h"
19
#include "lj_bc.h"
20
#include "lj_ff.h"
21
#include "lj_strfmt.h"
22
#if LJ_HASJIT
23
#include "lj_jit.h"
24
#endif
25
#if LJ_HASFFI
26
#include "lj_ccallback.h"
27
#endif
28
#include "lj_trace.h"
29
#include "lj_dispatch.h"
30
#if LJ_HASPROFILE
31
#include "lj_profile.h"
32
#endif
33
#include "lj_vm.h"
34
#include "luajit.h"
35
36
/* Bump GG_NUM_ASMFF in lj_dispatch.h as needed. Ugly. */
37
LJ_STATIC_ASSERT(GG_NUM_ASMFF == FF_NUM_ASMFUNC);
38
39
/* -- Dispatch table management ------------------------------------------- */
40
41
#if LJ_TARGET_MIPS
42
#include <math.h>
43
LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L,
44
                lua_State *co);
45
#if !LJ_HASJIT
46
#define lj_dispatch_stitch  lj_dispatch_ins
47
#endif
48
#if !LJ_HASPROFILE
49
#define lj_dispatch_profile lj_dispatch_ins
50
#endif
51
52
#define GOTFUNC(name) (ASMFunction)name,
53
static const ASMFunction dispatch_got[] = {
54
  GOTDEF(GOTFUNC)
55
};
56
#undef GOTFUNC
57
#endif
58
59
/* Initialize instruction dispatch table and hot counters. */
60
void lj_dispatch_init(GG_State *GG)
61
58.5k
{
62
58.5k
  uint32_t i;
63
58.5k
  ASMFunction *disp = GG->dispatch;
64
5.27M
  for (i = 0; i < GG_LEN_SDISP; i++)
65
5.21M
    disp[GG_LEN_DDISP+i] = disp[i] = makeasmfunc(lj_bc_ofs[i]);
66
3.86M
  for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++)
67
3.80M
    disp[i] = makeasmfunc(lj_bc_ofs[i]);
68
  /* The JIT engine is off by default. luaopen_jit() turns it on. */
69
58.5k
  disp[BC_FORL] = disp[BC_IFORL];
70
58.5k
  disp[BC_ITERL] = disp[BC_IITERL];
71
58.5k
  disp[BC_LOOP] = disp[BC_ILOOP];
72
58.5k
  disp[BC_FUNCF] = disp[BC_IFUNCF];
73
58.5k
  disp[BC_FUNCV] = disp[BC_IFUNCV];
74
58.5k
  GG->g.bc_cfunc_ext = GG->g.bc_cfunc_int = BCINS_AD(BC_FUNCC, LUA_MINSTACK, 0);
75
3.39M
  for (i = 0; i < GG_NUM_ASMFF; i++)
76
3.34M
    GG->bcff[i] = BCINS_AD(BC__MAX+i, 0, 0);
77
#if LJ_TARGET_MIPS
78
  memcpy(GG->got, dispatch_got, LJ_GOT__MAX*sizeof(ASMFunction *));
79
#endif
80
58.5k
}
81
82
#if LJ_HASJIT
83
/* Initialize hotcount table. */
84
void lj_dispatch_init_hotcount(global_State *g)
85
29.1k
{
86
29.1k
  int32_t hotloop = G2J(g)->param[JIT_P_hotloop];
87
29.1k
  HotCount start = (HotCount)(hotloop*HOTCOUNT_LOOP - 1);
88
29.1k
  HotCount *hotcount = G2GG(g)->hotcount;
89
29.1k
  uint32_t i;
90
1.89M
  for (i = 0; i < HOTCOUNT_SIZE; i++)
91
1.86M
    hotcount[i] = start;
92
29.1k
}
93
#endif
94
95
/* Internal dispatch mode bits. */
96
7.33M
#define DISPMODE_CALL 0x01  /* Override call dispatch. */
97
999k
#define DISPMODE_RET  0x02  /* Override return dispatch. */
98
5.54M
#define DISPMODE_INS  0x04  /* Override instruction dispatch. */
99
10.6M
#define DISPMODE_JIT  0x10  /* JIT compiler on. */
100
6.39M
#define DISPMODE_REC  0x20  /* Recording active. */
101
3.25M
#define DISPMODE_PROF 0x40  /* Profiling active. */
102
103
/* Update dispatch table depending on various flags. */
104
void lj_dispatch_update(global_State *g)
105
2.24M
{
106
2.24M
  uint8_t oldmode = g->dispatchmode;
107
2.24M
  uint8_t mode = 0;
108
2.24M
#if LJ_HASJIT
109
2.24M
  mode |= (G2J(g)->flags & JIT_F_ON) ? DISPMODE_JIT : 0;
110
2.24M
  mode |= G2J(g)->state != LJ_TRACE_IDLE ?
111
1.22M
      (DISPMODE_REC|DISPMODE_INS|DISPMODE_CALL) : 0;
112
2.24M
#endif
113
2.24M
#if LJ_HASPROFILE
114
2.24M
  mode |= (g->hookmask & HOOK_PROFILE) ? (DISPMODE_PROF|DISPMODE_INS) : 0;
115
2.24M
#endif
116
2.24M
  mode |= (g->hookmask & (LUA_MASKLINE|LUA_MASKCOUNT)) ? DISPMODE_INS : 0;
117
2.24M
  mode |= (g->hookmask & LUA_MASKCALL) ? DISPMODE_CALL : 0;
118
2.24M
  mode |= (g->hookmask & LUA_MASKRET) ? DISPMODE_RET : 0;
119
2.24M
  if (oldmode != mode) {  /* Mode changed? */
120
2.12M
    ASMFunction *disp = G2GG(g)->dispatch;
121
2.12M
    ASMFunction f_forl, f_iterl, f_loop, f_funcf, f_funcv;
122
2.12M
    g->dispatchmode = mode;
123
124
    /* Hotcount if JIT is on, but not while recording. */
125
2.12M
    if ((mode & (DISPMODE_JIT|DISPMODE_REC)) == DISPMODE_JIT) {
126
1.07M
      f_forl = makeasmfunc(lj_bc_ofs[BC_FORL]);
127
1.07M
      f_iterl = makeasmfunc(lj_bc_ofs[BC_ITERL]);
128
1.07M
      f_loop = makeasmfunc(lj_bc_ofs[BC_LOOP]);
129
1.07M
      f_funcf = makeasmfunc(lj_bc_ofs[BC_FUNCF]);
130
1.07M
      f_funcv = makeasmfunc(lj_bc_ofs[BC_FUNCV]);
131
1.07M
    } else {  /* Otherwise use the non-hotcounting instructions. */
132
1.04M
      f_forl = disp[GG_LEN_DDISP+BC_IFORL];
133
1.04M
      f_iterl = disp[GG_LEN_DDISP+BC_IITERL];
134
1.04M
      f_loop = disp[GG_LEN_DDISP+BC_ILOOP];
135
1.04M
      f_funcf = makeasmfunc(lj_bc_ofs[BC_IFUNCF]);
136
1.04M
      f_funcv = makeasmfunc(lj_bc_ofs[BC_IFUNCV]);
137
1.04M
    }
138
    /* Init static counting instruction dispatch first (may be copied below). */
139
2.12M
    disp[GG_LEN_DDISP+BC_FORL] = f_forl;
140
2.12M
    disp[GG_LEN_DDISP+BC_ITERL] = f_iterl;
141
2.12M
    disp[GG_LEN_DDISP+BC_LOOP] = f_loop;
142
143
    /* Set dynamic instruction dispatch. */
144
2.12M
    if ((oldmode ^ mode) & (DISPMODE_PROF|DISPMODE_REC|DISPMODE_INS)) {
145
      /* Need to update the whole table. */
146
2.06M
      if (!(mode & DISPMODE_INS)) {  /* No ins dispatch? */
147
  /* Copy static dispatch table to dynamic dispatch table. */
148
930k
  memcpy(&disp[0], &disp[GG_LEN_DDISP], GG_LEN_SDISP*sizeof(ASMFunction));
149
  /* Overwrite with dynamic return dispatch. */
150
930k
  if ((mode & DISPMODE_RET)) {
151
31
    disp[BC_RETM] = lj_vm_rethook;
152
31
    disp[BC_RET] = lj_vm_rethook;
153
31
    disp[BC_RET0] = lj_vm_rethook;
154
31
    disp[BC_RET1] = lj_vm_rethook;
155
31
  }
156
1.13M
      } else {
157
  /* The recording dispatch also checks for hooks. */
158
1.13M
  ASMFunction f = (mode & DISPMODE_PROF) ? lj_vm_profhook :
159
1.13M
      (mode & DISPMODE_REC) ? lj_vm_record : lj_vm_inshook;
160
1.13M
  uint32_t i;
161
102M
  for (i = 0; i < GG_LEN_SDISP; i++)
162
101M
    disp[i] = f;
163
1.13M
      }
164
2.06M
    } else if (!(mode & DISPMODE_INS)) {
165
      /* Otherwise set dynamic counting ins. */
166
52.4k
      disp[BC_FORL] = f_forl;
167
52.4k
      disp[BC_ITERL] = f_iterl;
168
52.4k
      disp[BC_LOOP] = f_loop;
169
      /* Set dynamic return dispatch. */
170
52.4k
      if ((mode & DISPMODE_RET)) {
171
343
  disp[BC_RETM] = lj_vm_rethook;
172
343
  disp[BC_RET] = lj_vm_rethook;
173
343
  disp[BC_RET0] = lj_vm_rethook;
174
343
  disp[BC_RET1] = lj_vm_rethook;
175
52.0k
      } else {
176
52.0k
  disp[BC_RETM] = disp[GG_LEN_DDISP+BC_RETM];
177
52.0k
  disp[BC_RET] = disp[GG_LEN_DDISP+BC_RET];
178
52.0k
  disp[BC_RET0] = disp[GG_LEN_DDISP+BC_RET0];
179
52.0k
  disp[BC_RET1] = disp[GG_LEN_DDISP+BC_RET1];
180
52.0k
      }
181
52.4k
    }
182
183
    /* Set dynamic call dispatch. */
184
2.12M
    if ((oldmode ^ mode) & DISPMODE_CALL) {  /* Update the whole table? */
185
2.02M
      uint32_t i;
186
2.02M
      if ((mode & DISPMODE_CALL) == 0) {  /* No call hooks? */
187
66.8M
  for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++)
188
65.8M
    disp[i] = makeasmfunc(lj_bc_ofs[i]);
189
1.01M
      } else {
190
66.8M
  for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++)
191
65.8M
    disp[i] = lj_vm_callhook;
192
1.01M
      }
193
2.02M
    }
194
2.12M
    if (!(mode & DISPMODE_CALL)) {  /* Overwrite dynamic counting ins. */
195
1.10M
      disp[BC_FUNCF] = f_funcf;
196
1.10M
      disp[BC_FUNCV] = f_funcv;
197
1.10M
    }
198
199
2.12M
#if LJ_HASJIT
200
    /* Reset hotcounts for JIT off to on transition. */
201
2.12M
    if ((mode & DISPMODE_JIT) && !(oldmode & DISPMODE_JIT))
202
25.9k
      lj_dispatch_init_hotcount(g);
203
2.12M
#endif
204
2.12M
  }
205
2.24M
}
206
207
/* -- JIT mode setting ---------------------------------------------------- */
208
209
#if LJ_HASJIT
210
/* Set JIT mode for a single prototype. */
211
static void setptmode(global_State *g, GCproto *pt, int mode)
212
85.8k
{
213
85.8k
  if ((mode & LUAJIT_MODE_ON)) {  /* (Re-)enable JIT compilation. */
214
0
    pt->flags &= ~PROTO_NOJIT;
215
0
    lj_trace_reenableproto(pt);  /* Unpatch all ILOOP etc. bytecodes. */
216
85.8k
  } else {  /* Flush and/or disable JIT compilation. */
217
85.8k
    if (!(mode & LUAJIT_MODE_FLUSH))
218
85.8k
      pt->flags |= PROTO_NOJIT;
219
85.8k
    lj_trace_flushproto(g, pt);  /* Flush all traces of prototype. */
220
85.8k
  }
221
85.8k
}
222
223
/* Recursively set the JIT mode for all children of a prototype. */
224
static void setptmode_all(global_State *g, GCproto *pt, int mode)
225
0
{
226
0
  ptrdiff_t i;
227
0
  if (!(pt->flags & PROTO_CHILD)) return;
228
0
  for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) {
229
0
    GCobj *o = proto_kgc(pt, i);
230
0
    if (o->gch.gct == ~LJ_TPROTO) {
231
0
      setptmode(g, gco2pt(o), mode);
232
0
      setptmode_all(g, gco2pt(o), mode);
233
0
    }
234
0
  }
235
0
}
236
#endif
237
238
/* Public API function: control the JIT engine. */
239
int luaJIT_setmode(lua_State *L, int idx, int mode)
240
365k
{
241
365k
  global_State *g = G(L);
242
365k
  int mm = mode & LUAJIT_MODE_MASK;
243
  /* Forbid JIT state change while running the trace */
244
365k
  if (tvref(g->jit_base)) {
245
0
    setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITMODE));
246
0
    if (g->panic) g->panic(L);
247
0
    exit(EXIT_FAILURE);
248
0
  }
249
365k
  lj_trace_abort(g);  /* Abort recording on any state change. */
250
  /* Avoid pulling the rug from under our own feet. */
251
365k
  if ((g->hookmask & HOOK_GC))
252
0
    lj_err_caller(L, LJ_ERR_NOGCMM);
253
365k
  switch (mm) {
254
0
#if LJ_HASJIT
255
265k
  case LUAJIT_MODE_ENGINE:
256
265k
    if ((mode & LUAJIT_MODE_FLUSH)) {
257
255k
      lj_trace_flushall(L);
258
255k
    } else {
259
10.3k
      if (!(mode & LUAJIT_MODE_ON))
260
10.3k
  G2J(g)->flags &= ~(uint32_t)JIT_F_ON;
261
0
      else
262
0
  G2J(g)->flags |= (uint32_t)JIT_F_ON;
263
10.3k
      lj_dispatch_update(g);
264
10.3k
    }
265
265k
    break;
266
85.8k
  case LUAJIT_MODE_FUNC:
267
85.8k
  case LUAJIT_MODE_ALLFUNC:
268
85.8k
  case LUAJIT_MODE_ALLSUBFUNC: {
269
85.8k
    cTValue *tv = idx == 0 ? frame_prev(L->base-1)-LJ_FR2 :
270
85.8k
      idx > 0 ? L->base + (idx-1) : L->top + idx;
271
85.8k
    GCproto *pt;
272
85.8k
    if ((idx == 0 || tvisfunc(tv)) && isluafunc(&gcval(tv)->fn))
273
85.8k
      pt = funcproto(&gcval(tv)->fn);  /* Cannot use funcV() for frame slot. */
274
1
    else if (tvisproto(tv))
275
0
      pt = protoV(tv);
276
1
    else
277
1
      return 0;  /* Failed. */
278
85.8k
    if (mm != LUAJIT_MODE_ALLSUBFUNC)
279
85.8k
      setptmode(g, pt, mode);
280
85.8k
    if (mm != LUAJIT_MODE_FUNC)
281
0
      setptmode_all(g, pt, mode);
282
85.8k
    break;
283
85.8k
    }
284
14.6k
  case LUAJIT_MODE_TRACE:
285
14.6k
    if (!(mode & LUAJIT_MODE_FLUSH))
286
0
      return 0;  /* Failed. */
287
14.6k
    lj_trace_flush(G2J(g), idx);
288
14.6k
    break;
289
#else
290
  case LUAJIT_MODE_ENGINE:
291
  case LUAJIT_MODE_FUNC:
292
  case LUAJIT_MODE_ALLFUNC:
293
  case LUAJIT_MODE_ALLSUBFUNC:
294
    UNUSED(idx);
295
    if ((mode & LUAJIT_MODE_ON))
296
      return 0;  /* Failed. */
297
    break;
298
#endif
299
0
  case LUAJIT_MODE_WRAPCFUNC:
300
0
    if ((mode & LUAJIT_MODE_ON)) {
301
0
      if (idx != 0) {
302
0
  cTValue *tv = idx > 0 ? L->base + (idx-1) : L->top + idx;
303
0
  if (tvislightud(tv))
304
0
    g->wrapf = (lua_CFunction)lightudV(g, tv);
305
0
  else
306
0
    return 0;  /* Failed. */
307
0
      } else {
308
0
  return 0;  /* Failed. */
309
0
      }
310
0
      g->bc_cfunc_ext = BCINS_AD(BC_FUNCCW, 0, 0);
311
0
    } else {
312
0
      g->bc_cfunc_ext = BCINS_AD(BC_FUNCC, 0, 0);
313
0
    }
314
0
    break;
315
0
  default:
316
0
    return 0;  /* Failed. */
317
365k
  }
318
365k
  return 1;  /* OK. */
319
365k
}
320
321
/* Enforce (dynamic) linker error for version mismatches. See luajit.c. */
322
LUA_API void LUAJIT_VERSION_SYM(void)
323
0
{
324
0
}
325
326
/* -- Hooks --------------------------------------------------------------- */
327
328
/* This function can be called asynchronously (e.g. during a signal). */
329
LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count)
330
117k
{
331
117k
  global_State *g = G(L);
332
117k
  mask &= HOOK_EVENTMASK;
333
117k
  if (func == NULL || mask == 0) { mask = 0; func = NULL; }  /* Consistency. */
334
117k
  g->hookf = func;
335
117k
  g->hookcount = g->hookcstart = (int32_t)count;
336
117k
  g->hookmask = (uint8_t)((g->hookmask & ~HOOK_EVENTMASK) | mask);
337
117k
  lj_trace_abort(g);  /* Abort recording on any hook change. */
338
117k
  lj_dispatch_update(g);
339
117k
  return 1;
340
117k
}
341
342
LUA_API lua_Hook lua_gethook(lua_State *L)
343
9.11k
{
344
9.11k
  return G(L)->hookf;
345
9.11k
}
346
347
LUA_API int lua_gethookmask(lua_State *L)
348
9.11k
{
349
9.11k
  return G(L)->hookmask & HOOK_EVENTMASK;
350
9.11k
}
351
352
LUA_API int lua_gethookcount(lua_State *L)
353
9.11k
{
354
9.11k
  return (int)G(L)->hookcstart;
355
9.11k
}
356
357
/* Call a hook. */
358
static void callhook(lua_State *L, int event, BCLine line)
359
9.32M
{
360
9.32M
  global_State *g = G(L);
361
9.32M
  lua_Hook hookf = g->hookf;
362
9.32M
  if (hookf && !hook_active(g)) {
363
6.03M
    lua_Debug ar;
364
6.03M
    lj_trace_abort(g);  /* Abort recording on any hook call. */
365
6.03M
    ar.event = event;
366
6.03M
    ar.currentline = line;
367
    /* Top frame, nextframe = NULL. */
368
6.03M
    ar.i_ci = (int)((L->base-1) - tvref(L->stack));
369
6.03M
    lj_state_checkstack(L, 1+LUA_MINSTACK);
370
#if LJ_HASPROFILE && !LJ_PROFILE_SIGPROF
371
    lj_profile_hook_enter(g);
372
#else
373
6.03M
    hook_enter(g);
374
6.03M
#endif
375
6.03M
    hookf(L, &ar);
376
6.03M
    lj_assertG(hook_active(g), "active hook flag removed");
377
6.03M
    setgcref(g->cur_L, obj2gco(L));
378
#if LJ_HASPROFILE && !LJ_PROFILE_SIGPROF
379
    lj_profile_hook_leave(g);
380
#else
381
6.03M
    hook_leave(g);
382
6.03M
#endif
383
6.03M
  }
384
9.32M
}
385
386
/* -- Dispatch callbacks -------------------------------------------------- */
387
388
/* Calculate number of used stack slots in the current frame. */
389
static BCReg cur_topslot(GCproto *pt, const BCIns *pc, uint32_t nres)
390
30.8M
{
391
30.8M
  BCIns ins = pc[-1];
392
30.8M
  if (bc_op(ins) == BC_UCLO)
393
4.33k
    ins = pc[bc_j(ins)];
394
30.8M
  switch (bc_op(ins)) {
395
301k
  case BC_CALLM: case BC_CALLMT: return bc_a(ins) + bc_c(ins) + nres-1+1+LJ_FR2;
396
5.70k
  case BC_RETM: return bc_a(ins) + bc_d(ins) + nres-1;
397
21.3k
  case BC_TSETM: return bc_a(ins) + nres-1;
398
30.5M
  default: return pt->framesize;
399
30.8M
  }
400
30.8M
}
401
402
/* Instruction dispatch. Used by instr/line/return hooks or when recording. */
403
void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc)
404
30.8M
{
405
30.8M
  ERRNO_SAVE
406
30.8M
  GCfunc *fn = curr_func(L);
407
30.8M
  GCproto *pt = funcproto(fn);
408
30.8M
  void *cf = cframe_raw(L->cframe);
409
30.8M
  const BCIns *oldpc = cframe_pc(cf);
410
30.8M
  global_State *g = G(L);
411
30.8M
  BCReg slots;
412
30.8M
  setcframe_pc(cf, pc);
413
30.8M
  slots = cur_topslot(pt, pc, cframe_multres_n(cf));
414
30.8M
  L->top = L->base + slots;  /* Fix top. */
415
30.8M
#if LJ_HASJIT
416
30.8M
  {
417
30.8M
    jit_State *J = G2J(g);
418
30.8M
    if (J->state != LJ_TRACE_IDLE) {
419
21.6M
#ifdef LUA_USE_ASSERT
420
21.6M
      ptrdiff_t delta = L->top - L->base;
421
21.6M
#endif
422
21.6M
      J->L = L;
423
21.6M
      lj_trace_ins(J, pc-1);  /* The interpreter bytecode PC is offset by 1. */
424
21.6M
      lj_assertG(L->top - L->base == delta,
425
21.6M
     "unbalanced stack after tracing of instruction");
426
21.6M
    }
427
30.8M
  }
428
30.8M
#endif
429
30.8M
  if ((g->hookmask & LUA_MASKCOUNT) && g->hookcount == 0) {
430
1.71M
    g->hookcount = g->hookcstart;
431
1.71M
    callhook(L, LUA_HOOKCOUNT, -1);
432
1.71M
    L->top = L->base + slots;  /* Fix top again. */
433
1.71M
  }
434
30.8M
  if ((g->hookmask & LUA_MASKLINE)) {
435
7.87M
    BCPos npc = proto_bcpos(pt, pc) - 1;
436
7.87M
    BCPos opc = proto_bcpos(pt, oldpc) - 1;
437
7.87M
    BCLine line = lj_debug_line(pt, npc);
438
7.87M
    if (pc <= oldpc || opc >= pt->sizebc || line != lj_debug_line(pt, opc)) {
439
3.16M
      callhook(L, LUA_HOOKLINE, line);
440
3.16M
      L->top = L->base + slots;  /* Fix top again. */
441
3.16M
    }
442
7.87M
  }
443
30.8M
  if ((g->hookmask & LUA_MASKRET) && bc_isret(bc_op(pc[-1])))
444
60.7k
    callhook(L, LUA_HOOKRET, -1);
445
30.8M
  ERRNO_RESTORE
446
30.8M
}
447
448
/* Initialize call. Ensure stack space and return # of missing parameters. */
449
static int call_init(lua_State *L, GCfunc *fn)
450
8.62M
{
451
8.62M
  if (isluafunc(fn)) {
452
5.82M
    GCproto *pt = funcproto(fn);
453
0
    int numparams = pt->numparams;
454
5.82M
    int gotparams = (int)(L->top - L->base);
455
5.82M
    int need = pt->framesize;
456
5.82M
    if ((pt->flags & PROTO_VARARG)) need += 1+gotparams;
457
5.82M
    lj_state_checkstack(L, (MSize)need);
458
5.82M
    numparams -= gotparams;
459
5.82M
    return numparams >= 0 ? numparams : 0;
460
5.82M
  } else {
461
2.80M
    lj_state_checkstack(L, LUA_MINSTACK);
462
2.80M
    return 0;
463
2.80M
  }
464
8.62M
}
465
466
/* Call dispatch. Used by call hooks, hot calls or when recording. */
467
ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns *pc)
468
8.62M
{
469
8.62M
  ERRNO_SAVE
470
8.62M
  GCfunc *fn = curr_func(L);
471
8.62M
  BCOp op;
472
8.62M
  global_State *g = G(L);
473
8.62M
#if LJ_HASJIT
474
8.62M
  jit_State *J = G2J(g);
475
8.62M
#endif
476
8.62M
  int missing = call_init(L, fn);
477
8.62M
#if LJ_HASJIT
478
8.62M
  J->L = L;
479
8.62M
  if ((uintptr_t)pc & 1) {  /* Marker for hot call. */
480
32.8k
#ifdef LUA_USE_ASSERT
481
32.8k
    ptrdiff_t delta = L->top - L->base;
482
32.8k
#endif
483
32.8k
    pc = (const BCIns *)((uintptr_t)pc & ~(uintptr_t)1);
484
32.8k
    lj_trace_hot(J, pc);
485
32.8k
    lj_assertG(L->top - L->base == delta,
486
32.8k
         "unbalanced stack after hot call");
487
32.8k
    goto out;
488
8.59M
  } else if (J->state != LJ_TRACE_IDLE &&
489
8.59M
       !(g->hookmask & (HOOK_GC|HOOK_VMEVENT))) {
490
4.20M
#ifdef LUA_USE_ASSERT
491
4.20M
    ptrdiff_t delta = L->top - L->base;
492
4.20M
#endif
493
    /* Record the FUNC* bytecodes, too. */
494
4.20M
    lj_trace_ins(J, pc-1);  /* The interpreter bytecode PC is offset by 1. */
495
4.20M
    lj_assertG(L->top - L->base == delta,
496
4.20M
         "unbalanced stack after hot instruction");
497
4.20M
  }
498
8.59M
#endif
499
8.59M
  if ((g->hookmask & LUA_MASKCALL)) {
500
4.38M
    int i;
501
4.39M
    for (i = 0; i < missing; i++)  /* Add missing parameters. */
502
5.00k
      setnilV(L->top++);
503
4.38M
    callhook(L, LUA_HOOKCALL, -1);
504
    /* Preserve modifications of missing parameters by lua_setlocal(). */
505
4.39M
    while (missing-- > 0 && tvisnil(L->top - 1))
506
5.00k
      L->top--;
507
4.38M
  }
508
8.59M
#if LJ_HASJIT
509
8.62M
out:
510
8.62M
#endif
511
8.62M
  op = bc_op(pc[-1]);  /* Get FUNC* op. */
512
8.62M
#if LJ_HASJIT
513
  /* Use the non-hotcounting variants if JIT is off or while recording. */
514
8.62M
  if ((!(J->flags & JIT_F_ON) || J->state != LJ_TRACE_IDLE) &&
515
8.62M
      (op == BC_FUNCF || op == BC_FUNCV))
516
2.26M
    op = (BCOp)((int)op+(int)BC_IFUNCF-(int)BC_FUNCF);
517
8.62M
#endif
518
8.62M
  ERRNO_RESTORE
519
8.62M
  return makeasmfunc(lj_bc_ofs[op]);  /* Return static dispatch target. */
520
8.59M
}
521
522
#if LJ_HASJIT
523
/* Stitch a new trace. */
524
void LJ_FASTCALL lj_dispatch_stitch(jit_State *J, const BCIns *pc)
525
15.1k
{
526
15.1k
  ERRNO_SAVE
527
15.1k
  lua_State *L = J->L;
528
15.1k
  void *cf = cframe_raw(L->cframe);
529
15.1k
  const BCIns *oldpc = cframe_pc(cf);
530
15.1k
  setcframe_pc(cf, pc);
531
  /* Before dispatch, have to bias PC by 1. */
532
15.1k
  L->top = L->base + cur_topslot(curr_proto(L), pc+1, cframe_multres_n(cf));
533
0
  lj_trace_stitch(J, pc-1);  /* Point to the CALL instruction. */
534
15.1k
  setcframe_pc(cf, oldpc);
535
15.1k
  ERRNO_RESTORE
536
15.1k
}
537
#endif
538
539
#if LJ_HASPROFILE
540
/* Profile dispatch. */
541
void LJ_FASTCALL lj_dispatch_profile(lua_State *L, const BCIns *pc)
542
0
{
543
0
  ERRNO_SAVE
544
0
  GCfunc *fn = curr_func(L);
545
0
  GCproto *pt = funcproto(fn);
546
0
  void *cf = cframe_raw(L->cframe);
547
0
  const BCIns *oldpc = cframe_pc(cf);
548
0
  global_State *g;
549
0
  setcframe_pc(cf, pc);
550
0
  L->top = L->base + cur_topslot(pt, pc, cframe_multres_n(cf));
551
0
  lj_profile_interpreter(L);
552
0
  setcframe_pc(cf, oldpc);
553
0
  g = G(L);
554
0
  setgcref(g->cur_L, obj2gco(L));
555
0
  setvmstate(g, INTERP);
556
0
  ERRNO_RESTORE
557
0
}
558
#endif
559