Coverage Report

Created: 2026-01-25 07:04

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