Coverage Report

Created: 2024-04-23 06:32

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