Coverage Report

Created: 2025-08-09 06:54

/src/testdir/build/lua-master/source/lvm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: lvm.c $
3
** Lua virtual machine
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lvm_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
#include <float.h>
13
#include <limits.h>
14
#include <math.h>
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <string.h>
18
19
#include "lua.h"
20
21
#include "lapi.h"
22
#include "ldebug.h"
23
#include "ldo.h"
24
#include "lfunc.h"
25
#include "lgc.h"
26
#include "lobject.h"
27
#include "lopcodes.h"
28
#include "lstate.h"
29
#include "lstring.h"
30
#include "ltable.h"
31
#include "ltm.h"
32
#include "lvm.h"
33
34
35
/*
36
** By default, use jump tables in the main interpreter loop on gcc
37
** and compatible compilers.
38
*/
39
#if !defined(LUA_USE_JUMPTABLE)
40
#if defined(__GNUC__)
41
#define LUA_USE_JUMPTABLE 1
42
#else
43
#define LUA_USE_JUMPTABLE 0
44
#endif
45
#endif
46
47
48
49
/* limit for table tag-method chains (to avoid infinite loops) */
50
233M
#define MAXTAGLOOP  2000
51
52
53
/*
54
** 'l_intfitsf' checks whether a given integer is in the range that
55
** can be converted to a float without rounding. Used in comparisons.
56
*/
57
58
/* number of bits in the mantissa of a float */
59
16.2M
#define NBM   (l_floatatt(MANT_DIG))
60
61
/*
62
** Check whether some integers may not fit in a float, testing whether
63
** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.)
64
** (The shifts are done in parts, to avoid shifting by more than the size
65
** of an integer. In a worst case, NBM == 113 for long double and
66
** sizeof(long) == 32.)
67
*/
68
#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
69
  >> (NBM - (3 * (NBM / 4))))  >  0
70
71
/* limit for integers that fit in a float */
72
16.2M
#define MAXINTFITSF ((lua_Unsigned)1 << NBM)
73
74
/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
75
8.12M
#define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
76
77
#else  /* all integers fit in a float precisely */
78
79
#define l_intfitsf(i) 1
80
81
#endif
82
83
84
/*
85
** Try to convert a value from string to a number value.
86
** If the value is not a string or is a string not representing
87
** a valid numeral (or if coercions from strings to numbers
88
** are disabled via macro 'cvt2num'), do not modify 'result'
89
** and return 0.
90
*/
91
4.02M
static int l_strton (const TValue *obj, TValue *result) {
92
4.02M
  lua_assert(obj != result);
93
4.02M
  if (!cvt2num(obj))  /* is object not a string? */
94
2.97M
    return 0;
95
1.04M
  else {
96
1.04M
    TString *st = tsvalue(obj);
97
0
    size_t stlen;
98
1.04M
    const char *s = getlstr(st, stlen);
99
1.04M
    return (luaO_str2num(s, result) == stlen + 1);
100
1.04M
  }
101
4.02M
}
102
103
104
/*
105
** Try to convert a value to a float. The float case is already handled
106
** by the macro 'tonumber'.
107
*/
108
901k
int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
109
901k
  TValue v;
110
901k
  if (ttisinteger(obj)) {
111
379k
    *n = cast_num(ivalue(obj));
112
0
    return 1;
113
379k
  }
114
521k
  else if (l_strton(obj, &v)) {  /* string coercible to number? */
115
293k
    *n = nvalue(&v);  /* convert result of 'luaO_str2num' to a float */
116
293k
    return 1;
117
293k
  }
118
227k
  else
119
227k
    return 0;  /* conversion failed */
120
901k
}
121
122
123
/*
124
** try to convert a float to an integer, rounding according to 'mode'.
125
*/
126
149M
int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
127
149M
  lua_Number f = l_floor(n);
128
149M
  if (n != f) {  /* not an integral value? */
129
13.7M
    if (mode == F2Ieq) return 0;  /* fails if mode demands integral value */
130
72.6k
    else if (mode == F2Iceil)  /* needs ceiling? */
131
66.4k
      f += 1;  /* convert floor to ceiling (remember: n != f) */
132
13.7M
  }
133
135M
  return lua_numbertointeger(f, p);
134
149M
}
135
136
137
/*
138
** try to convert a value to an integer, rounding according to 'mode',
139
** without string coercion.
140
** ("Fast track" handled by macro 'tointegerns'.)
141
*/
142
10.8M
int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
143
10.8M
  if (ttisfloat(obj))
144
4.60M
    return luaV_flttointeger(fltvalue(obj), p, mode);
145
6.27M
  else if (ttisinteger(obj)) {
146
5.91M
    *p = ivalue(obj);
147
0
    return 1;
148
5.91M
  }
149
358k
  else
150
358k
    return 0;
151
10.8M
}
152
153
154
/*
155
** try to convert a value to an integer.
156
*/
157
3.50M
int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
158
3.50M
  TValue v;
159
3.50M
  if (l_strton(obj, &v))  /* does 'obj' point to a numerical string? */
160
370k
    obj = &v;  /* change it to point to its corresponding number */
161
3.50M
  return luaV_tointegerns(obj, p, mode);
162
3.50M
}
163
164
165
/*
166
** Try to convert a 'for' limit to an integer, preserving the semantics
167
** of the loop. Return true if the loop must not run; otherwise, '*p'
168
** gets the integer limit.
169
** (The following explanation assumes a positive step; it is valid for
170
** negative steps mutatis mutandis.)
171
** If the limit is an integer or can be converted to an integer,
172
** rounding down, that is the limit.
173
** Otherwise, check whether the limit can be converted to a float. If
174
** the float is too large, clip it to LUA_MAXINTEGER.  If the float
175
** is too negative, the loop should not run, because any initial
176
** integer value is greater than such limit; so, the function returns
177
** true to signal that. (For this latter case, no integer limit would be
178
** correct; even a limit of LUA_MININTEGER would run the loop once for
179
** an initial value equal to LUA_MININTEGER.)
180
*/
181
static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
182
2.80M
                                   lua_Integer *p, lua_Integer step) {
183
2.80M
  if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
184
    /* not coercible to in integer */
185
17.9k
    lua_Number flim;  /* try to convert to float */
186
17.9k
    if (!tonumber(lim, &flim)) /* cannot convert to float? */
187
2.33k
      luaG_forerror(L, lim, "limit");
188
    /* else 'flim' is a float out of integer bounds */
189
15.5k
    if (luai_numlt(0, flim)) {  /* if it is positive, it is too large */
190
4.34k
      if (step < 0) return 1;  /* initial value must be less than it */
191
3.27k
      *p = LUA_MAXINTEGER;  /* truncate */
192
3.27k
    }
193
11.2k
    else {  /* it is less than min integer */
194
11.2k
      if (step > 0) return 1;  /* initial value must be greater than it */
195
8.95k
      *p = LUA_MININTEGER;  /* truncate */
196
8.95k
    }
197
15.5k
  }
198
2.80M
  return (step > 0 ? init > *p : init < *p);  /* not to run? */
199
2.80M
}
200
201
202
/*
203
** Prepare a numerical for loop (opcode OP_FORPREP).
204
** Before execution, stack is as follows:
205
**   ra     : initial value
206
**   ra + 1 : limit
207
**   ra + 2 : step
208
** Return true to skip the loop. Otherwise,
209
** after preparation, stack will be as follows:
210
**   ra     : loop counter (integer loops) or limit (float loops)
211
**   ra + 1 : step
212
**   ra + 2 : control variable
213
*/
214
3.12M
static int forprep (lua_State *L, StkId ra) {
215
3.12M
  TValue *pinit = s2v(ra);
216
3.12M
  TValue *plimit = s2v(ra + 1);
217
3.12M
  TValue *pstep = s2v(ra + 2);
218
3.12M
  if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
219
2.80M
    lua_Integer init = ivalue(pinit);
220
2.80M
    lua_Integer step = ivalue(pstep);
221
0
    lua_Integer limit;
222
2.80M
    if (step == 0)
223
59
      luaG_runerror(L, "'for' step is zero");
224
2.80M
    if (forlimit(L, init, plimit, &limit, step))
225
1.31M
      return 1;  /* skip the loop */
226
1.49M
    else {  /* prepare loop counter */
227
1.49M
      lua_Unsigned count;
228
1.49M
      if (step > 0) {  /* ascending loop? */
229
1.47M
        count = l_castS2U(limit) - l_castS2U(init);
230
1.47M
        if (step != 1)  /* avoid division in the too common case */
231
6.40k
          count /= l_castS2U(step);
232
1.47M
      }
233
15.4k
      else {  /* step < 0; descending loop */
234
15.4k
        count = l_castS2U(init) - l_castS2U(limit);
235
        /* 'step+1' avoids negating 'mininteger' */
236
15.4k
        count /= l_castS2U(-(step + 1)) + 1u;
237
15.4k
      }
238
      /* use 'chgivalue' for places that for sure had integers */
239
1.49M
      chgivalue(s2v(ra), l_castU2S(count));  /* change init to count */
240
1.49M
      setivalue(s2v(ra + 1), step);  /* change limit to step */
241
1.49M
      chgivalue(s2v(ra + 2), init);  /* change step to init */
242
1.49M
    }
243
2.80M
  }
244
321k
  else {  /* try making all values floats */
245
321k
    lua_Number init; lua_Number limit; lua_Number step;
246
321k
    if (l_unlikely(!tonumber(plimit, &limit)))
247
130
      luaG_forerror(L, plimit, "limit");
248
321k
    if (l_unlikely(!tonumber(pstep, &step)))
249
67
      luaG_forerror(L, pstep, "step");
250
321k
    if (l_unlikely(!tonumber(pinit, &init)))
251
4.64k
      luaG_forerror(L, pinit, "initial value");
252
316k
    if (step == 0)
253
1.63k
      luaG_runerror(L, "'for' step is zero");
254
314k
    if (luai_numlt(0, step) ? luai_numlt(limit, init)
255
314k
                            : luai_numlt(init, limit))
256
34.1k
      return 1;  /* skip the loop */
257
280k
    else {
258
      /* make sure all values are floats */
259
280k
      setfltvalue(s2v(ra), limit);
260
280k
      setfltvalue(s2v(ra + 1), step);
261
280k
      setfltvalue(s2v(ra + 2), init);  /* control variable */
262
280k
    }
263
314k
  }
264
1.77M
  return 0;
265
3.12M
}
266
267
268
/*
269
** Execute a step of a float numerical for loop, returning
270
** true iff the loop must continue. (The integer case is
271
** written online with opcode OP_FORLOOP, for performance.)
272
*/
273
4.15M
static int floatforloop (StkId ra) {
274
4.15M
  lua_Number step = fltvalue(s2v(ra + 1));
275
4.15M
  lua_Number limit = fltvalue(s2v(ra));
276
4.15M
  lua_Number idx = fltvalue(s2v(ra + 2));  /* control variable */
277
4.15M
  idx = luai_numadd(L, idx, step);  /* increment index */
278
4.15M
  if (luai_numlt(0, step) ? luai_numle(idx, limit)
279
4.15M
                          : luai_numle(limit, idx)) {
280
3.87M
    chgfltvalue(s2v(ra + 2), idx);  /* update control variable */
281
3.87M
    return 1;  /* jump back */
282
3.87M
  }
283
280k
  else
284
280k
    return 0;  /* finish the loop */
285
4.15M
}
286
287
288
/*
289
** Finish the table access 'val = t[key]' and return the tag of the result.
290
*/
291
lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key,
292
152M
                                      StkId val, lu_byte tag) {
293
152M
  int loop;  /* counter to avoid infinite loops */
294
152M
  const TValue *tm;  /* metamethod */
295
172M
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
296
172M
    if (tag == LUA_VNOTABLE) {  /* 't' is not a table? */
297
106M
      lua_assert(!ttistable(t));
298
106M
      tm = luaT_gettmbyobj(L, t, TM_INDEX);
299
106M
      if (l_unlikely(notm(tm)))
300
61.3k
        luaG_typeerror(L, t, "index");  /* no metamethod */
301
      /* else will try the metamethod */
302
106M
    }
303
66.6M
    else {  /* 't' is a table */
304
66.6M
      tm = fasttm(L, hvalue(t)->metatable, TM_INDEX);  /* table's metamethod */
305
66.6M
      if (tm == NULL) {  /* no metamethod? */
306
66.6M
        setnilvalue(s2v(val));  /* result is nil */
307
66.6M
        return LUA_VNIL;
308
66.6M
      }
309
      /* else will try the metamethod */
310
66.6M
    }
311
106M
    if (ttisfunction(tm)) {  /* is metamethod a function? */
312
79.1M
      tag = luaT_callTMres(L, tm, t, key, val);  /* call it */
313
79.1M
      return tag;  /* return tag of the result */
314
79.1M
    }
315
26.9M
    t = tm;  /* else try to access 'tm[key]' */
316
26.9M
    luaV_fastget(t, key, s2v(val), luaH_get, tag);
317
26.9M
    if (!tagisempty(tag))
318
6.20M
      return tag;  /* done */
319
    /* else repeat (tail call 'luaV_finishget') */
320
26.9M
  }
321
0
  luaG_runerror(L, "'__index' chain too long; possible loop");
322
0
  return 0;  /* to avoid warnings */
323
152M
}
324
325
326
/*
327
** Finish a table assignment 't[key] = val'.
328
** About anchoring the table before the call to 'luaH_finishset':
329
** This call may trigger an emergency collection. When loop>0,
330
** the table being accessed is a field in some metatable. If this
331
** metatable is weak and the table is not anchored, this collection
332
** could collect that table while it is being updated.
333
*/
334
void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
335
60.9M
                      TValue *val, int hres) {
336
60.9M
  int loop;  /* counter to avoid infinite loops */
337
60.9M
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
338
60.9M
    const TValue *tm;  /* '__newindex' metamethod */
339
60.9M
    if (hres != HNOTATABLE) {  /* is 't' a table? */
340
46.9M
      Table *h = hvalue(t);  /* save 't' table */
341
23.4M
      tm = fasttm(L, h->metatable, TM_NEWINDEX);  /* get metamethod */
342
46.9M
      if (tm == NULL) {  /* no metamethod? */
343
23.1M
        sethvalue2s(L, L->top.p, h);  /* anchor 't' */
344
23.1M
        L->top.p++;  /* assume EXTRA_STACK */
345
23.1M
        luaH_finishset(L, h, key, val, hres);  /* set new value */
346
23.1M
        L->top.p--;
347
23.1M
        invalidateTMcache(h);
348
23.1M
        luaC_barrierback(L, obj2gco(h), val);
349
23.1M
        return;
350
23.1M
      }
351
      /* else will try the metamethod */
352
46.9M
    }
353
37.5M
    else {  /* not a table; check metamethod */
354
37.5M
      tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
355
37.5M
      if (l_unlikely(notm(tm)))
356
7.32k
        luaG_typeerror(L, t, "index");
357
37.5M
    }
358
    /* try the metamethod */
359
37.7M
    if (ttisfunction(tm)) {
360
37.7M
      luaT_callTM(L, tm, t, key, val);
361
37.7M
      return;
362
37.7M
    }
363
11
    t = tm;  /* else repeat assignment over 'tm' */
364
11
    luaV_fastset(t, key, val, hres, luaH_pset);
365
11
    if (hres == HOK) {
366
9
      luaV_finishfastset(L, t, val);
367
9
      return;  /* done */
368
9
    }
369
    /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
370
11
  }
371
0
  luaG_runerror(L, "'__newindex' chain too long; possible loop");
372
60.9M
}
373
374
375
/*
376
** Compare two strings 'ts1' x 'ts2', returning an integer less-equal-
377
** -greater than zero if 'ts1' is less-equal-greater than 'ts2'.
378
** The code is a little tricky because it allows '\0' in the strings
379
** and it uses 'strcoll' (to respect locales) for each segment
380
** of the strings. Note that segments can compare equal but still
381
** have different lengths.
382
*/
383
1.11M
static int l_strcmp (const TString *ts1, const TString *ts2) {
384
1.11M
  size_t rl1;  /* real length */
385
1.11M
  const char *s1 = getlstr(ts1, rl1);
386
1.11M
  size_t rl2;
387
1.11M
  const char *s2 = getlstr(ts2, rl2);
388
1.71M
  for (;;) {  /* for each segment */
389
1.71M
    int temp = strcoll(s1, s2);
390
1.71M
    if (temp != 0)  /* not equal? */
391
875k
      return temp;  /* done */
392
835k
    else {  /* strings are equal up to a '\0' */
393
835k
      size_t zl1 = strlen(s1);  /* index of first '\0' in 's1' */
394
835k
      size_t zl2 = strlen(s2);  /* index of first '\0' in 's2' */
395
835k
      if (zl2 == rl2)  /* 's2' is finished? */
396
228k
        return (zl1 == rl1) ? 0 : 1;  /* check 's1' */
397
606k
      else if (zl1 == rl1)  /* 's1' is finished? */
398
7.38k
        return -1;  /* 's1' is less than 's2' ('s2' is not finished) */
399
      /* both strings longer than 'zl'; go on comparing after the '\0' */
400
599k
      zl1++; zl2++;
401
599k
      s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
402
599k
    }
403
1.71M
  }
404
1.11M
}
405
406
407
/*
408
** Check whether integer 'i' is less than float 'f'. If 'i' has an
409
** exact representation as a float ('l_intfitsf'), compare numbers as
410
** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'.
411
** If 'ceil(f)' is out of integer range, either 'f' is greater than
412
** all integers or less than all integers.
413
** (The test with 'l_intfitsf' is only for performance; the else
414
** case is correct for all values, but it is slow due to the conversion
415
** from float to int.)
416
** When 'f' is NaN, comparisons must result in false.
417
*/
418
773k
l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
419
773k
  if (l_intfitsf(i))
420
689k
    return luai_numlt(cast_num(i), f);  /* compare them as floats */
421
83.4k
  else {  /* i < f <=> i < ceil(f) */
422
83.4k
    lua_Integer fi;
423
83.4k
    if (luaV_flttointeger(f, &fi, F2Iceil))  /* fi = ceil(f) */
424
11.1k
      return i < fi;   /* compare them as integers */
425
72.2k
    else  /* 'f' is either greater or less than all integers */
426
72.2k
      return f > 0;  /* greater? */
427
83.4k
  }
428
773k
}
429
430
431
/*
432
** Check whether integer 'i' is less than or equal to float 'f'.
433
** See comments on previous function.
434
*/
435
177k
l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
436
177k
  if (l_intfitsf(i))
437
171k
    return luai_numle(cast_num(i), f);  /* compare them as floats */
438
6.88k
  else {  /* i <= f <=> i <= floor(f) */
439
6.88k
    lua_Integer fi;
440
6.88k
    if (luaV_flttointeger(f, &fi, F2Ifloor))  /* fi = floor(f) */
441
6.15k
      return i <= fi;   /* compare them as integers */
442
731
    else  /* 'f' is either greater or less than all integers */
443
731
      return f > 0;  /* greater? */
444
6.88k
  }
445
177k
}
446
447
448
/*
449
** Check whether float 'f' is less than integer 'i'.
450
** See comments on previous function.
451
*/
452
6.03M
l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
453
6.03M
  if (l_intfitsf(i))
454
6.02M
    return luai_numlt(f, cast_num(i));  /* compare them as floats */
455
5.40k
  else {  /* f < i <=> floor(f) < i */
456
5.40k
    lua_Integer fi;
457
5.40k
    if (luaV_flttointeger(f, &fi, F2Ifloor))  /* fi = floor(f) */
458
3.19k
      return fi < i;   /* compare them as integers */
459
2.20k
    else  /* 'f' is either greater or less than all integers */
460
2.20k
      return f < 0;  /* less? */
461
5.40k
  }
462
6.03M
}
463
464
465
/*
466
** Check whether float 'f' is less than or equal to integer 'i'.
467
** See comments on previous function.
468
*/
469
1.14M
l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
470
1.14M
  if (l_intfitsf(i))
471
1.09M
    return luai_numle(f, cast_num(i));  /* compare them as floats */
472
46.2k
  else {  /* f <= i <=> ceil(f) <= i */
473
46.2k
    lua_Integer fi;
474
46.2k
    if (luaV_flttointeger(f, &fi, F2Iceil))  /* fi = ceil(f) */
475
44.8k
      return fi <= i;   /* compare them as integers */
476
1.38k
    else  /* 'f' is either greater or less than all integers */
477
1.38k
      return f < 0;  /* less? */
478
46.2k
  }
479
1.14M
}
480
481
482
/*
483
** Return 'l < r', for numbers.
484
*/
485
7.10M
l_sinline int LTnum (const TValue *l, const TValue *r) {
486
7.10M
  lua_assert(ttisnumber(l) && ttisnumber(r));
487
7.10M
  if (ttisinteger(l)) {
488
773k
    lua_Integer li = ivalue(l);
489
773k
    if (ttisinteger(r))
490
773k
      return li < ivalue(r);  /* both are integers */
491
773k
    else  /* 'l' is int and 'r' is float */
492
773k
      return LTintfloat(li, fltvalue(r));  /* l < r ? */
493
773k
  }
494
6.33M
  else {
495
6.33M
    lua_Number lf = fltvalue(l);  /* 'l' must be float */
496
6.33M
    if (ttisfloat(r))
497
6.33M
      return luai_numlt(lf, fltvalue(r));  /* both are float */
498
6.03M
    else  /* 'l' is float and 'r' is int */
499
6.03M
      return LTfloatint(lf, ivalue(r));
500
6.33M
  }
501
7.10M
}
502
503
504
/*
505
** Return 'l <= r', for numbers.
506
*/
507
1.34M
l_sinline int LEnum (const TValue *l, const TValue *r) {
508
1.34M
  lua_assert(ttisnumber(l) && ttisnumber(r));
509
1.34M
  if (ttisinteger(l)) {
510
177k
    lua_Integer li = ivalue(l);
511
177k
    if (ttisinteger(r))
512
177k
      return li <= ivalue(r);  /* both are integers */
513
177k
    else  /* 'l' is int and 'r' is float */
514
177k
      return LEintfloat(li, fltvalue(r));  /* l <= r ? */
515
177k
  }
516
1.16M
  else {
517
1.16M
    lua_Number lf = fltvalue(l);  /* 'l' must be float */
518
1.16M
    if (ttisfloat(r))
519
1.16M
      return luai_numle(lf, fltvalue(r));  /* both are float */
520
1.14M
    else  /* 'l' is float and 'r' is int */
521
1.14M
      return LEfloatint(lf, ivalue(r));
522
1.16M
  }
523
1.34M
}
524
525
526
/*
527
** return 'l < r' for non-numbers.
528
*/
529
2.01M
static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
530
2.01M
  lua_assert(!ttisnumber(l) || !ttisnumber(r));
531
2.01M
  if (ttisstring(l) && ttisstring(r))  /* both are strings? */
532
2.04M
    return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
533
987k
  else
534
987k
    return luaT_callorderTM(L, l, r, TM_LT);
535
2.01M
}
536
537
538
/*
539
** Main operation less than; return 'l < r'.
540
*/
541
994k
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
542
994k
  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
543
774
    return LTnum(l, r);
544
994k
  else return lessthanothers(L, l, r);
545
994k
}
546
547
548
/*
549
** return 'l <= r' for non-numbers.
550
*/
551
99.5k
static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
552
99.5k
  lua_assert(!ttisnumber(l) || !ttisnumber(r));
553
99.5k
  if (ttisstring(l) && ttisstring(r))  /* both are strings? */
554
178k
    return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
555
10.2k
  else
556
10.2k
    return luaT_callorderTM(L, l, r, TM_LE);
557
99.5k
}
558
559
560
/*
561
** Main operation less than or equal to; return 'l <= r'.
562
*/
563
26
int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
564
26
  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
565
26
    return LEnum(l, r);
566
0
  else return lessequalothers(L, l, r);
567
26
}
568
569
570
/*
571
** Main operation for equality of Lua values; return 't1 == t2'.
572
** L == NULL means raw equality (no metamethods)
573
*/
574
417M
int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
575
417M
  const TValue *tm;
576
417M
  if (ttype(t1) != ttype(t2))  /* not the same type? */
577
38.7M
    return 0;
578
378M
  else if (ttypetag(t1) != ttypetag(t2)) {
579
36.7M
    switch (ttypetag(t1)) {
580
26.1k
      case LUA_VNUMINT: {  /* integer == float? */
581
        /* integer and float can only be equal if float has an integer
582
           value equal to the integer */
583
26.1k
        lua_Integer i2;
584
26.1k
        return (luaV_flttointeger(fltvalue(t2), &i2, F2Ieq) &&
585
31.9k
                ivalue(t1) == i2);
586
0
      }
587
182k
      case LUA_VNUMFLT: {  /* float == integer? */
588
182k
        lua_Integer i1;  /* see comment in previous case */
589
182k
        return (luaV_flttointeger(fltvalue(t1), &i1, F2Ieq) &&
590
182k
                i1 == ivalue(t2));
591
0
      }
592
8.56k
      case LUA_VSHRSTR: case LUA_VLNGSTR: {
593
        /* compare two strings with different variants: they can be
594
           equal when one string is a short string and the other is
595
           an external string  */
596
17.1k
        return luaS_eqstr(tsvalue(t1), tsvalue(t2));
597
8.56k
      }
598
36.5M
      default:
599
        /* only numbers (integer/float) and strings (long/short) can have
600
           equal values with different variants */
601
36.5M
        return 0;
602
36.7M
    }
603
36.7M
  }
604
341M
  else {  /* equal variants */
605
341M
    switch (ttypetag(t1)) {
606
927k
      case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
607
927k
        return 1;
608
2.99M
      case LUA_VNUMINT:
609
2.99M
        return (ivalue(t1) == ivalue(t2));
610
5.05M
      case LUA_VNUMFLT:
611
5.05M
        return (fltvalue(t1) == fltvalue(t2));
612
0
      case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
613
261M
      case LUA_VSHRSTR:
614
1.04G
        return eqshrstr(tsvalue(t1), tsvalue(t2));
615
2.32M
      case LUA_VLNGSTR:
616
4.65M
        return luaS_eqstr(tsvalue(t1), tsvalue(t2));
617
32.5k
      case LUA_VUSERDATA: {
618
97.5k
        if (uvalue(t1) == uvalue(t2)) return 1;
619
2.62k
        else if (L == NULL) return 0;
620
2.62k
        tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
621
2.62k
        if (tm == NULL)
622
2.62k
          tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
623
2.62k
        break;  /* will try TM */
624
2.62k
      }
625
5.22M
      case LUA_VTABLE: {
626
15.6M
        if (hvalue(t1) == hvalue(t2)) return 1;
627
149k
        else if (L == NULL) return 0;
628
148k
        tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
629
148k
        if (tm == NULL)
630
143k
          tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
631
148k
        break;  /* will try TM */
632
148k
      }
633
62.7M
      case LUA_VLCF:
634
62.7M
        return (fvalue(t1) == fvalue(t2));
635
842k
      default:  /* functions and threads */
636
842k
        return (gcvalue(t1) == gcvalue(t2));
637
341M
    }
638
150k
    if (tm == NULL)  /* no TM? */
639
145k
      return 0;  /* objects are different */
640
5.27k
    else {
641
5.27k
      int tag = luaT_callTMres(L, tm, t1, t2, L->top.p);  /* call TM */
642
5.27k
      return !tagisfalse(tag);
643
5.27k
    }
644
150k
  }
645
417M
}
646
647
648
/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
649
#define tostring(L,o)  \
650
24.6M
  (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
651
652
44.9M
#define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
653
654
/* copy strings in stack from top - n up to top - 1 to buffer */
655
10.5M
static void copy2buff (StkId top, int n, char *buff) {
656
10.5M
  size_t tl = 0;  /* size already copied */
657
22.6M
  do {
658
45.3M
    TString *st = tsvalue(s2v(top - n));
659
0
    size_t l;  /* length of string being copied */
660
45.3M
    const char *s = getlstr(st, l);
661
45.3M
    memcpy(buff + tl, s, l * sizeof(char));
662
45.3M
    tl += l;
663
45.3M
  } while (--n > 0);
664
10.5M
}
665
666
667
/*
668
** Main operation for concatenation: concat 'total' values in the stack,
669
** from 'L->top.p - total' up to 'L->top.p - 1'.
670
*/
671
13.6M
void luaV_concat (lua_State *L, int total) {
672
13.6M
  if (total == 1)
673
6
    return;  /* "all" values already concatenated */
674
13.7M
  do {
675
13.7M
    StkId top = L->top.p;
676
13.7M
    int n = 2;  /* number of elements handled in this pass (at least 2) */
677
13.7M
    if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
678
13.7M
        !tostring(L, s2v(top - 1)))
679
1.87M
      luaT_tryconcatTM(L);  /* may invalidate 'top' */
680
11.8M
    else if (isemptystr(s2v(top - 1)))  /* second operand is empty? */
681
11.8M
      cast_void(tostring(L, s2v(top - 2)));  /* result is first operand */
682
11.8M
    else if (isemptystr(s2v(top - 2))) {  /* first operand is empty string? */
683
1.28M
      setobjs2s(L, top - 2, top - 1);  /* result is second op. */
684
1.28M
    }
685
10.5M
    else {
686
      /* at least two non-empty string values; get as many as possible */
687
10.5M
      size_t tl = tsslen(tsvalue(s2v(top - 1)));
688
10.5M
      TString *ts;
689
      /* collect total length and number of strings */
690
22.6M
      for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
691
12.1M
        size_t l = tsslen(tsvalue(s2v(top - n - 1)));
692
12.1M
        if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) {
693
0
          L->top.p = top - total;  /* pop strings to avoid wasting stack */
694
0
          luaG_runerror(L, "string length overflow");
695
0
        }
696
12.1M
        tl += l;
697
12.1M
      }
698
10.5M
      if (tl <= LUAI_MAXSHORTLEN) {  /* is result a short string? */
699
8.80M
        char buff[LUAI_MAXSHORTLEN];
700
8.80M
        copy2buff(top, n, buff);  /* copy strings to buffer */
701
8.80M
        ts = luaS_newlstr(L, buff, tl);
702
8.80M
      }
703
1.74M
      else {  /* long string; copy strings directly to final result */
704
1.74M
        ts = luaS_createlngstrobj(L, tl);
705
1.74M
        copy2buff(top, n, getlngstr(ts));
706
1.74M
      }
707
21.0M
      setsvalue2s(L, top - n, ts);  /* create result */
708
10.5M
    }
709
13.7M
    total -= n - 1;  /* got 'n' strings to create one new */
710
13.7M
    L->top.p -= n - 1;  /* popped 'n' strings and pushed one */
711
13.7M
  } while (total > 1);  /* repeat until only 1 result left */
712
13.6M
}
713
714
715
/*
716
** Main operation 'ra = #rb'.
717
*/
718
12.8M
void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
719
12.8M
  const TValue *tm;
720
12.8M
  switch (ttypetag(rb)) {
721
8.62M
    case LUA_VTABLE: {
722
17.2M
      Table *h = hvalue(rb);
723
8.62M
      tm = fasttm(L, h->metatable, TM_LEN);
724
17.2M
      if (tm) break;  /* metamethod? break switch to call it */
725
8.62M
      setivalue(s2v(ra), l_castU2S(luaH_getn(L, h)));  /* else primitive len */
726
8.62M
      return;
727
17.2M
    }
728
3.95M
    case LUA_VSHRSTR: {
729
3.95M
      setivalue(s2v(ra), tsvalue(rb)->shrlen);
730
3.95M
      return;
731
3.95M
    }
732
160k
    case LUA_VLNGSTR: {
733
160k
      setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen));
734
160k
      return;
735
160k
    }
736
152k
    default: {  /* try metamethod */
737
152k
      tm = luaT_gettmbyobj(L, rb, TM_LEN);
738
152k
      if (l_unlikely(notm(tm)))  /* no metamethod? */
739
1.59k
        luaG_typeerror(L, rb, "get length of");
740
150k
      break;
741
152k
    }
742
12.8M
  }
743
152k
  luaT_callTMres(L, tm, rb, rb, ra);
744
152k
}
745
746
747
/*
748
** Integer division; return 'm // n', that is, floor(m/n).
749
** C division truncates its result (rounds towards zero).
750
** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
751
** otherwise 'floor(q) == trunc(q) - 1'.
752
*/
753
1.97M
lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
754
1.97M
  if (l_unlikely(l_castS2U(n) + 1u <= 1u)) {  /* special cases: -1 or 0 */
755
8.93k
    if (n == 0)
756
25
      luaG_runerror(L, "attempt to divide by zero");
757
8.91k
    return intop(-, 0, m);   /* n==-1; avoid overflow with 0x80000...//-1 */
758
8.93k
  }
759
1.96M
  else {
760
1.96M
    lua_Integer q = m / n;  /* perform C division */
761
1.96M
    if ((m ^ n) < 0 && m % n != 0)  /* 'm/n' would be negative non-integer? */
762
58.9k
      q -= 1;  /* correct result for different rounding */
763
1.96M
    return q;
764
1.96M
  }
765
1.97M
}
766
767
768
/*
769
** Integer modulus; return 'm % n'. (Assume that C '%' with
770
** negative operands follows C99 behavior. See previous comment
771
** about luaV_idiv.)
772
*/
773
1.68M
lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
774
1.68M
  if (l_unlikely(l_castS2U(n) + 1u <= 1u)) {  /* special cases: -1 or 0 */
775
43.3k
    if (n == 0)
776
3.31k
      luaG_runerror(L, "attempt to perform 'n%%0'");
777
40.0k
    return 0;   /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
778
43.3k
  }
779
1.64M
  else {
780
1.64M
    lua_Integer r = m % n;
781
1.64M
    if (r != 0 && (r ^ n) < 0)  /* 'm/n' would be non-integer negative? */
782
379k
      r += n;  /* correct result for different rounding */
783
1.64M
    return r;
784
1.64M
  }
785
1.68M
}
786
787
788
/*
789
** Float modulus
790
*/
791
4.15M
lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
792
4.15M
  lua_Number r;
793
4.15M
  luai_nummod(L, m, n, r);
794
4.15M
  return r;
795
4.15M
}
796
797
798
/* number of bits in an integer */
799
1.89M
#define NBITS l_numbits(lua_Integer)
800
801
802
/*
803
** Shift left operation. (Shift right just negates 'y'.)
804
*/
805
1.89M
lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
806
1.89M
  if (y < 0) {  /* shift right? */
807
752k
    if (y <= -NBITS) return 0;
808
530k
    else return intop(>>, x, -y);
809
752k
  }
810
1.14M
  else {  /* shift left */
811
1.14M
    if (y >= NBITS) return 0;
812
964k
    else return intop(<<, x, y);
813
1.14M
  }
814
1.89M
}
815
816
817
/*
818
** create a new Lua closure, push it in the stack, and initialize
819
** its upvalues.
820
*/
821
static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
822
21.6M
                         StkId ra) {
823
21.6M
  int nup = p->sizeupvalues;
824
21.6M
  Upvaldesc *uv = p->upvalues;
825
21.6M
  int i;
826
21.6M
  LClosure *ncl = luaF_newLclosure(L, nup);
827
21.6M
  ncl->p = p;
828
21.6M
  setclLvalue2s(L, ra, ncl);  /* anchor new closure in stack */
829
40.7M
  for (i = 0; i < nup; i++) {  /* fill in its upvalues */
830
19.0M
    if (uv[i].instack)  /* upvalue refers to local variable? */
831
15.9M
      ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
832
3.12M
    else  /* get upvalue from enclosing function */
833
3.12M
      ncl->upvals[i] = encup[uv[i].idx];
834
19.0M
    luaC_objbarrier(L, ncl, ncl->upvals[i]);
835
19.0M
  }
836
21.6M
}
837
838
839
/*
840
** finish execution of an opcode interrupted by a yield
841
*/
842
381
void luaV_finishOp (lua_State *L) {
843
381
  CallInfo *ci = L->ci;
844
381
  StkId base = ci->func.p + 1;
845
381
  Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
846
381
  OpCode op = GET_OPCODE(inst);
847
381
  switch (op) {  /* finish its execution */
848
3
    case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
849
3
      setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p);
850
3
      break;
851
3
    }
852
0
    case OP_UNM: case OP_BNOT: case OP_LEN:
853
0
    case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
854
0
    case OP_GETFIELD: case OP_SELF: {
855
0
      setobjs2s(L, base + GETARG_A(inst), --L->top.p);
856
0
      break;
857
0
    }
858
0
    case OP_LT: case OP_LE:
859
0
    case OP_LTI: case OP_LEI:
860
0
    case OP_GTI: case OP_GEI:
861
0
    case OP_EQ: {  /* note that 'OP_EQI'/'OP_EQK' cannot yield */
862
0
      int res = !l_isfalse(s2v(L->top.p - 1));
863
0
      L->top.p--;
864
#if defined(LUA_COMPAT_LT_LE)
865
      if (ci->callstatus & CIST_LEQ) {  /* "<=" using "<" instead? */
866
        ci->callstatus ^= CIST_LEQ;  /* clear mark */
867
        res = !res;  /* negate result */
868
      }
869
#endif
870
0
      lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
871
0
      if (res != GETARG_k(inst))  /* condition failed? */
872
0
        ci->u.l.savedpc++;  /* skip jump instruction */
873
0
      break;
874
0
    }
875
0
    case OP_CONCAT: {
876
0
      StkId top = L->top.p - 1;  /* top when 'luaT_tryconcatTM' was called */
877
0
      int a = GETARG_A(inst);      /* first element to concatenate */
878
0
      int total = cast_int(top - 1 - (base + a));  /* yet to concatenate */
879
0
      setobjs2s(L, top - 2, top);  /* put TM result in proper position */
880
0
      L->top.p = top - 1;  /* top is one after last element (at top-2) */
881
0
      luaV_concat(L, total);  /* concat them (may yield again) */
882
0
      break;
883
0
    }
884
0
    case OP_CLOSE: {  /* yielded closing variables */
885
0
      ci->u.l.savedpc--;  /* repeat instruction to close other vars. */
886
0
      break;
887
0
    }
888
0
    case OP_RETURN: {  /* yielded closing variables */
889
0
      StkId ra = base + GETARG_A(inst);
890
      /* adjust top to signal correct number of returns, in case the
891
         return is "up to top" ('isIT') */
892
0
      L->top.p = ra + ci->u2.nres;
893
      /* repeat instruction to close other vars. and complete the return */
894
0
      ci->u.l.savedpc--;
895
0
      break;
896
0
    }
897
378
    default: {
898
      /* only these other opcodes can yield */
899
378
      lua_assert(op == OP_TFORCALL || op == OP_CALL ||
900
378
           op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
901
378
           op == OP_SETI || op == OP_SETFIELD);
902
378
      break;
903
378
    }
904
381
  }
905
381
}
906
907
908
909
910
/*
911
** {==================================================================
912
** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
913
** ===================================================================
914
*/
915
916
#define l_addi(L,a,b) intop(+, a, b)
917
#define l_subi(L,a,b) intop(-, a, b)
918
#define l_muli(L,a,b) intop(*, a, b)
919
#define l_band(a,b) intop(&, a, b)
920
#define l_bor(a,b)  intop(|, a, b)
921
#define l_bxor(a,b) intop(^, a, b)
922
923
2.03M
#define l_lti(a,b)  (a < b)
924
2.60M
#define l_lei(a,b)  (a <= b)
925
118M
#define l_gti(a,b)  (a > b)
926
273k
#define l_gei(a,b)  (a >= b)
927
928
929
/*
930
** Arithmetic operations with immediate operands. 'iop' is the integer
931
** operation, 'fop' is the float operation.
932
*/
933
60.5M
#define op_arithI(L,iop,fop) {  \
934
60.5M
  StkId ra = RA(i); \
935
60.5M
  TValue *v1 = vRB(i);  \
936
60.5M
  int imm = GETARG_sC(i);  \
937
60.5M
  if (ttisinteger(v1)) {  \
938
48.9M
    lua_Integer iv1 = ivalue(v1);  \
939
48.9M
    pc++; setivalue(s2v(ra), iop(L, iv1, imm));  \
940
48.9M
  }  \
941
60.5M
  else if (ttisfloat(v1)) {  \
942
11.0M
    lua_Number nb = fltvalue(v1);  \
943
11.0M
    lua_Number fimm = cast_num(imm);  \
944
11.0M
    pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
945
11.0M
  }}
946
947
948
/*
949
** Auxiliary function for arithmetic operations over floats and others
950
** with two operands.
951
*/
952
50.6M
#define op_arithf_aux(L,v1,v2,fop) {  \
953
50.6M
  lua_Number n1; lua_Number n2;  \
954
50.6M
  if (tonumberns(v1, n1) && tonumberns(v2, n2)) {  \
955
45.8M
    pc++; setfltvalue(s2v(ra), fop(L, n1, n2));  \
956
45.8M
  }}
957
958
959
/*
960
** Arithmetic operations over floats and others with register operands.
961
*/
962
9.35M
#define op_arithf(L,fop) {  \
963
9.35M
  StkId ra = RA(i); \
964
9.35M
  TValue *v1 = vRB(i);  \
965
9.35M
  TValue *v2 = vRC(i);  \
966
9.35M
  op_arithf_aux(L, v1, v2, fop); }
967
968
969
/*
970
** Arithmetic operations with K operands for floats.
971
*/
972
16.2M
#define op_arithfK(L,fop) {  \
973
16.2M
  StkId ra = RA(i); \
974
16.2M
  TValue *v1 = vRB(i);  \
975
16.2M
  TValue *v2 = KC(i); lua_assert(ttisnumber(v2));  \
976
16.2M
  op_arithf_aux(L, v1, v2, fop); }
977
978
979
/*
980
** Arithmetic operations over integers and floats.
981
*/
982
36.2M
#define op_arith_aux(L,v1,v2,iop,fop) {  \
983
36.2M
  StkId ra = RA(i); \
984
36.2M
  if (ttisinteger(v1) && ttisinteger(v2)) {  \
985
11.2M
    lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2);  \
986
11.2M
    pc++; setivalue(s2v(ra), iop(L, i1, i2));  \
987
11.2M
  }  \
988
36.2M
  else op_arithf_aux(L, v1, v2, fop); }
989
990
991
/*
992
** Arithmetic operations with register operands.
993
*/
994
15.2M
#define op_arith(L,iop,fop) {  \
995
15.2M
  TValue *v1 = vRB(i);  \
996
15.2M
  TValue *v2 = vRC(i);  \
997
15.2M
  op_arith_aux(L, v1, v2, iop, fop); }
998
999
1000
/*
1001
** Arithmetic operations with K operands.
1002
*/
1003
20.9M
#define op_arithK(L,iop,fop) {  \
1004
20.9M
  TValue *v1 = vRB(i);  \
1005
20.9M
  TValue *v2 = KC(i); lua_assert(ttisnumber(v2));  \
1006
20.9M
  op_arith_aux(L, v1, v2, iop, fop); }
1007
1008
1009
/*
1010
** Bitwise operations with constant operand.
1011
*/
1012
1.53M
#define op_bitwiseK(L,op) {  \
1013
1.53M
  StkId ra = RA(i); \
1014
1.53M
  TValue *v1 = vRB(i);  \
1015
1.53M
  TValue *v2 = KC(i);  \
1016
1.53M
  lua_Integer i1;  \
1017
1.53M
  lua_Integer i2 = ivalue(v2);  \
1018
1.53M
  if (tointegerns(v1, &i1)) {  \
1019
1.51M
    pc++; setivalue(s2v(ra), op(i1, i2));  \
1020
1.51M
  }}
1021
1022
1023
/*
1024
** Bitwise operations with register operands.
1025
*/
1026
4.42M
#define op_bitwise(L,op) {  \
1027
4.42M
  StkId ra = RA(i); \
1028
4.42M
  TValue *v1 = vRB(i);  \
1029
4.42M
  TValue *v2 = vRC(i);  \
1030
4.42M
  lua_Integer i1; lua_Integer i2;  \
1031
4.42M
  if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) {  \
1032
4.29M
    pc++; setivalue(s2v(ra), op(i1, i2));  \
1033
4.29M
  }}
1034
1035
1036
/*
1037
** Order operations with register operands. 'opn' actually works
1038
** for all numbers, but the fast track improves performance for
1039
** integers.
1040
*/
1041
13.6M
#define op_order(L,opi,opn,other) {  \
1042
13.6M
  StkId ra = RA(i); \
1043
13.6M
  int cond;  \
1044
13.6M
  TValue *rb = vRB(i);  \
1045
13.6M
  if (ttisinteger(s2v(ra)) && ttisinteger(rb)) {  \
1046
4.11M
    lua_Integer ia = ivalue(s2v(ra));  \
1047
4.11M
    lua_Integer ib = ivalue(rb);  \
1048
4.11M
    cond = opi(ia, ib);  \
1049
4.11M
  }  \
1050
13.6M
  else if (ttisnumber(s2v(ra)) && ttisnumber(rb))  \
1051
9.55M
    cond = opn(s2v(ra), rb);  \
1052
9.55M
  else  \
1053
9.55M
    Protect(cond = other(L, s2v(ra), rb));  \
1054
13.6M
  docondjump(); }
1055
1056
1057
/*
1058
** Order operations with immediate operand. (Immediate operand is
1059
** always small enough to have an exact representation as a float.)
1060
*/
1061
35.0M
#define op_orderI(L,opi,opf,inv,tm) {  \
1062
35.0M
  StkId ra = RA(i); \
1063
35.0M
  int cond;  \
1064
35.0M
  int im = GETARG_sB(i);  \
1065
35.0M
  if (ttisinteger(s2v(ra)))  \
1066
35.0M
    cond = opi(ivalue(s2v(ra)), im);  \
1067
35.0M
  else if (ttisfloat(s2v(ra))) {  \
1068
4.10M
    lua_Number fa = fltvalue(s2v(ra));  \
1069
4.10M
    lua_Number fim = cast_num(im);  \
1070
4.10M
    cond = opf(fa, fim);  \
1071
4.10M
  }  \
1072
4.69M
  else {  \
1073
584k
    int isf = GETARG_C(i);  \
1074
584k
    Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm));  \
1075
584k
  }  \
1076
35.0M
  docondjump(); }
1077
1078
/* }================================================================== */
1079
1080
1081
/*
1082
** {==================================================================
1083
** Function 'luaV_execute': main interpreter loop
1084
** ===================================================================
1085
*/
1086
1087
/*
1088
** some macros for common tasks in 'luaV_execute'
1089
*/
1090
1091
1092
2.47G
#define RA(i) (base+GETARG_A(i))
1093
#define RB(i) (base+GETARG_B(i))
1094
288M
#define vRB(i)  s2v(RB(i))
1095
236M
#define KB(i) (k+GETARG_B(i))
1096
#define RC(i) (base+GETARG_C(i))
1097
33.5M
#define vRC(i)  s2v(RC(i))
1098
497M
#define KC(i) (k+GETARG_C(i))
1099
120M
#define RKC(i)  ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
1100
1101
1102
1103
717M
#define updatetrap(ci)  (trap = ci->u.l.trap)
1104
1105
105M
#define updatebase(ci)  (base = ci->func.p + 1)
1106
1107
1108
#define updatestack(ci)  \
1109
593k
  { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } }
1110
1111
1112
/*
1113
** Execute a jump instruction. The 'updatetrap' allows signals to stop
1114
** tight loops. (Without it, the local copy of 'trap' could never change.)
1115
*/
1116
183M
#define dojump(ci,i,e)  { pc += GETARG_sJ(i) + e; updatetrap(ci); }
1117
1118
1119
/* for test instructions, execute the jump instruction that follows it */
1120
180M
#define donextjump(ci)  { Instruction ni = *pc; dojump(ci, ni, 1); }
1121
1122
/*
1123
** do a conditional jump: skip next instruction if 'cond' is not what
1124
** was expected (parameter 'k'), else do next instruction, which must
1125
** be a jump.
1126
*/
1127
285M
#define docondjump()  if (cond != GETARG_k(i)) pc++; else donextjump(ci);
1128
1129
1130
/*
1131
** Correct global 'pc'.
1132
*/
1133
720M
#define savepc(L) (ci->u.l.savedpc = pc)
1134
1135
1136
/*
1137
** Whenever code can raise errors, the global 'pc' and the global
1138
** 'top' must be correct to report occasional errors.
1139
*/
1140
248M
#define savestate(L,ci)   (savepc(L), L->top.p = ci->top.p)
1141
1142
1143
/*
1144
** Protect code that, in general, can raise errors, reallocate the
1145
** stack, and change the hooks.
1146
*/
1147
209M
#define Protect(exp)  (savestate(L,ci), (exp), updatetrap(ci))
1148
1149
/* special version that does not change the top */
1150
28.6M
#define ProtectNT(exp)  (savepc(L), (exp), updatetrap(ci))
1151
1152
/*
1153
** Protect code that can only raise errors. (That is, it cannot change
1154
** the stack or hooks.)
1155
*/
1156
21.7M
#define halfProtect(exp)  (savestate(L,ci), (exp))
1157
1158
/*
1159
** macro executed during Lua functions at points where the
1160
** function can yield.
1161
*/
1162
#if !defined(luai_threadyield)
1163
53.0M
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
1164
#endif
1165
1166
/* 'c' is the limit of live values in the stack */
1167
#define checkGC(L,c)  \
1168
53.0M
  { luaC_condGC(L, (savepc(L), L->top.p = (c)), \
1169
53.0M
                         updatetrap(ci)); \
1170
53.0M
           luai_threadyield(L); }
1171
1172
1173
/* fetch an instruction and prepare its execution */
1174
2.59G
#define vmfetch() { \
1175
2.59G
  if (l_unlikely(trap)) {  /* stack reallocation or hooks? */ \
1176
87.5M
    trap = luaG_traceexec(L, pc);  /* handle hooks */ \
1177
87.5M
    updatebase(ci);  /* correct stack */ \
1178
87.5M
  } \
1179
2.59G
  i = *(pc++); \
1180
2.59G
}
1181
1182
#define vmdispatch(o) switch(o)
1183
#define vmcase(l) case l:
1184
#define vmbreak   break
1185
1186
1187
135M
void luaV_execute (lua_State *L, CallInfo *ci) {
1188
135M
  LClosure *cl;
1189
135M
  TValue *k;
1190
135M
  StkId base;
1191
135M
  const Instruction *pc;
1192
135M
  int trap;
1193
135M
#if LUA_USE_JUMPTABLE
1194
135M
#include "ljumptab.h"
1195
135M
#endif
1196
306M
 startfunc:
1197
306M
  trap = L->hookmask;
1198
355M
 returning:  /* trap already set */
1199
710M
  cl = ci_func(ci);
1200
0
  k = cl->p->k;
1201
710M
  pc = ci->u.l.savedpc;
1202
710M
  if (l_unlikely(trap))
1203
5.99M
    trap = luaG_tracecall(L);
1204
710M
  base = ci->func.p + 1;
1205
  /* main loop of interpreter */
1206
710M
  for (;;) {
1207
355M
    Instruction i;  /* instruction being executed */
1208
355M
    vmfetch();
1209
    #if 0
1210
    { /* low-level line tracing for debugging Lua */
1211
      #include "lopnames.h"
1212
      int pcrel = pcRel(pc, cl->p);
1213
      printf("line: %d; %s (%d)\n", luaG_getfuncline(cl->p, pcrel),
1214
             opnames[GET_OPCODE(i)], pcrel);
1215
    }
1216
    #endif
1217
355M
    lua_assert(base == ci->func.p + 1);
1218
355M
    lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
1219
    /* for tests, invalidate top for instructions not expecting it */
1220
355M
    lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1));
1221
355M
    vmdispatch (GET_OPCODE(i)) {
1222
399M
      vmcase(OP_MOVE) {
1223
399M
        StkId ra = RA(i);
1224
799M
        setobjs2s(L, ra, RB(i));
1225
399M
        vmbreak;
1226
399M
      }
1227
36.4M
      vmcase(OP_LOADI) {
1228
36.4M
        StkId ra = RA(i);
1229
36.4M
        lua_Integer b = GETARG_sBx(i);
1230
36.4M
        setivalue(s2v(ra), b);
1231
36.4M
        vmbreak;
1232
36.4M
      }
1233
4.13M
      vmcase(OP_LOADF) {
1234
4.13M
        StkId ra = RA(i);
1235
4.13M
        int b = GETARG_sBx(i);
1236
4.13M
        setfltvalue(s2v(ra), cast_num(b));
1237
4.13M
        vmbreak;
1238
4.13M
      }
1239
88.7M
      vmcase(OP_LOADK) {
1240
88.7M
        StkId ra = RA(i);
1241
88.7M
        TValue *rb = k + GETARG_Bx(i);
1242
88.7M
        setobj2s(L, ra, rb);
1243
88.7M
        vmbreak;
1244
88.7M
      }
1245
0
      vmcase(OP_LOADKX) {
1246
0
        StkId ra = RA(i);
1247
0
        TValue *rb;
1248
0
        rb = k + GETARG_Ax(*pc); pc++;
1249
0
        setobj2s(L, ra, rb);
1250
0
        vmbreak;
1251
0
      }
1252
3.25M
      vmcase(OP_LOADFALSE) {
1253
3.25M
        StkId ra = RA(i);
1254
3.25M
        setbfvalue(s2v(ra));
1255
3.25M
        vmbreak;
1256
3.25M
      }
1257
5.42M
      vmcase(OP_LFALSESKIP) {
1258
5.42M
        StkId ra = RA(i);
1259
5.42M
        setbfvalue(s2v(ra));
1260
5.42M
        pc++;  /* skip next instruction */
1261
5.42M
        vmbreak;
1262
5.42M
      }
1263
15.7M
      vmcase(OP_LOADTRUE) {
1264
15.7M
        StkId ra = RA(i);
1265
15.7M
        setbtvalue(s2v(ra));
1266
15.7M
        vmbreak;
1267
15.7M
      }
1268
15.7M
      vmcase(OP_LOADNIL) {
1269
7.34M
        StkId ra = RA(i);
1270
7.34M
        int b = GETARG_B(i);
1271
39.1M
        do {
1272
39.1M
          setnilvalue(s2v(ra++));
1273
39.1M
        } while (b--);
1274
7.34M
        vmbreak;
1275
7.34M
      }
1276
260M
      vmcase(OP_GETUPVAL) {
1277
260M
        StkId ra = RA(i);
1278
260M
        int b = GETARG_B(i);
1279
260M
        setobj2s(L, ra, cl->upvals[b]->v.p);
1280
260M
        vmbreak;
1281
260M
      }
1282
6.66M
      vmcase(OP_SETUPVAL) {
1283
6.66M
        StkId ra = RA(i);
1284
6.66M
        UpVal *uv = cl->upvals[GETARG_B(i)];
1285
6.66M
        setobj(L, uv->v.p, s2v(ra));
1286
6.66M
        luaC_barrier(L, uv, s2v(ra));
1287
6.66M
        vmbreak;
1288
6.66M
      }
1289
349M
      vmcase(OP_GETTABUP) {
1290
349M
        StkId ra = RA(i);
1291
349M
        TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
1292
349M
        TValue *rc = KC(i);
1293
698M
        TString *key = tsvalue(rc);  /* key must be a short string */
1294
0
        lu_byte tag;
1295
698M
        luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag);
1296
349M
        if (tagisempty(tag))
1297
45.4M
          Protect(luaV_finishget(L, upval, rc, ra, tag));
1298
349M
        vmbreak;
1299
349M
      }
1300
4.47M
      vmcase(OP_GETTABLE) {
1301
4.47M
        StkId ra = RA(i);
1302
4.47M
        TValue *rb = vRB(i);
1303
4.47M
        TValue *rc = vRC(i);
1304
0
        lu_byte tag;
1305
4.47M
        if (ttisinteger(rc)) {  /* fast track for integers? */
1306
717k
          luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag);
1307
717k
        }
1308
3.75M
        else
1309
3.75M
          luaV_fastget(rb, rc, s2v(ra), luaH_get, tag);
1310
4.47M
        if (tagisempty(tag))
1311
1.67M
          Protect(luaV_finishget(L, rb, rc, ra, tag));
1312
4.47M
        vmbreak;
1313
4.47M
      }
1314
631k
      vmcase(OP_GETI) {
1315
631k
        StkId ra = RA(i);
1316
631k
        TValue *rb = vRB(i);
1317
631k
        int c = GETARG_C(i);
1318
0
        lu_byte tag;
1319
631k
        luaV_fastgeti(rb, c, s2v(ra), tag);
1320
631k
        if (tagisempty(tag)) {
1321
142k
          TValue key;
1322
142k
          setivalue(&key, c);
1323
142k
          Protect(luaV_finishget(L, rb, &key, ra, tag));
1324
142k
        }
1325
631k
        vmbreak;
1326
631k
      }
1327
100M
      vmcase(OP_GETFIELD) {
1328
100M
        StkId ra = RA(i);
1329
100M
        TValue *rb = vRB(i);
1330
100M
        TValue *rc = KC(i);
1331
201M
        TString *key = tsvalue(rc);  /* key must be a short string */
1332
0
        lu_byte tag;
1333
201M
        luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag);
1334
100M
        if (tagisempty(tag))
1335
75.0M
          Protect(luaV_finishget(L, rb, rc, ra, tag));
1336
100M
        vmbreak;
1337
100M
      }
1338
55.3M
      vmcase(OP_SETTABUP) {
1339
55.3M
        int hres;
1340
55.3M
        TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
1341
55.3M
        TValue *rb = KB(i);
1342
55.3M
        TValue *rc = RKC(i);
1343
110M
        TString *key = tsvalue(rb);  /* key must be a short string */
1344
55.3M
        luaV_fastset(upval, key, rc, hres, luaH_psetshortstr);
1345
55.3M
        if (hres == HOK)
1346
55.3M
          luaV_finishfastset(L, upval, rc);
1347
3.08M
        else
1348
3.08M
          Protect(luaV_finishset(L, upval, rb, rc, hres));
1349
55.3M
        vmbreak;
1350
55.3M
      }
1351
15.5M
      vmcase(OP_SETTABLE) {
1352
15.5M
        StkId ra = RA(i);
1353
15.5M
        int hres;
1354
15.5M
        TValue *rb = vRB(i);  /* key (table is in 'ra') */
1355
15.5M
        TValue *rc = RKC(i);  /* value */
1356
15.5M
        if (ttisinteger(rb)) {  /* fast track for integers? */
1357
11.2M
          luaV_fastseti(s2v(ra), ivalue(rb), rc, hres);
1358
11.2M
        }
1359
4.31M
        else {
1360
4.31M
          luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset);
1361
4.31M
        }
1362
15.5M
        if (hres == HOK)
1363
15.5M
          luaV_finishfastset(L, s2v(ra), rc);
1364
5.80M
        else
1365
5.80M
          Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
1366
15.5M
        vmbreak;
1367
15.5M
      }
1368
2.17M
      vmcase(OP_SETI) {
1369
2.17M
        StkId ra = RA(i);
1370
2.17M
        int hres;
1371
2.17M
        int b = GETARG_B(i);
1372
2.17M
        TValue *rc = RKC(i);
1373
2.17M
        luaV_fastseti(s2v(ra), b, rc, hres);
1374
2.17M
        if (hres == HOK)
1375
2.17M
          luaV_finishfastset(L, s2v(ra), rc);
1376
228k
        else {
1377
228k
          TValue key;
1378
228k
          setivalue(&key, b);
1379
228k
          Protect(luaV_finishset(L, s2v(ra), &key, rc, hres));
1380
228k
        }
1381
2.17M
        vmbreak;
1382
2.17M
      }
1383
47.0M
      vmcase(OP_SETFIELD) {
1384
47.0M
        StkId ra = RA(i);
1385
47.0M
        int hres;
1386
47.0M
        TValue *rb = KB(i);
1387
47.0M
        TValue *rc = RKC(i);
1388
94.0M
        TString *key = tsvalue(rb);  /* key must be a short string */
1389
47.0M
        luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr);
1390
47.0M
        if (hres == HOK)
1391
47.0M
          luaV_finishfastset(L, s2v(ra), rc);
1392
35.0M
        else
1393
35.0M
          Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
1394
47.0M
        vmbreak;
1395
47.0M
      }
1396
20.6M
      vmcase(OP_NEWTABLE) {
1397
20.6M
        StkId ra = RA(i);
1398
20.6M
        unsigned b = cast_uint(GETARG_vB(i));  /* log2(hash size) + 1 */
1399
20.6M
        unsigned c = cast_uint(GETARG_vC(i));  /* array size */
1400
0
        Table *t;
1401
20.6M
        if (b > 0)
1402
5.81M
          b = 1u << (b - 1);  /* hash size is 2^(b - 1) */
1403
20.6M
        if (TESTARG_k(i)) {  /* non-zero extra argument? */
1404
12.9k
          lua_assert(GETARG_Ax(*pc) != 0);
1405
          /* add it to array size */
1406
12.9k
          c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1);
1407
12.9k
        }
1408
20.6M
        pc++;  /* skip extra argument */
1409
20.6M
        L->top.p = ra + 1;  /* correct top in case of emergency GC */
1410
20.6M
        t = luaH_new(L);  /* memory allocation */
1411
20.6M
        sethvalue2s(L, ra, t);
1412
20.6M
        if (b != 0 || c != 0)
1413
7.50M
          luaH_resize(L, t, c, b);  /* idem */
1414
20.6M
        checkGC(L, ra + 1);
1415
20.6M
        vmbreak;
1416
20.6M
      }
1417
8.99M
      vmcase(OP_SELF) {
1418
8.99M
        StkId ra = RA(i);
1419
8.99M
        lu_byte tag;
1420
8.99M
        TValue *rb = vRB(i);
1421
8.99M
        TValue *rc = KC(i);
1422
17.9M
        TString *key = tsvalue(rc);  /* key must be a short string */
1423
8.99M
        setobj2s(L, ra + 1, rb);
1424
8.99M
        luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag);
1425
8.99M
        if (tagisempty(tag))
1426
8.57M
          Protect(luaV_finishget(L, rb, rc, ra, tag));
1427
8.99M
        vmbreak;
1428
8.99M
      }
1429
60.5M
      vmcase(OP_ADDI) {
1430
121M
        op_arithI(L, l_addi, luai_numadd);
1431
60.5M
        vmbreak;
1432
60.5M
      }
1433
7.36M
      vmcase(OP_ADDK) {
1434
29.4M
        op_arithK(L, l_addi, luai_numadd);
1435
29.4M
        vmbreak;
1436
29.4M
      }
1437
2.56M
      vmcase(OP_SUBK) {
1438
10.2M
        op_arithK(L, l_subi, luai_numsub);
1439
10.2M
        vmbreak;
1440
10.2M
      }
1441
415k
      vmcase(OP_MULK) {
1442
1.66M
        op_arithK(L, l_muli, luai_nummul);
1443
1.66M
        vmbreak;
1444
1.66M
      }
1445
4.52M
      vmcase(OP_MODK) {
1446
4.52M
        savestate(L, ci);  /* in case of division by 0 */
1447
18.1M
        op_arithK(L, luaV_mod, luaV_modf);
1448
18.1M
        vmbreak;
1449
18.1M
      }
1450
962k
      vmcase(OP_POWK) {
1451
2.88M
        op_arithfK(L, luai_numpow);
1452
2.88M
        vmbreak;
1453
2.88M
      }
1454
15.2M
      vmcase(OP_DIVK) {
1455
45.8M
        op_arithfK(L, luai_numdiv);
1456
45.8M
        vmbreak;
1457
45.8M
      }
1458
6.08M
      vmcase(OP_IDIVK) {
1459
6.08M
        savestate(L, ci);  /* in case of division by 0 */
1460
24.3M
        op_arithK(L, luaV_idiv, luai_numidiv);
1461
24.3M
        vmbreak;
1462
24.3M
      }
1463
48.1k
      vmcase(OP_BANDK) {
1464
144k
        op_bitwiseK(L, l_band);
1465
144k
        vmbreak;
1466
144k
      }
1467
593k
      vmcase(OP_BORK) {
1468
1.78M
        op_bitwiseK(L, l_bor);
1469
1.78M
        vmbreak;
1470
1.78M
      }
1471
896k
      vmcase(OP_BXORK) {
1472
2.68M
        op_bitwiseK(L, l_bxor);
1473
2.68M
        vmbreak;
1474
2.68M
      }
1475
46.7k
      vmcase(OP_SHRI) {
1476
46.7k
        StkId ra = RA(i);
1477
46.7k
        TValue *rb = vRB(i);
1478
46.7k
        int ic = GETARG_sC(i);
1479
0
        lua_Integer ib;
1480
46.7k
        if (tointegerns(rb, &ib)) {
1481
35.1k
          pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
1482
35.1k
        }
1483
46.7k
        vmbreak;
1484
46.7k
      }
1485
186k
      vmcase(OP_SHLI) {
1486
186k
        StkId ra = RA(i);
1487
186k
        TValue *rb = vRB(i);
1488
186k
        int ic = GETARG_sC(i);
1489
0
        lua_Integer ib;
1490
186k
        if (tointegerns(rb, &ib)) {
1491
176k
          pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
1492
176k
        }
1493
186k
        vmbreak;
1494
186k
      }
1495
6.23M
      vmcase(OP_ADD) {
1496
18.7M
        op_arith(L, l_addi, luai_numadd);
1497
18.7M
        vmbreak;
1498
18.7M
      }
1499
1.72M
      vmcase(OP_SUB) {
1500
5.16M
        op_arith(L, l_subi, luai_numsub);
1501
5.16M
        vmbreak;
1502
5.16M
      }
1503
3.79M
      vmcase(OP_MUL) {
1504
11.3M
        op_arith(L, l_muli, luai_nummul);
1505
11.3M
        vmbreak;
1506
11.3M
      }
1507
1.55M
      vmcase(OP_MOD) {
1508
1.55M
        savestate(L, ci);  /* in case of division by 0 */
1509
4.66M
        op_arith(L, luaV_mod, luaV_modf);
1510
4.66M
        vmbreak;
1511
4.66M
      }
1512
5.25M
      vmcase(OP_POW) {
1513
10.5M
        op_arithf(L, luai_numpow);
1514
10.5M
        vmbreak;
1515
10.5M
      }
1516
4.10M
      vmcase(OP_DIV) {  /* float division (always with floats) */
1517
8.20M
        op_arithf(L, luai_numdiv);
1518
8.20M
        vmbreak;
1519
8.20M
      }
1520
1.93M
      vmcase(OP_IDIV) {  /* floor division */
1521
1.93M
        savestate(L, ci);  /* in case of division by 0 */
1522
5.81M
        op_arith(L, luaV_idiv, luai_numidiv);
1523
5.81M
        vmbreak;
1524
5.81M
      }
1525
279k
      vmcase(OP_BAND) {
1526
558k
        op_bitwise(L, l_band);
1527
558k
        vmbreak;
1528
558k
      }
1529
2.04M
      vmcase(OP_BOR) {
1530
4.08M
        op_bitwise(L, l_bor);
1531
4.08M
        vmbreak;
1532
4.08M
      }
1533
534k
      vmcase(OP_BXOR) {
1534
1.06M
        op_bitwise(L, l_bxor);
1535
1.06M
        vmbreak;
1536
1.06M
      }
1537
1.36M
      vmcase(OP_SHR) {
1538
2.73M
        op_bitwise(L, luaV_shiftr);
1539
2.73M
        vmbreak;
1540
2.73M
      }
1541
194k
      vmcase(OP_SHL) {
1542
388k
        op_bitwise(L, luaV_shiftl);
1543
388k
        vmbreak;
1544
388k
      }
1545
4.45M
      vmcase(OP_MMBIN) {
1546
4.45M
        StkId ra = RA(i);
1547
4.45M
        Instruction pi = *(pc - 2);  /* original arith. expression */
1548
4.45M
        TValue *rb = vRB(i);
1549
4.45M
        TMS tm = (TMS)GETARG_C(i);
1550
4.45M
        StkId result = RA(pi);
1551
4.45M
        lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
1552
4.45M
        Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
1553
4.45M
        vmbreak;
1554
4.45M
      }
1555
653k
      vmcase(OP_MMBINI) {
1556
653k
        StkId ra = RA(i);
1557
653k
        Instruction pi = *(pc - 2);  /* original arith. expression */
1558
653k
        int imm = GETARG_sB(i);
1559
653k
        TMS tm = (TMS)GETARG_C(i);
1560
653k
        int flip = GETARG_k(i);
1561
653k
        StkId result = RA(pi);
1562
653k
        Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
1563
653k
        vmbreak;
1564
653k
      }
1565
461k
      vmcase(OP_MMBINK) {
1566
461k
        StkId ra = RA(i);
1567
461k
        Instruction pi = *(pc - 2);  /* original arith. expression */
1568
461k
        TValue *imm = KB(i);
1569
461k
        TMS tm = (TMS)GETARG_C(i);
1570
461k
        int flip = GETARG_k(i);
1571
461k
        StkId result = RA(pi);
1572
461k
        Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
1573
461k
        vmbreak;
1574
461k
      }
1575
3.57M
      vmcase(OP_UNM) {
1576
3.57M
        StkId ra = RA(i);
1577
3.57M
        TValue *rb = vRB(i);
1578
0
        lua_Number nb;
1579
3.57M
        if (ttisinteger(rb)) {
1580
1.43M
          lua_Integer ib = ivalue(rb);
1581
1.43M
          setivalue(s2v(ra), intop(-, 0, ib));
1582
1.43M
        }
1583
2.13M
        else if (tonumberns(rb, nb)) {
1584
1.92M
          setfltvalue(s2v(ra), luai_numunm(L, nb));
1585
1.92M
        }
1586
209k
        else
1587
209k
          Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
1588
3.57M
        vmbreak;
1589
3.57M
      }
1590
2.48M
      vmcase(OP_BNOT) {
1591
2.48M
        StkId ra = RA(i);
1592
2.48M
        TValue *rb = vRB(i);
1593
0
        lua_Integer ib;
1594
2.48M
        if (tointegerns(rb, &ib)) {
1595
2.45M
          setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
1596
2.45M
        }
1597
25.4k
        else
1598
25.4k
          Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
1599
2.48M
        vmbreak;
1600
2.48M
      }
1601
609k
      vmcase(OP_NOT) {
1602
609k
        StkId ra = RA(i);
1603
609k
        TValue *rb = vRB(i);
1604
609k
        if (l_isfalse(rb))
1605
609k
          setbtvalue(s2v(ra));
1606
415k
        else
1607
609k
          setbfvalue(s2v(ra));
1608
609k
        vmbreak;
1609
609k
      }
1610
12.7M
      vmcase(OP_LEN) {
1611
12.7M
        StkId ra = RA(i);
1612
12.7M
        Protect(luaV_objlen(L, ra, vRB(i)));
1613
12.7M
        vmbreak;
1614
12.7M
      }
1615
10.7M
      vmcase(OP_CONCAT) {
1616
10.7M
        StkId ra = RA(i);
1617
10.7M
        int n = GETARG_B(i);  /* number of elements to concatenate */
1618
0
        L->top.p = ra + n;  /* mark the end of concat operands */
1619
10.7M
        ProtectNT(luaV_concat(L, n));
1620
10.7M
        checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */
1621
10.7M
        vmbreak;
1622
10.7M
      }
1623
5.75M
      vmcase(OP_CLOSE) {
1624
5.75M
        StkId ra = RA(i);
1625
5.75M
        lua_assert(!GETARG_B(i));  /* 'close must be alive */
1626
5.75M
        Protect(luaF_close(L, ra, LUA_OK, 1));
1627
5.75M
        vmbreak;
1628
5.75M
      }
1629
3.49k
      vmcase(OP_TBC) {
1630
3.49k
        StkId ra = RA(i);
1631
        /* create new to-be-closed upvalue */
1632
3.49k
        halfProtect(luaF_newtbcupval(L, ra));
1633
3.49k
        vmbreak;
1634
3.49k
      }
1635
3.09M
      vmcase(OP_JMP) {
1636
3.09M
        dojump(ci, i, 0);
1637
3.09M
        vmbreak;
1638
3.09M
      }
1639
4.08M
      vmcase(OP_EQ) {
1640
4.08M
        StkId ra = RA(i);
1641
4.08M
        int cond;
1642
4.08M
        TValue *rb = vRB(i);
1643
4.08M
        Protect(cond = luaV_equalobj(L, s2v(ra), rb));
1644
4.08M
        docondjump();
1645
4.08M
        vmbreak;
1646
4.08M
      }
1647
9.75M
      vmcase(OP_LT) {
1648
29.2M
        op_order(L, l_lti, LTnum, lessthanothers);
1649
29.2M
        vmbreak;
1650
29.2M
      }
1651
3.91M
      vmcase(OP_LE) {
1652
11.7M
        op_order(L, l_lei, LEnum, lessequalothers);
1653
11.7M
        vmbreak;
1654
11.7M
      }
1655
133M
      vmcase(OP_EQK) {
1656
133M
        StkId ra = RA(i);
1657
133M
        TValue *rb = KB(i);
1658
        /* basic types do not use '__eq'; we can use raw equality */
1659
133M
        int cond = luaV_rawequalobj(s2v(ra), rb);
1660
133M
        docondjump();
1661
133M
        vmbreak;
1662
133M
      }
1663
552k
      vmcase(OP_EQI) {
1664
552k
        StkId ra = RA(i);
1665
552k
        int cond;
1666
552k
        int im = GETARG_sB(i);
1667
552k
        if (ttisinteger(s2v(ra)))
1668
439k
          cond = (ivalue(s2v(ra)) == im);
1669
113k
        else if (ttisfloat(s2v(ra)))
1670
88.3k
          cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
1671
24.9k
        else
1672
24.9k
          cond = 0;  /* other types cannot be equal to a number */
1673
1.10M
        docondjump();
1674
1.10M
        vmbreak;
1675
1.10M
      }
1676
448k
      vmcase(OP_LTI) {
1677
1.34M
        op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
1678
1.34M
        vmbreak;
1679
1.34M
      }
1680
134k
      vmcase(OP_LEI) {
1681
403k
        op_orderI(L, l_lei, luai_numle, 0, TM_LE);
1682
403k
        vmbreak;
1683
403k
      }
1684
31.3M
      vmcase(OP_GTI) {
1685
94.0M
        op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
1686
94.0M
        vmbreak;
1687
94.0M
      }
1688
3.09M
      vmcase(OP_GEI) {
1689
9.29M
        op_orderI(L, l_gei, luai_numge, 1, TM_LE);
1690
9.29M
        vmbreak;
1691
9.29M
      }
1692
98.5M
      vmcase(OP_TEST) {
1693
98.5M
        StkId ra = RA(i);
1694
98.5M
        int cond = !l_isfalse(s2v(ra));
1695
98.5M
        docondjump();
1696
98.5M
        vmbreak;
1697
98.5M
      }
1698
521k
      vmcase(OP_TESTSET) {
1699
521k
        StkId ra = RA(i);
1700
521k
        TValue *rb = vRB(i);
1701
521k
        if (l_isfalse(rb) == GETARG_k(i))
1702
490k
          pc++;
1703
30.8k
        else {
1704
30.8k
          setobj2s(L, ra, rb);
1705
30.8k
          donextjump(ci);
1706
30.8k
        }
1707
521k
        vmbreak;
1708
521k
      }
1709
354M
      vmcase(OP_CALL) {
1710
354M
        StkId ra = RA(i);
1711
354M
        CallInfo *newci;
1712
354M
        int b = GETARG_B(i);
1713
354M
        int nresults = GETARG_C(i) - 1;
1714
354M
        if (b != 0)  /* fixed number of arguments? */
1715
340M
          L->top.p = ra + b;  /* top signals number of arguments */
1716
        /* else previous instruction set top */
1717
354M
        savepc(L);  /* in case of errors */
1718
354M
        if ((newci = luaD_precall(L, ra, nresults)) == NULL)
1719
262M
          updatetrap(ci);  /* C call; nothing else to be done */
1720
91.9M
        else {  /* Lua call: run function in this same C frame */
1721
91.9M
          ci = newci;
1722
91.9M
          goto startfunc;
1723
91.9M
        }
1724
354M
        vmbreak;
1725
262M
      }
1726
80.7M
      vmcase(OP_TAILCALL) {
1727
80.7M
        StkId ra = RA(i);
1728
80.7M
        int b = GETARG_B(i);  /* number of arguments + 1 (function) */
1729
0
        int n;  /* number of results when calling a C function */
1730
80.7M
        int nparams1 = GETARG_C(i);
1731
        /* delta is virtual 'func' - real 'func' (vararg functions) */
1732
80.7M
        int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
1733
80.7M
        if (b != 0)
1734
80.7M
          L->top.p = ra + b;
1735
3.09k
        else  /* previous instruction set top */
1736
3.09k
          b = cast_int(L->top.p - ra);
1737
80.7M
        savepc(ci);  /* several calls here can raise errors */
1738
80.7M
        if (TESTARG_k(i)) {
1739
30.4k
          luaF_closeupval(L, base);  /* close upvalues from current call */
1740
30.4k
          lua_assert(L->tbclist.p < base);  /* no pending tbc variables */
1741
30.4k
          lua_assert(base == ci->func.p + 1);
1742
30.4k
        }
1743
80.7M
        if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0)  /* Lua function? */
1744
80.7M
          goto startfunc;  /* execute the callee */
1745
13.8k
        else {  /* C function? */
1746
13.8k
          ci->func.p -= delta;  /* restore 'func' (if vararg) */
1747
13.8k
          luaD_poscall(L, ci, n);  /* finish caller */
1748
13.8k
          updatetrap(ci);  /* 'luaD_poscall' can change hooks */
1749
13.8k
          goto ret;  /* caller returns after the tail call */
1750
13.8k
        }
1751
80.7M
      }
1752
3.56M
      vmcase(OP_RETURN) {
1753
3.56M
        StkId ra = RA(i);
1754
3.56M
        int n = GETARG_B(i) - 1;  /* number of results */
1755
3.56M
        int nparams1 = GETARG_C(i);
1756
3.56M
        if (n < 0)  /* not fixed? */
1757
6.44k
          n = cast_int(L->top.p - ra);  /* get what is available */
1758
3.56M
        savepc(ci);
1759
3.56M
        if (TESTARG_k(i)) {  /* may there be open upvalues? */
1760
158k
          ci->u2.nres = n;  /* save number of returns */
1761
158k
          if (L->top.p < ci->top.p)
1762
90.1k
            L->top.p = ci->top.p;
1763
158k
          luaF_close(L, base, CLOSEKTOP, 1);
1764
158k
          updatetrap(ci);
1765
158k
          updatestack(ci);
1766
158k
        }
1767
3.56M
        if (nparams1)  /* vararg function? */
1768
3.23M
          ci->func.p -= ci->u.l.nextraargs + nparams1;
1769
3.56M
        L->top.p = ra + n;  /* set call for 'luaD_poscall' */
1770
3.56M
        luaD_poscall(L, ci, n);
1771
3.56M
        updatetrap(ci);  /* 'luaD_poscall' can change hooks */
1772
3.56M
        goto ret;
1773
3.56M
      }
1774
43.2M
      vmcase(OP_RETURN0) {
1775
43.2M
        if (l_unlikely(L->hookmask)) {
1776
4.77M
          StkId ra = RA(i);
1777
4.77M
          L->top.p = ra;
1778
4.77M
          savepc(ci);
1779
4.77M
          luaD_poscall(L, ci, 0);  /* no hurry... */
1780
4.77M
          trap = 1;
1781
4.77M
        }
1782
38.4M
        else {  /* do the 'poscall' here */
1783
38.4M
          int nres = get_nresults(ci->callstatus);
1784
38.4M
          L->ci = ci->previous;  /* back to caller */
1785
38.4M
          L->top.p = base - 1;
1786
38.4M
          for (; l_unlikely(nres > 0); nres--)
1787
38.4M
            setnilvalue(s2v(L->top.p++));  /* all results are nil */
1788
38.4M
        }
1789
43.2M
        goto ret;
1790
3.56M
      }
1791
135M
      vmcase(OP_RETURN1) {
1792
135M
        if (l_unlikely(L->hookmask)) {
1793
42.6k
          StkId ra = RA(i);
1794
42.6k
          L->top.p = ra + 1;
1795
42.6k
          savepc(ci);
1796
42.6k
          luaD_poscall(L, ci, 1);  /* no hurry... */
1797
42.6k
          trap = 1;
1798
42.6k
        }
1799
135M
        else {  /* do the 'poscall' here */
1800
135M
          int nres = get_nresults(ci->callstatus);
1801
135M
          L->ci = ci->previous;  /* back to caller */
1802
135M
          if (nres == 0)
1803
4.58M
            L->top.p = base - 1;  /* asked for no results */
1804
130M
          else {
1805
130M
            StkId ra = RA(i);
1806
130M
            setobjs2s(L, base - 1, ra);  /* at least this result */
1807
130M
            L->top.p = base;
1808
130M
            for (; l_unlikely(nres > 1); nres--)
1809
130M
              setnilvalue(s2v(L->top.p++));  /* complete missing results */
1810
130M
          }
1811
135M
        }
1812
182M
       ret:  /* return from a Lua function */
1813
182M
        if (ci->callstatus & CIST_FRESH)
1814
134M
          return;  /* end this frame */
1815
48.2M
        else {
1816
48.2M
          ci = ci->previous;
1817
48.2M
          goto returning;  /* continue running caller in this frame */
1818
48.2M
        }
1819
182M
      }
1820
30.0M
      vmcase(OP_FORLOOP) {
1821
30.0M
        StkId ra = RA(i);
1822
30.0M
        if (ttisinteger(s2v(ra + 1))) {  /* integer loop? */
1823
25.8M
          lua_Unsigned count = l_castS2U(ivalue(s2v(ra)));
1824
25.8M
          if (count > 0) {  /* still more iterations? */
1825
25.0M
            lua_Integer step = ivalue(s2v(ra + 1));
1826
25.0M
            lua_Integer idx = ivalue(s2v(ra + 2));  /* control variable */
1827
25.0M
            chgivalue(s2v(ra), l_castU2S(count - 1));  /* update counter */
1828
25.0M
            idx = intop(+, idx, step);  /* add step to index */
1829
25.0M
            chgivalue(s2v(ra + 2), idx);  /* update control variable */
1830
25.0M
            pc -= GETARG_Bx(i);  /* jump back */
1831
25.0M
          }
1832
25.8M
        }
1833
4.15M
        else if (floatforloop(ra))  /* float loop */
1834
3.87M
          pc -= GETARG_Bx(i);  /* jump back */
1835
30.0M
        updatetrap(ci);  /* allows a signal to break the loop */
1836
30.0M
        vmbreak;
1837
30.0M
      }
1838
3.12M
      vmcase(OP_FORPREP) {
1839
3.12M
        StkId ra = RA(i);
1840
3.12M
        savestate(L, ci);  /* in case of errors */
1841
3.12M
        if (forprep(L, ra))
1842
1.34M
          pc += GETARG_Bx(i) + 1;  /* skip the loop */
1843
3.12M
        vmbreak;
1844
3.12M
      }
1845
142k
      vmcase(OP_TFORPREP) {
1846
       /* before: 'ra' has the iterator function, 'ra + 1' has the state,
1847
          'ra + 2' has the initial value for the control variable, and
1848
          'ra + 3' has the closing variable. This opcode then swaps the
1849
          control and the closing variables and marks the closing variable
1850
          as to-be-closed.
1851
       */
1852
142k
       StkId ra = RA(i);
1853
142k
       TValue temp;  /* to swap control and closing variables */
1854
142k
       setobj(L, &temp, s2v(ra + 3));
1855
142k
       setobjs2s(L, ra + 3, ra + 2);
1856
142k
       setobj2s(L, ra + 2, &temp);
1857
        /* create to-be-closed upvalue (if closing var. is not nil) */
1858
142k
        halfProtect(luaF_newtbcupval(L, ra + 2));
1859
142k
        pc += GETARG_Bx(i);  /* go to end of the loop */
1860
0
        i = *(pc++);  /* fetch next instruction */
1861
141k
        lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
1862
141k
        goto l_tforcall;
1863
141k
      }
1864
293k
      vmcase(OP_TFORCALL) {
1865
435k
       l_tforcall: {
1866
        /* 'ra' has the iterator function, 'ra + 1' has the state,
1867
           'ra + 2' has the closing variable, and 'ra + 3' has the control
1868
           variable. The call will use the stack starting at 'ra + 3',
1869
           so that it preserves the first three values, and the first
1870
           return will be the new value for the control variable.
1871
        */
1872
435k
        StkId ra = RA(i);
1873
435k
        setobjs2s(L, ra + 5, ra + 3);  /* copy the control variable */
1874
435k
        setobjs2s(L, ra + 4, ra + 1);  /* copy state */
1875
435k
        setobjs2s(L, ra + 3, ra);  /* copy function */
1876
435k
        L->top.p = ra + 3 + 3;
1877
435k
        ProtectNT(luaD_call(L, ra + 3, GETARG_C(i)));  /* do the call */
1878
435k
        updatestack(ci);  /* stack may have changed */
1879
435k
        i = *(pc++);  /* go to next instruction */
1880
435k
        lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
1881
433k
        goto l_tforloop;
1882
435k
      }}
1883
433k
      vmcase(OP_TFORLOOP) {
1884
433k
       l_tforloop: {
1885
433k
        StkId ra = RA(i);
1886
433k
        if (!ttisnil(s2v(ra + 3)))  /* continue loop? */
1887
355k
          pc -= GETARG_Bx(i);  /* jump back */
1888
433k
        vmbreak;
1889
433k
      }}
1890
3.27M
      vmcase(OP_SETLIST) {
1891
3.27M
        StkId ra = RA(i);
1892
3.27M
        unsigned n = cast_uint(GETARG_vB(i));
1893
3.27M
        unsigned int last = cast_uint(GETARG_vC(i));
1894
6.54M
        Table *h = hvalue(s2v(ra));
1895
3.27M
        if (n == 0)
1896
422k
          n = cast_uint(L->top.p - ra) - 1;  /* get up to the top */
1897
2.84M
        else
1898
2.84M
          L->top.p = ci->top.p;  /* correct top in case of emergency GC */
1899
6.54M
        last += n;
1900
6.54M
        if (TESTARG_k(i)) {
1901
75.5k
          last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1);
1902
0
          pc++;
1903
75.5k
        }
1904
        /* when 'n' is known, table should have proper size */
1905
3.27M
        if (last > h->asize) {  /* needs more space? */
1906
          /* fixed-size sets should have space preallocated */
1907
394k
          lua_assert(GETARG_vB(i) == 0);
1908
394k
          luaH_resizearray(L, h, last);  /* preallocate it at once */
1909
394k
        }
1910
44.9M
        for (; n > 0; n--) {
1911
41.6M
          TValue *val = s2v(ra + n);
1912
41.6M
          obj2arr(h, last - 1, val);
1913
41.6M
          last--;
1914
41.6M
          luaC_barrierback(L, obj2gco(h), val);
1915
41.6M
        }
1916
3.27M
        vmbreak;
1917
3.27M
      }
1918
21.6M
      vmcase(OP_CLOSURE) {
1919
21.6M
        StkId ra = RA(i);
1920
21.6M
        Proto *p = cl->p->p[GETARG_Bx(i)];
1921
21.6M
        halfProtect(pushclosure(L, p, cl->upvals, base, ra));
1922
21.6M
        checkGC(L, ra + 1);
1923
21.6M
        vmbreak;
1924
21.6M
      }
1925
4.14M
      vmcase(OP_VARARG) {
1926
4.14M
        StkId ra = RA(i);
1927
4.14M
        int n = GETARG_C(i) - 1;  /* required results */
1928
4.14M
        Protect(luaT_getvarargs(L, ci, ra, n));
1929
4.14M
        vmbreak;
1930
4.14M
      }
1931
17.4M
      vmcase(OP_VARARGPREP) {
1932
17.4M
        ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
1933
17.4M
        if (l_unlikely(trap)) {  /* previous "Protect" updated trap */
1934
1.22M
          luaD_hookcall(L, ci);
1935
1.22M
          L->oldpc = 1;  /* next opcode will be seen as a "new" line */
1936
1.22M
        }
1937
17.4M
        updatebase(ci);  /* function has new base after adjustment */
1938
17.4M
        vmbreak;
1939
17.4M
      }
1940
17.4M
      vmcase(OP_EXTRAARG) {
1941
0
        lua_assert(0);
1942
0
        vmbreak;
1943
0
      }
1944
0
    }
1945
0
  }
1946
710M
}
1947
1948
/* }================================================================== */