Coverage Report

Created: 2025-07-11 06:33

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