Coverage Report

Created: 2026-01-09 06:47

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