Coverage Report

Created: 2023-09-30 06:14

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