Coverage Report

Created: 2026-02-26 07:19

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
119M
#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
2.06M
#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
2.06M
#define MAXINTFITSF ((lua_Unsigned)1 << NBM)
73
74
/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
75
1.03M
#define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
76
77
#else  /* all integers fit in a float precisely */
78
79
#define l_intfitsf(i) 1
80
81
#endif
82
83
84
/*
85
** Try to convert a value from string to a number value.
86
** If the value is not a string or is a string not representing
87
** a valid numeral (or if coercions from strings to numbers
88
** are disabled via macro 'cvt2num'), do not modify 'result'
89
** and return 0.
90
*/
91
2.15M
static int l_strton (const TValue *obj, TValue *result) {
92
2.15M
  lua_assert(obj != result);
93
2.15M
  if (!cvt2num(obj))  /* is object not a string? */
94
1.52M
    return 0;
95
638k
  else {
96
638k
    TString *st = tsvalue(obj);
97
638k
    size_t stlen;
98
638k
    const char *s = getlstr(st, stlen);
99
638k
    return (luaO_str2num(s, result) == stlen + 1);
100
638k
  }
101
2.15M
}
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
957k
int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
109
957k
  TValue v;
110
957k
  if (ttisinteger(obj)) {
111
465k
    *n = cast_num(ivalue(obj));
112
465k
    return 1;
113
465k
  }
114
492k
  else if (l_strton(obj, &v)) {  /* string coercible to number? */
115
340k
    *n = nvalue(&v);  /* convert result of 'luaO_str2num' to a float */
116
340k
    return 1;
117
340k
  }
118
152k
  else
119
152k
    return 0;  /* conversion failed */
120
957k
}
121
122
123
/*
124
** try to convert a float to an integer, rounding according to 'mode'.
125
*/
126
169M
int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
127
169M
  lua_Number f = l_floor(n);
128
169M
  if (n != f) {  /* not an integral value? */
129
10.1M
    if (mode == F2Ieq) return 0;  /* fails if mode demands integral value */
130
127k
    else if (mode == F2Iceil)  /* needs ceiling? */
131
119k
      f += 1;  /* convert floor to ceiling (remember: n != f) */
132
10.1M
  }
133
159M
  return lua_numbertointeger(f, p);
134
169M
}
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
8.04M
int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
143
8.04M
  if (ttisfloat(obj))
144
4.21M
    return luaV_flttointeger(fltvalue(obj), p, mode);
145
3.83M
  else if (ttisinteger(obj)) {
146
3.70M
    *p = ivalue(obj);
147
3.70M
    return 1;
148
3.70M
  }
149
128k
  else
150
128k
    return 0;
151
8.04M
}
152
153
154
/*
155
** try to convert a value to an integer.
156
*/
157
1.66M
int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
158
1.66M
  TValue v;
159
1.66M
  if (l_strton(obj, &v))  /* does 'obj' point to a numerical string? */
160
118k
    obj = &v;  /* change it to point to its corresponding number */
161
1.66M
  return luaV_tointegerns(obj, p, mode);
162
1.66M
}
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.50M
                                   lua_Integer *p, lua_Integer step) {
183
1.50M
  if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
184
    /* not coercible to in integer */
185
23.9k
    lua_Number flim;  /* try to convert to float */
186
23.9k
    if (!tonumber(lim, &flim)) /* cannot convert to float? */
187
2.21k
      luaG_forerror(L, lim, "limit");
188
    /* else 'flim' is a float out of integer bounds */
189
21.7k
    if (luai_numlt(0, flim)) {  /* if it is positive, it is too large */
190
8.23k
      if (step < 0) return 1;  /* initial value must be less than it */
191
2.11k
      *p = LUA_MAXINTEGER;  /* truncate */
192
2.11k
    }
193
13.4k
    else {  /* it is less than min integer */
194
13.4k
      if (step > 0) return 1;  /* initial value must be greater than it */
195
7.20k
      *p = LUA_MININTEGER;  /* truncate */
196
7.20k
    }
197
21.7k
  }
198
1.49M
  return (step > 0 ? init > *p : init < *p);  /* not to run? */
199
1.50M
}
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.89M
static int forprep (lua_State *L, StkId ra) {
215
1.89M
  TValue *pinit = s2v(ra);
216
1.89M
  TValue *plimit = s2v(ra + 1);
217
1.89M
  TValue *pstep = s2v(ra + 2);
218
1.89M
  if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
219
1.51M
    lua_Integer init = ivalue(pinit);
220
1.51M
    lua_Integer step = ivalue(pstep);
221
1.51M
    lua_Integer limit;
222
1.51M
    if (step == 0)
223
2.15k
      luaG_runerror(L, "'for' step is zero");
224
1.50M
    if (forlimit(L, init, plimit, &limit, step))
225
1.18M
      return 1;  /* skip the loop */
226
320k
    else {  /* prepare loop counter */
227
320k
      lua_Unsigned count;
228
320k
      if (step > 0) {  /* ascending loop? */
229
304k
        count = l_castS2U(limit) - l_castS2U(init);
230
304k
        if (step != 1)  /* avoid division in the too common case */
231
4.39k
          count /= l_castS2U(step);
232
304k
      }
233
15.9k
      else {  /* step < 0; descending loop */
234
15.9k
        count = l_castS2U(init) - l_castS2U(limit);
235
        /* 'step+1' avoids negating 'mininteger' */
236
15.9k
        count /= l_castS2U(-(step + 1)) + 1u;
237
15.9k
      }
238
      /* use 'chgivalue' for places that for sure had integers */
239
320k
      chgivalue(s2v(ra), l_castU2S(count));  /* change init to count */
240
318k
      setivalue(s2v(ra + 1), step);  /* change limit to step */
241
318k
      chgivalue(s2v(ra + 2), init);  /* change step to init */
242
318k
    }
243
1.50M
  }
244
384k
  else {  /* try making all values floats */
245
384k
    lua_Number init; lua_Number limit; lua_Number step;
246
384k
    if (l_unlikely(!tonumber(plimit, &limit)))
247
349
      luaG_forerror(L, plimit, "limit");
248
384k
    if (l_unlikely(!tonumber(pstep, &step)))
249
727
      luaG_forerror(L, pstep, "step");
250
383k
    if (l_unlikely(!tonumber(pinit, &init)))
251
1.77k
      luaG_forerror(L, pinit, "initial value");
252
381k
    if (step == 0)
253
870
      luaG_runerror(L, "'for' step is zero");
254
381k
    if (luai_numlt(0, step) ? luai_numlt(limit, init)
255
381k
                            : luai_numlt(init, limit))
256
65.6k
      return 1;  /* skip the loop */
257
315k
    else {
258
      /* make sure all values are floats */
259
315k
      setfltvalue(s2v(ra), limit);
260
315k
      setfltvalue(s2v(ra + 1), step);
261
315k
      setfltvalue(s2v(ra + 2), init);  /* control variable */
262
315k
    }
263
381k
  }
264
634k
  return 0;
265
1.89M
}
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
2.13M
static int floatforloop (StkId ra) {
274
2.13M
  lua_Number step = fltvalue(s2v(ra + 1));
275
2.13M
  lua_Number limit = fltvalue(s2v(ra));
276
2.13M
  lua_Number idx = fltvalue(s2v(ra + 2));  /* control variable */
277
2.13M
  idx = luai_numadd(L, idx, step);  /* increment index */
278
2.13M
  if (luai_numlt(0, step) ? luai_numle(idx, limit)
279
2.13M
                          : luai_numle(limit, idx)) {
280
1.81M
    chgfltvalue(s2v(ra + 2), idx);  /* update control variable */
281
1.81M
    return 1;  /* jump back */
282
1.81M
  }
283
313k
  else
284
313k
    return 0;  /* finish the loop */
285
2.13M
}
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
73.9M
                                      StkId val, lu_byte tag) {
293
73.9M
  int loop;  /* counter to avoid infinite loops */
294
73.9M
  const TValue *tm;  /* metamethod */
295
89.2M
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
296
89.2M
    if (tag == LUA_VNOTABLE) {  /* 't' is not a table? */
297
21.8M
      lua_assert(!ttistable(t));
298
21.8M
      tm = luaT_gettmbyobj(L, t, TM_INDEX);
299
21.8M
      if (l_unlikely(notm(tm)))
300
9.52k
        luaG_typeerror(L, t, "index");  /* no metamethod */
301
      /* else will try the metamethod */
302
21.8M
    }
303
67.3M
    else {  /* 't' is a table */
304
67.3M
      tm = fasttm(L, hvalue(t)->metatable, TM_INDEX);  /* table's metamethod */
305
67.3M
      if (tm == NULL) {  /* no metamethod? */
306
66.6M
        setnilvalue(s2v(val));  /* result is nil */
307
66.6M
        return LUA_VNIL;
308
66.6M
      }
309
      /* else will try the metamethod */
310
67.3M
    }
311
22.5M
    if (ttisfunction(tm)) {  /* is metamethod a function? */
312
5.25M
      tag = luaT_callTMres(L, tm, t, key, val);  /* call it */
313
5.25M
      return tag;  /* return tag of the result */
314
5.25M
    }
315
17.3M
    t = tm;  /* else try to access 'tm[key]' */
316
17.3M
    luaV_fastget(t, key, s2v(val), luaH_get, tag);
317
17.3M
    if (!tagisempty(tag))
318
2.02M
      return tag;  /* done */
319
    /* else repeat (tail call 'luaV_finishget') */
320
17.3M
  }
321
0
  luaG_runerror(L, "'__index' chain too long; possible loop");
322
0
  return 0;  /* to avoid warnings */
323
73.9M
}
324
325
326
/*
327
** Finish a table assignment 't[key] = val'.
328
** About anchoring the table before the call to 'luaH_finishset':
329
** This call may trigger an emergency collection. When loop>0,
330
** the table being accessed is a field in some metatable. If this
331
** metatable is weak and the table is not anchored, this collection
332
** could collect that table while it is being updated.
333
*/
334
void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
335
30.6M
                      TValue *val, int hres) {
336
30.6M
  int loop;  /* counter to avoid infinite loops */
337
30.6M
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
338
30.6M
    const TValue *tm;  /* '__newindex' metamethod */
339
30.6M
    if (hres != HNOTATABLE) {  /* is 't' a table? */
340
54.0M
      Table *h = hvalue(t);  /* save 't' table */
341
54.0M
      tm = fasttm(L, h->metatable, TM_NEWINDEX);  /* get metamethod */
342
54.0M
      if (tm == NULL) {  /* no metamethod? */
343
23.6M
        sethvalue2s(L, L->top.p, h);  /* anchor 't' */
344
23.6M
        L->top.p++;  /* assume EXTRA_STACK */
345
23.6M
        luaH_finishset(L, h, key, val, hres);  /* set new value */
346
23.6M
        L->top.p--;
347
23.6M
        invalidateTMcache(h);
348
23.6M
        luaC_barrierback(L, obj2gco(h), val);
349
23.6M
        return;
350
23.6M
      }
351
      /* else will try the metamethod */
352
54.0M
    }
353
3.64M
    else {  /* not a table; check metamethod */
354
3.64M
      tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
355
3.64M
      if (l_unlikely(notm(tm)))
356
4.26k
        luaG_typeerror(L, t, "index");
357
3.64M
    }
358
    /* try the metamethod */
359
6.99M
    if (ttisfunction(tm)) {
360
6.99M
      luaT_callTM(L, tm, t, key, val);
361
6.99M
      return;
362
6.99M
    }
363
2.44k
    t = tm;  /* else repeat assignment over 'tm' */
364
2.44k
    luaV_fastset(t, key, val, hres, luaH_pset);
365
2.44k
    if (hres == HOK) {
366
349
      luaV_finishfastset(L, t, val);
367
349
      return;  /* done */
368
349
    }
369
    /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
370
2.44k
  }
371
1
  luaG_runerror(L, "'__newindex' chain too long; possible loop");
372
30.6M
}
373
374
375
/*
376
** Function to be used for 0-terminated string order comparison
377
*/
378
#if !defined(l_strcoll)
379
236M
#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
1.03M
static int l_strcmp (const TString *ts1, const TString *ts2) {
392
1.03M
  size_t rl1;  /* real length */
393
1.03M
  const char *s1 = getlstr(ts1, rl1);
394
1.03M
  size_t rl2;
395
1.03M
  const char *s2 = getlstr(ts2, rl2);
396
236M
  for (;;) {  /* for each segment */
397
236M
    int temp = l_strcoll(s1, s2);
398
236M
    if (temp != 0)  /* not equal? */
399
994k
      return temp;  /* done */
400
235M
    else {  /* strings are equal up to a '\0' */
401
235M
      size_t zl1 = strlen(s1);  /* index of first '\0' in 's1' */
402
235M
      size_t zl2 = strlen(s2);  /* index of first '\0' in 's2' */
403
235M
      if (zl2 == rl2)  /* 's2' is finished? */
404
39.8k
        return (zl1 == rl1) ? 0 : 1;  /* check 's1' */
405
234M
      else if (zl1 == rl1)  /* 's1' is finished? */
406
4.51k
        return -1;  /* 's1' is less than 's2' ('s2' is not finished) */
407
      /* both strings longer than 'zl'; go on comparing after the '\0' */
408
234M
      zl1++; zl2++;
409
234M
      s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
410
234M
    }
411
236M
  }
412
1.03M
}
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
639k
l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
427
639k
  if (l_intfitsf(i))
428
430k
    return luai_numlt(cast_num(i), f);  /* compare them as floats */
429
209k
  else {  /* i < f <=> i < ceil(f) */
430
209k
    lua_Integer fi;
431
209k
    if (luaV_flttointeger(f, &fi, F2Iceil))  /* fi = ceil(f) */
432
50.5k
      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
209k
  }
436
639k
}
437
438
439
/*
440
** Check whether integer 'i' is less than or equal to float 'f'.
441
** See comments on previous function.
442
*/
443
91.0k
l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
444
91.0k
  if (l_intfitsf(i))
445
79.8k
    return luai_numle(cast_num(i), f);  /* compare them as floats */
446
11.2k
  else {  /* i <= f <=> i <= floor(f) */
447
11.2k
    lua_Integer fi;
448
11.2k
    if (luaV_flttointeger(f, &fi, F2Ifloor))  /* fi = floor(f) */
449
10.5k
      return i <= fi;   /* compare them as integers */
450
727
    else  /* 'f' is either greater or less than all integers */
451
727
      return f > 0;  /* greater? */
452
11.2k
  }
453
91.0k
}
454
455
456
/*
457
** Check whether float 'f' is less than integer 'i'.
458
** See comments on previous function.
459
*/
460
28.3k
l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
461
28.3k
  if (l_intfitsf(i))
462
25.6k
    return luai_numlt(f, cast_num(i));  /* compare them as floats */
463
2.70k
  else {  /* f < i <=> floor(f) < i */
464
2.70k
    lua_Integer fi;
465
2.70k
    if (luaV_flttointeger(f, &fi, F2Ifloor))  /* fi = floor(f) */
466
1.06k
      return fi < i;   /* compare them as integers */
467
1.63k
    else  /* 'f' is either greater or less than all integers */
468
1.63k
      return f < 0;  /* less? */
469
2.70k
  }
470
28.3k
}
471
472
473
/*
474
** Check whether float 'f' is less than or equal to integer 'i'.
475
** See comments on previous function.
476
*/
477
274k
l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
478
274k
  if (l_intfitsf(i))
479
174k
    return luai_numle(f, cast_num(i));  /* compare them as floats */
480
100k
  else {  /* f <= i <=> ceil(f) <= i */
481
100k
    lua_Integer fi;
482
100k
    if (luaV_flttointeger(f, &fi, F2Iceil))  /* fi = ceil(f) */
483
97.0k
      return fi <= i;   /* compare them as integers */
484
3.56k
    else  /* 'f' is either greater or less than all integers */
485
3.56k
      return f < 0;  /* less? */
486
100k
  }
487
274k
}
488
489
490
/*
491
** Return 'l < r', for numbers.
492
*/
493
1.39M
l_sinline int LTnum (const TValue *l, const TValue *r) {
494
1.39M
  lua_assert(ttisnumber(l) && ttisnumber(r));
495
1.39M
  if (ttisinteger(l)) {
496
640k
    lua_Integer li = ivalue(l);
497
640k
    if (ttisinteger(r))
498
640k
      return li < ivalue(r);  /* both are integers */
499
639k
    else  /* 'l' is int and 'r' is float */
500
639k
      return LTintfloat(li, fltvalue(r));  /* l < r ? */
501
640k
  }
502
756k
  else {
503
756k
    lua_Number lf = fltvalue(l);  /* 'l' must be float */
504
756k
    if (ttisfloat(r))
505
756k
      return luai_numlt(lf, fltvalue(r));  /* both are float */
506
28.3k
    else  /* 'l' is float and 'r' is int */
507
28.3k
      return LTfloatint(lf, ivalue(r));
508
756k
  }
509
1.39M
}
510
511
512
/*
513
** Return 'l <= r', for numbers.
514
*/
515
380k
l_sinline int LEnum (const TValue *l, const TValue *r) {
516
380k
  lua_assert(ttisnumber(l) && ttisnumber(r));
517
380k
  if (ttisinteger(l)) {
518
91.0k
    lua_Integer li = ivalue(l);
519
91.0k
    if (ttisinteger(r))
520
91.0k
      return li <= ivalue(r);  /* both are integers */
521
91.0k
    else  /* 'l' is int and 'r' is float */
522
91.0k
      return LEintfloat(li, fltvalue(r));  /* l <= r ? */
523
91.0k
  }
524
289k
  else {
525
289k
    lua_Number lf = fltvalue(l);  /* 'l' must be float */
526
289k
    if (ttisfloat(r))
527
289k
      return luai_numle(lf, fltvalue(r));  /* both are float */
528
274k
    else  /* 'l' is float and 'r' is int */
529
274k
      return LEfloatint(lf, ivalue(r));
530
289k
  }
531
380k
}
532
533
534
/*
535
** return 'l < r' for non-numbers.
536
*/
537
1.04M
static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
538
1.04M
  lua_assert(!ttisnumber(l) || !ttisnumber(r));
539
1.04M
  if (ttisstring(l) && ttisstring(r))  /* both are strings? */
540
2.06M
    return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
541
13.7k
  else
542
13.7k
    return luaT_callorderTM(L, l, r, TM_LT);
543
1.04M
}
544
545
546
/*
547
** Main operation less than; return 'l < r'.
548
*/
549
1.02M
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
550
1.02M
  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
551
5.28k
    return LTnum(l, r);
552
1.02M
  else return lessthanothers(L, l, r);
553
1.02M
}
554
555
556
/*
557
** return 'l <= r' for non-numbers.
558
*/
559
12.9k
static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
560
12.9k
  lua_assert(!ttisnumber(l) || !ttisnumber(r));
561
12.9k
  if (ttisstring(l) && ttisstring(r))  /* both are strings? */
562
12.6k
    return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
563
6.60k
  else
564
6.60k
    return luaT_callorderTM(L, l, r, TM_LE);
565
12.9k
}
566
567
568
/*
569
** Main operation less than or equal to; return 'l <= r'.
570
*/
571
24
int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
572
24
  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
573
24
    return LEnum(l, r);
574
0
  else return lessequalothers(L, l, r);
575
24
}
576
577
578
/*
579
** Main operation for equality of Lua values; return 't1 == t2'.
580
** L == NULL means raw equality (no metamethods)
581
*/
582
251M
int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
583
251M
  const TValue *tm;
584
251M
  if (ttype(t1) != ttype(t2))  /* not the same type? */
585
23.6M
    return 0;
586
227M
  else if (ttypetag(t1) != ttypetag(t2)) {
587
6.64M
    switch (ttypetag(t1)) {
588
1.34M
      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
1.34M
        lua_Integer i2;
592
1.34M
        return (luaV_flttointeger(fltvalue(t2), &i2, F2Ieq) &&
593
65.7k
                ivalue(t1) == i2);
594
0
      }
595
24.3k
      case LUA_VNUMFLT: {  /* float == integer? */
596
24.3k
        lua_Integer i1;  /* see comment in previous case */
597
24.3k
        return (luaV_flttointeger(fltvalue(t1), &i1, F2Ieq) &&
598
11.8k
                i1 == ivalue(t2));
599
0
      }
600
8.85k
      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
17.7k
        return luaS_eqstr(tsvalue(t1), tsvalue(t2));
605
8.85k
      }
606
5.25M
      default:
607
        /* only numbers (integer/float) and strings (long/short) can have
608
           equal values with different variants */
609
5.25M
        return 0;
610
6.64M
    }
611
6.64M
  }
612
221M
  else {  /* equal variants */
613
221M
    switch (ttypetag(t1)) {
614
205k
      case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
615
205k
        return 1;
616
2.80M
      case LUA_VNUMINT:
617
2.80M
        return (ivalue(t1) == ivalue(t2));
618
3.89M
      case LUA_VNUMFLT:
619
3.89M
        return (fltvalue(t1) == fltvalue(t2));
620
0
      case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
621
163M
      case LUA_VSHRSTR:
622
654M
        return eqshrstr(tsvalue(t1), tsvalue(t2));
623
281k
      case LUA_VLNGSTR:
624
563k
        return luaS_eqstr(tsvalue(t1), tsvalue(t2));
625
8.67k
      case LUA_VUSERDATA: {
626
26.0k
        if (uvalue(t1) == uvalue(t2)) return 1;
627
2.70k
        else if (L == NULL) return 0;
628
2.46k
        tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
629
2.46k
        if (tm == NULL)
630
2.46k
          tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
631
2.46k
        break;  /* will try TM */
632
2.46k
      }
633
2.49M
      case LUA_VTABLE: {
634
7.49M
        if (hvalue(t1) == hvalue(t2)) return 1;
635
14.9k
        else if (L == NULL) return 0;
636
14.8k
        tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
637
14.8k
        if (tm == NULL)
638
12.9k
          tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
639
14.8k
        break;  /* will try TM */
640
14.8k
      }
641
47.9M
      case LUA_VLCF:
642
47.9M
        return (fvalue(t1) == fvalue(t2));
643
27.1k
      default:  /* functions and threads */
644
27.1k
        return (gcvalue(t1) == gcvalue(t2));
645
221M
    }
646
17.3k
    if (tm == NULL)  /* no TM? */
647
13.1k
      return 0;  /* objects are different */
648
4.25k
    else {
649
4.25k
      int tag = luaT_callTMres(L, tm, t1, t2, L->top.p);  /* call TM */
650
4.25k
      return !tagisfalse(tag);
651
4.25k
    }
652
17.3k
  }
653
251M
}
654
655
656
/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
657
#define tostring(L,o)  \
658
30.5M
  (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
62.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
13.9M
static void copy2buff (StkId top, int n, char *buff) {
669
13.9M
  size_t tl = 0;  /* size already copied */
670
29.0M
  do {
671
58.1M
    TString *st = tsvalue(s2v(top - n));
672
58.1M
    size_t l;  /* length of string being copied */
673
58.1M
    const char *s = getlstr(st, l);
674
58.1M
    memcpy(buff + tl, s, l * sizeof(char));
675
58.1M
    tl += l;
676
58.1M
  } while (--n > 0);
677
13.9M
}
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
15.5M
void luaV_concat (lua_State *L, int total) {
685
15.5M
  if (total == 1)
686
6
    return;  /* "all" values already concatenated */
687
15.6M
  do {
688
15.6M
    StkId top = L->top.p;
689
15.6M
    int n = 2;  /* number of elements handled in this pass (at least 2) */
690
15.6M
    if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
691
15.4M
        !tostring(L, s2v(top - 1)))
692
605k
      luaT_tryconcatTM(L);  /* may invalidate 'top' */
693
15.0M
    else if (isemptystr(s2v(top - 1)))  /* second operand is empty? */
694
15.0M
      cast_void(tostring(L, s2v(top - 2)));  /* result is first operand */
695
15.0M
    else if (isemptystr(s2v(top - 2))) {  /* first operand is empty string? */
696
1.02M
      setobjs2s(L, top - 2, top - 1);  /* result is second op. */
697
1.02M
    }
698
13.9M
    else {
699
      /* at least two string values; get as many as possible */
700
13.9M
      size_t tl = tsslen(tsvalue(s2v(top - 1)));  /* total length */
701
13.9M
      TString *ts;
702
      /* collect total length and number of strings */
703
29.0M
      for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
704
15.0M
        size_t l = tsslen(tsvalue(s2v(top - n - 1)));
705
15.0M
        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
15.0M
        tl += l;
710
15.0M
      }
711
13.9M
      if (tl <= LUAI_MAXSHORTLEN) {  /* is result a short string? */
712
9.82M
        char buff[LUAI_MAXSHORTLEN];
713
9.82M
        copy2buff(top, n, buff);  /* copy strings to buffer */
714
9.82M
        ts = luaS_newlstr(L, buff, tl);
715
9.82M
      }
716
4.15M
      else {  /* long string; copy strings directly to final result */
717
4.15M
        ts = luaS_createlngstrobj(L, tl);
718
4.15M
        copy2buff(top, n, getlngstr(ts));
719
4.15M
      }
720
27.9M
      setsvalue2s(L, top - n, ts);  /* create result */
721
13.9M
    }
722
15.6M
    total -= n - 1;  /* got 'n' strings to create one new */
723
15.6M
    L->top.p -= n - 1;  /* popped 'n' strings and pushed one */
724
15.6M
  } while (total > 1);  /* repeat until only 1 result left */
725
15.5M
}
726
727
728
/*
729
** Main operation 'ra = #rb'.
730
*/
731
10.2M
void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
732
10.2M
  const TValue *tm;
733
10.2M
  switch (ttypetag(rb)) {
734
7.58M
    case LUA_VTABLE: {
735
15.1M
      Table *h = hvalue(rb);
736
15.1M
      tm = fasttm(L, h->metatable, TM_LEN);
737
15.1M
      if (tm) break;  /* metamethod? break switch to call it */
738
7.57M
      setivalue(s2v(ra), l_castU2S(luaH_getn(L, h)));  /* else primitive len */
739
7.57M
      return;
740
15.1M
    }
741
2.39M
    case LUA_VSHRSTR: {
742
2.39M
      setivalue(s2v(ra), tsvalue(rb)->shrlen);
743
2.39M
      return;
744
2.39M
    }
745
298k
    case LUA_VLNGSTR: {
746
298k
      setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen));
747
298k
      return;
748
298k
    }
749
17.6k
    default: {  /* try metamethod */
750
17.6k
      tm = luaT_gettmbyobj(L, rb, TM_LEN);
751
17.6k
      if (l_unlikely(notm(tm)))  /* no metamethod? */
752
1.15k
        luaG_typeerror(L, rb, "get length of");
753
16.5k
      break;
754
17.6k
    }
755
10.2M
  }
756
17.1k
  luaT_callTMres(L, tm, rb, rb, ra);
757
17.1k
}
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
658k
lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
767
658k
  if (l_unlikely(l_castS2U(n) + 1u <= 1u)) {  /* special cases: -1 or 0 */
768
43.5k
    if (n == 0)
769
370
      luaG_runerror(L, "attempt to divide by zero");
770
43.2k
    return intop(-, 0, m);   /* n==-1; avoid overflow with 0x80000...//-1 */
771
43.5k
  }
772
615k
  else {
773
615k
    lua_Integer q = m / n;  /* perform C division */
774
615k
    if ((m ^ n) < 0 && m % n != 0)  /* 'm/n' would be negative non-integer? */
775
41.0k
      q -= 1;  /* correct result for different rounding */
776
615k
    return q;
777
615k
  }
778
658k
}
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
571k
lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
787
571k
  if (l_unlikely(l_castS2U(n) + 1u <= 1u)) {  /* special cases: -1 or 0 */
788
69.2k
    if (n == 0)
789
45.2k
      luaG_runerror(L, "attempt to perform 'n%%0'");
790
24.0k
    return 0;   /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
791
69.2k
  }
792
502k
  else {
793
502k
    lua_Integer r = m % n;
794
502k
    if (r != 0 && (r ^ n) < 0)  /* 'm/n' would be non-integer negative? */
795
136k
      r += n;  /* correct result for different rounding */
796
502k
    return r;
797
502k
  }
798
571k
}
799
800
801
/*
802
** Float modulus
803
*/
804
5.30M
lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
805
5.30M
  lua_Number r;
806
5.30M
  luai_nummod(L, m, n, r);
807
5.30M
  return r;
808
5.30M
}
809
810
811
/* number of bits in an integer */
812
981k
#define NBITS l_numbits(lua_Integer)
813
814
815
/*
816
** Shift left operation. (Shift right just negates 'y'.)
817
*/
818
981k
lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
819
981k
  if (y < 0) {  /* shift right? */
820
514k
    if (y <= -NBITS) return 0;
821
76.7k
    else return intop(>>, x, -y);
822
514k
  }
823
467k
  else {  /* shift left */
824
467k
    if (y >= NBITS) return 0;
825
194k
    else return intop(<<, x, y);
826
467k
  }
827
981k
}
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
7.01M
                         StkId ra) {
836
7.01M
  int nup = p->sizeupvalues;
837
7.01M
  Upvaldesc *uv = p->upvalues;
838
7.01M
  int i;
839
7.01M
  LClosure *ncl = luaF_newLclosure(L, nup);
840
7.01M
  ncl->p = p;
841
7.01M
  setclLvalue2s(L, ra, ncl);  /* anchor new closure in stack */
842
14.6M
  for (i = 0; i < nup; i++) {  /* fill in its upvalues */
843
7.59M
    if (uv[i].instack)  /* upvalue refers to local variable? */
844
3.54M
      ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
845
4.05M
    else  /* get upvalue from enclosing function */
846
4.05M
      ncl->upvals[i] = encup[uv[i].idx];
847
7.59M
    luaC_objbarrier(L, ncl, ncl->upvals[i]);
848
7.59M
  }
849
7.01M
}
850
851
852
/*
853
** finish execution of an opcode interrupted by a yield
854
*/
855
14.6k
void luaV_finishOp (lua_State *L) {
856
14.6k
  CallInfo *ci = L->ci;
857
14.6k
  StkId base = ci->func.p + 1;
858
14.6k
  Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
859
14.6k
  OpCode op = GET_OPCODE(inst);
860
14.6k
  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.6k
    default: {
905
      /* only these other opcodes can yield */
906
14.6k
      lua_assert(op == OP_TFORCALL || op == OP_CALL ||
907
14.6k
           op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
908
14.6k
           op == OP_SETI || op == OP_SETFIELD);
909
14.6k
      break;
910
14.6k
    }
911
14.6k
  }
912
14.6k
}
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
8.45M
#define l_lti(a,b)  (a < b)
935
1.08M
#define l_lei(a,b)  (a <= b)
936
7.71M
#define l_gti(a,b)  (a > b)
937
616k
#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
20.1M
#define op_arithI(L,iop,fop) {  \
945
20.1M
  TValue *ra = vRA(i); \
946
20.1M
  TValue *v1 = vRB(i);  \
947
20.1M
  int imm = GETARG_sC(i);  \
948
20.1M
  if (ttisinteger(v1)) {  \
949
14.4M
    lua_Integer iv1 = ivalue(v1);  \
950
14.4M
    pc++; setivalue(ra, iop(L, iv1, imm));  \
951
14.4M
  }  \
952
20.1M
  else if (ttisfloat(v1)) {  \
953
5.53M
    lua_Number nb = fltvalue(v1);  \
954
5.53M
    lua_Number fimm = cast_num(imm);  \
955
5.53M
    pc++; setfltvalue(ra, fop(L, nb, fimm)); \
956
5.53M
  }}
957
958
959
/*
960
** Auxiliary function for arithmetic operations over floats and others
961
** with two operands.
962
*/
963
31.6M
#define op_arithf_aux(L,v1,v2,fop) {  \
964
31.6M
  lua_Number n1; lua_Number n2;  \
965
31.6M
  if (tonumberns(v1, n1) && tonumberns(v2, n2)) {  \
966
30.9M
    StkId ra = RA(i);  \
967
30.9M
    pc++; setfltvalue(s2v(ra), fop(L, n1, n2));  \
968
30.9M
  }}
969
970
971
/*
972
** Arithmetic operations over floats and others with register operands.
973
*/
974
8.93M
#define op_arithf(L,fop) {  \
975
8.93M
  TValue *v1 = vRB(i);  \
976
8.93M
  TValue *v2 = vRC(i);  \
977
8.93M
  op_arithf_aux(L, v1, v2, fop); }
978
979
980
/*
981
** Arithmetic operations with K operands for floats.
982
*/
983
10.0M
#define op_arithfK(L,fop) {  \
984
10.0M
  TValue *v1 = vRB(i);  \
985
10.0M
  TValue *v2 = KC(i); lua_assert(ttisnumber(v2));  \
986
10.0M
  op_arithf_aux(L, v1, v2, fop); }
987
988
989
/*
990
** Arithmetic operations over integers and floats.
991
*/
992
17.8M
#define op_arith_aux(L,v1,v2,iop,fop) {  \
993
17.8M
  if (ttisinteger(v1) && ttisinteger(v2)) {  \
994
5.19M
    StkId ra = RA(i); \
995
5.19M
    lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2);  \
996
5.19M
    pc++; setivalue(s2v(ra), iop(L, i1, i2));  \
997
5.19M
  }  \
998
17.8M
  else op_arithf_aux(L, v1, v2, fop); }
999
1000
1001
/*
1002
** Arithmetic operations with register operands.
1003
*/
1004
6.46M
#define op_arith(L,iop,fop) {  \
1005
6.46M
  TValue *v1 = vRB(i);  \
1006
6.46M
  TValue *v2 = vRC(i);  \
1007
6.46M
  op_arith_aux(L, v1, v2, iop, fop); }
1008
1009
1010
/*
1011
** Arithmetic operations with K operands.
1012
*/
1013
11.3M
#define op_arithK(L,iop,fop) {  \
1014
11.3M
  TValue *v1 = vRB(i);  \
1015
11.3M
  TValue *v2 = KC(i); lua_assert(ttisnumber(v2));  \
1016
11.3M
  op_arith_aux(L, v1, v2, iop, fop); }
1017
1018
1019
/*
1020
** Bitwise operations with constant operand.
1021
*/
1022
2.07M
#define op_bitwiseK(L,op) {  \
1023
2.07M
  TValue *v1 = vRB(i);  \
1024
2.07M
  TValue *v2 = KC(i);  \
1025
2.07M
  lua_Integer i1;  \
1026
2.07M
  lua_Integer i2 = ivalue(v2);  \
1027
2.07M
  if (tointegerns(v1, &i1)) {  \
1028
2.02M
    StkId ra = RA(i); \
1029
2.02M
    pc++; setivalue(s2v(ra), op(i1, i2));  \
1030
2.02M
  }}
1031
1032
1033
/*
1034
** Bitwise operations with register operands.
1035
*/
1036
1.85M
#define op_bitwise(L,op) {  \
1037
1.85M
  TValue *v1 = vRB(i);  \
1038
1.85M
  TValue *v2 = vRC(i);  \
1039
1.85M
  lua_Integer i1; lua_Integer i2;  \
1040
1.85M
  if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) {  \
1041
1.80M
    StkId ra = RA(i); \
1042
1.80M
    pc++; setivalue(s2v(ra), op(i1, i2));  \
1043
1.80M
  }}
1044
1045
1046
/*
1047
** Order operations with register operands. 'opn' actually works
1048
** for all numbers, but the fast track improves performance for
1049
** integers.
1050
*/
1051
2.91M
#define op_order(L,opi,opn,other) {  \
1052
2.91M
  TValue *ra = vRA(i); \
1053
2.91M
  int cond;  \
1054
2.91M
  TValue *rb = vRB(i);  \
1055
2.91M
  if (ttisinteger(ra) && ttisinteger(rb)) {  \
1056
1.10M
    lua_Integer ia = ivalue(ra);  \
1057
1.10M
    lua_Integer ib = ivalue(rb);  \
1058
1.10M
    cond = opi(ia, ib);  \
1059
1.10M
  }  \
1060
2.91M
  else if (ttisnumber(ra) && ttisnumber(rb))  \
1061
1.80M
    cond = opn(ra, rb);  \
1062
1.80M
  else  \
1063
1.80M
    Protect(cond = other(L, ra, rb));  \
1064
2.91M
  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
4.67M
#define op_orderI(L,opi,opf,inv,tm) {  \
1072
4.67M
  TValue *ra = vRA(i); \
1073
4.67M
  int cond;  \
1074
4.67M
  int im = GETARG_sB(i);  \
1075
4.67M
  if (ttisinteger(ra))  \
1076
4.67M
    cond = opi(ivalue(ra), im);  \
1077
4.67M
  else if (ttisfloat(ra)) {  \
1078
227k
    lua_Number fa = fltvalue(ra);  \
1079
227k
    lua_Number fim = cast_num(im);  \
1080
227k
    cond = opf(fa, fim);  \
1081
227k
  }  \
1082
246k
  else {  \
1083
19.4k
    int isf = GETARG_C(i);  \
1084
19.4k
    Protect(cond = luaT_callorderiTM(L, ra, im, inv, isf, tm));  \
1085
19.4k
  }  \
1086
4.67M
  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
687M
#define RA(i) (base+GETARG_A(i))
1103
27.6M
#define vRA(i)  s2v(RA(i))
1104
#define RB(i) (base+GETARG_B(i))
1105
116M
#define vRB(i)  s2v(RB(i))
1106
66.5M
#define KB(i) (k+GETARG_B(i))
1107
#define RC(i) (base+GETARG_C(i))
1108
20.2M
#define vRC(i)  s2v(RC(i))
1109
174M
#define KC(i) (k+GETARG_C(i))
1110
70.7M
#define RKC(i)  ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
1111
1112
1113
1114
227M
#define updatetrap(ci)  (trap = ci->u.l.trap)
1115
1116
91.2M
#define updatebase(ci)  (base = ci->func.p + 1)
1117
1118
1119
#define updatestack(ci)  \
1120
4.49M
  { 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
18.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
15.9M
#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
28.1M
#define docondjump()  if (cond != GETARG_k(i)) pc++; else donextjump(ci);
1139
1140
1141
/*
1142
** Correct global 'pc'.
1143
*/
1144
211M
#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
108M
#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
90.3M
#define Protect(exp)  (savestate(L,ci), (exp), updatetrap(ci))
1159
1160
/* special version that does not change the top */
1161
19.8M
#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
9.55M
#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
31.4M
#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
31.4M
  { luaC_condGC(L, (savepc(ci), L->top.p = (c)), \
1180
31.4M
                         updatetrap(ci)); \
1181
31.4M
           luai_threadyield(L); }
1182
1183
1184
/* fetch an instruction and prepare its execution */
1185
766M
#define vmfetch() { \
1186
766M
  if (l_unlikely(trap)) {  /* stack reallocation or hooks? */ \
1187
88.7M
    trap = luaG_traceexec(L, pc);  /* handle hooks */ \
1188
88.7M
    updatebase(ci);  /* correct stack */ \
1189
88.7M
  } \
1190
766M
  i = *(pc++); \
1191
766M
}
1192
1193
#define vmdispatch(o) switch(o)
1194
#define vmcase(l) case l:
1195
#define vmbreak   break
1196
1197
1198
19.4M
void luaV_execute (lua_State *L, CallInfo *ci) {
1199
19.4M
  LClosure *cl;
1200
19.4M
  TValue *k;
1201
19.4M
  StkId base;
1202
19.4M
  const Instruction *pc;
1203
19.4M
  int trap;
1204
19.4M
#if LUA_USE_JUMPTABLE
1205
19.4M
#include "ljumptab.h"
1206
19.4M
#endif
1207
47.4M
 startfunc:
1208
47.4M
  trap = L->hookmask;
1209
51.8M
 returning:  /* trap already set */
1210
103M
  cl = ci_func(ci);
1211
103M
  k = cl->p->k;
1212
103M
  pc = ci->u.l.savedpc;
1213
103M
  if (l_unlikely(trap))
1214
5.70M
    trap = luaG_tracecall(L);
1215
103M
  base = ci->func.p + 1;
1216
  /* main loop of interpreter */
1217
103M
  for (;;) {
1218
51.8M
    Instruction i;  /* instruction being executed */
1219
51.8M
    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
51.8M
    lua_assert(base == ci->func.p + 1);
1229
51.8M
    lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
1230
    /* for tests, invalidate top for instructions not expecting it */
1231
51.8M
    lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1));
1232
51.8M
    vmdispatch (GET_OPCODE(i)) {
1233
66.4M
      vmcase(OP_MOVE) {
1234
66.4M
        StkId ra = RA(i);
1235
132M
        setobjs2s(L, ra, RB(i));
1236
66.4M
        vmbreak;
1237
66.4M
      }
1238
66.4M
      vmcase(OP_LOADI) {
1239
31.5M
        StkId ra = RA(i);
1240
31.5M
        lua_Integer b = GETARG_sBx(i);
1241
31.5M
        setivalue(s2v(ra), b);
1242
31.5M
        vmbreak;
1243
31.5M
      }
1244
31.5M
      vmcase(OP_LOADF) {
1245
2.20M
        StkId ra = RA(i);
1246
2.20M
        int b = GETARG_sBx(i);
1247
2.20M
        setfltvalue(s2v(ra), cast_num(b));
1248
2.20M
        vmbreak;
1249
2.20M
      }
1250
65.1M
      vmcase(OP_LOADK) {
1251
65.1M
        StkId ra = RA(i);
1252
65.1M
        TValue *rb = k + GETARG_Bx(i);
1253
65.1M
        setobj2s(L, ra, rb);
1254
65.1M
        vmbreak;
1255
65.1M
      }
1256
65.1M
      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
846k
      vmcase(OP_LOADFALSE) {
1264
846k
        StkId ra = RA(i);
1265
846k
        setbfvalue(s2v(ra));
1266
846k
        vmbreak;
1267
846k
      }
1268
2.69M
      vmcase(OP_LFALSESKIP) {
1269
2.69M
        StkId ra = RA(i);
1270
2.69M
        setbfvalue(s2v(ra));
1271
2.69M
        pc++;  /* skip next instruction */
1272
2.69M
        vmbreak;
1273
2.69M
      }
1274
4.62M
      vmcase(OP_LOADTRUE) {
1275
4.62M
        StkId ra = RA(i);
1276
4.62M
        setbtvalue(s2v(ra));
1277
4.62M
        vmbreak;
1278
4.62M
      }
1279
7.37M
      vmcase(OP_LOADNIL) {
1280
7.37M
        StkId ra = RA(i);
1281
7.37M
        int b = GETARG_B(i);
1282
14.1M
        do {
1283
14.1M
          setnilvalue(s2v(ra++));
1284
14.1M
        } while (b--);
1285
7.37M
        vmbreak;
1286
7.37M
      }
1287
47.7M
      vmcase(OP_GETUPVAL) {
1288
47.7M
        StkId ra = RA(i);
1289
47.7M
        int b = GETARG_B(i);
1290
47.7M
        setobj2s(L, ra, cl->upvals[b]->v.p);
1291
47.7M
        vmbreak;
1292
47.7M
      }
1293
47.7M
      vmcase(OP_SETUPVAL) {
1294
1.89M
        StkId ra = RA(i);
1295
1.89M
        UpVal *uv = cl->upvals[GETARG_B(i)];
1296
1.89M
        setobj(L, uv->v.p, s2v(ra));
1297
1.89M
        luaC_barrier(L, uv, s2v(ra));
1298
1.89M
        vmbreak;
1299
1.89M
      }
1300
126M
      vmcase(OP_GETTABUP) {
1301
126M
        StkId ra = RA(i);
1302
126M
        TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
1303
126M
        TValue *rc = KC(i);
1304
252M
        TString *key = tsvalue(rc);  /* key must be a short string */
1305
252M
        lu_byte tag;
1306
252M
        luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag);
1307
252M
        if (tagisempty(tag))
1308
49.7M
          Protect(luaV_finishget(L, upval, rc, ra, tag));
1309
126M
        vmbreak;
1310
126M
      }
1311
126M
      vmcase(OP_GETTABLE) {
1312
3.01M
        StkId ra = RA(i);
1313
3.01M
        TValue *rb = vRB(i);
1314
3.01M
        TValue *rc = vRC(i);
1315
3.01M
        lu_byte tag;
1316
3.01M
        if (ttisinteger(rc)) {  /* fast track for integers? */
1317
1.43M
          luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag);
1318
1.43M
        }
1319
1.57M
        else
1320
1.57M
          luaV_fastget(rb, rc, s2v(ra), luaH_get, tag);
1321
3.01M
        if (tagisempty(tag))
1322
1.45M
          Protect(luaV_finishget(L, rb, rc, ra, tag));
1323
3.01M
        vmbreak;
1324
3.01M
      }
1325
3.01M
      vmcase(OP_GETI) {
1326
2.49M
        StkId ra = RA(i);
1327
2.49M
        TValue *rb = vRB(i);
1328
2.49M
        int c = GETARG_C(i);
1329
2.49M
        lu_byte tag;
1330
2.49M
        luaV_fastgeti(rb, c, s2v(ra), tag);
1331
2.49M
        if (tagisempty(tag)) {
1332
139k
          TValue key;
1333
139k
          setivalue(&key, c);
1334
139k
          Protect(luaV_finishget(L, rb, &key, ra, tag));
1335
139k
        }
1336
2.49M
        vmbreak;
1337
2.49M
      }
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.5M
        TString *key = tsvalue(rc);  /* key must be a short string */
1343
44.5M
        lu_byte tag;
1344
44.5M
        luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag);
1345
44.5M
        if (tagisempty(tag))
1346
4.94M
          Protect(luaV_finishget(L, rb, rc, ra, tag));
1347
22.2M
        vmbreak;
1348
22.2M
      }
1349
44.4M
      vmcase(OP_SETTABUP) {
1350
44.4M
        int hres;
1351
44.4M
        TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
1352
44.4M
        TValue *rb = KB(i);
1353
44.4M
        TValue *rc = RKC(i);
1354
88.9M
        TString *key = tsvalue(rb);  /* key must be a short string */
1355
88.9M
        luaV_fastset(upval, key, rc, hres, luaH_psetshortstr);
1356
88.9M
        if (hres == HOK)
1357
44.4M
          luaV_finishfastset(L, upval, rc);
1358
1.39M
        else
1359
1.39M
          Protect(luaV_finishset(L, upval, rb, rc, hres));
1360
44.4M
        vmbreak;
1361
44.4M
      }
1362
44.4M
      vmcase(OP_SETTABLE) {
1363
15.1M
        StkId ra = RA(i);
1364
15.1M
        int hres;
1365
15.1M
        TValue *rb = vRB(i);  /* key (table is in 'ra') */
1366
15.1M
        TValue *rc = RKC(i);  /* value */
1367
15.1M
        if (ttisinteger(rb)) {  /* fast track for integers? */
1368
11.2M
          luaV_fastseti(s2v(ra), ivalue(rb), rc, hres);
1369
11.2M
        }
1370
3.88M
        else {
1371
3.88M
          luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset);
1372
3.88M
        }
1373
15.1M
        if (hres == HOK)
1374
15.1M
          luaV_finishfastset(L, s2v(ra), rc);
1375
8.34M
        else
1376
8.34M
          Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
1377
15.1M
        vmbreak;
1378
15.1M
      }
1379
15.1M
      vmcase(OP_SETI) {
1380
661k
        StkId ra = RA(i);
1381
661k
        int hres;
1382
661k
        int b = GETARG_B(i);
1383
661k
        TValue *rc = RKC(i);
1384
661k
        luaV_fastseti(s2v(ra), b, rc, hres);
1385
661k
        if (hres == HOK)
1386
661k
          luaV_finishfastset(L, s2v(ra), rc);
1387
134k
        else {
1388
134k
          TValue key;
1389
134k
          setivalue(&key, b);
1390
134k
          Protect(luaV_finishset(L, s2v(ra), &key, rc, hres));
1391
134k
        }
1392
661k
        vmbreak;
1393
661k
      }
1394
10.4M
      vmcase(OP_SETFIELD) {
1395
10.4M
        StkId ra = RA(i);
1396
10.4M
        int hres;
1397
10.4M
        TValue *rb = KB(i);
1398
10.4M
        TValue *rc = RKC(i);
1399
20.8M
        TString *key = tsvalue(rb);  /* key must be a short string */
1400
20.8M
        luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr);
1401
20.8M
        if (hres == HOK)
1402
10.4M
          luaV_finishfastset(L, s2v(ra), rc);
1403
3.07M
        else
1404
3.07M
          Protect(luaV_finishset(L, s2v(ra), rb, rc, hres));
1405
10.4M
        vmbreak;
1406
10.4M
      }
1407
11.1M
      vmcase(OP_NEWTABLE) {
1408
11.1M
        StkId ra = RA(i);
1409
11.1M
        unsigned b = cast_uint(GETARG_vB(i));  /* log2(hash size) + 1 */
1410
11.1M
        unsigned c = cast_uint(GETARG_vC(i));  /* array size */
1411
11.1M
        Table *t;
1412
11.1M
        if (b > 0)
1413
2.82M
          b = 1u << (b - 1);  /* hash size is 2^(b - 1) */
1414
11.1M
        if (TESTARG_k(i)) {  /* non-zero extra argument? */
1415
2.87k
          lua_assert(GETARG_Ax(*pc) != 0);
1416
          /* add it to array size */
1417
2.87k
          c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1);
1418
2.87k
        }
1419
11.1M
        pc++;  /* skip extra argument */
1420
11.1M
        L->top.p = ra + 1;  /* correct top in case of emergency GC */
1421
11.1M
        t = luaH_new(L);  /* memory allocation */
1422
11.1M
        sethvalue2s(L, ra, t);
1423
11.1M
        if (b != 0 || c != 0)
1424
5.79M
          luaH_resize(L, t, c, b);  /* idem */
1425
11.1M
        checkGC(L, ra + 1);
1426
11.1M
        vmbreak;
1427
11.1M
      }
1428
11.1M
      vmcase(OP_SELF) {
1429
2.45M
        StkId ra = RA(i);
1430
2.45M
        lu_byte tag;
1431
2.45M
        TValue *rb = vRB(i);
1432
2.45M
        TValue *rc = KC(i);
1433
4.90M
        TString *key = tsvalue(rc);  /* key must be a short string */
1434
4.90M
        setobj2s(L, ra + 1, rb);
1435
2.45M
        luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag);
1436
2.45M
        if (tagisempty(tag))
1437
2.22M
          Protect(luaV_finishget(L, rb, rc, ra, tag));
1438
2.45M
        vmbreak;
1439
2.45M
      }
1440
20.1M
      vmcase(OP_ADDI) {
1441
40.2M
        op_arithI(L, l_addi, luai_numadd);
1442
20.1M
        vmbreak;
1443
20.1M
      }
1444
20.1M
      vmcase(OP_ADDK) {
1445
13.1M
        op_arithK(L, l_addi, luai_numadd);
1446
13.1M
        vmbreak;
1447
13.1M
      }
1448
13.1M
      vmcase(OP_SUBK) {
1449
6.25M
        op_arithK(L, l_subi, luai_numsub);
1450
6.25M
        vmbreak;
1451
6.25M
      }
1452
6.25M
      vmcase(OP_MULK) {
1453
4.58M
        op_arithK(L, l_muli, luai_nummul);
1454
4.58M
        vmbreak;
1455
4.58M
      }
1456
5.26M
      vmcase(OP_MODK) {
1457
5.26M
        savestate(L, ci);  /* in case of division by 0 */
1458
21.0M
        op_arithK(L, luaV_mod, luaV_modf);
1459
21.0M
        vmbreak;
1460
21.0M
      }
1461
21.0M
      vmcase(OP_POWK) {
1462
9.50M
        op_arithfK(L, luai_numpow);
1463
9.50M
        vmbreak;
1464
9.50M
      }
1465
9.50M
      vmcase(OP_DIVK) {
1466
20.6M
        op_arithfK(L, luai_numdiv);
1467
20.6M
        vmbreak;
1468
20.6M
      }
1469
20.6M
      vmcase(OP_IDIVK) {
1470
108k
        savestate(L, ci);  /* in case of division by 0 */
1471
435k
        op_arithK(L, luaV_idiv, luai_numidiv);
1472
435k
        vmbreak;
1473
435k
      }
1474
465k
      vmcase(OP_BANDK) {
1475
1.39M
        op_bitwiseK(L, l_band);
1476
1.39M
        vmbreak;
1477
1.39M
      }
1478
1.39M
      vmcase(OP_BORK) {
1479
595k
        op_bitwiseK(L, l_bor);
1480
595k
        vmbreak;
1481
595k
      }
1482
1.41M
      vmcase(OP_BXORK) {
1483
4.23M
        op_bitwiseK(L, l_bxor);
1484
4.23M
        vmbreak;
1485
4.23M
      }
1486
4.23M
      vmcase(OP_SHLI) {
1487
77.8k
        StkId ra = RA(i);
1488
77.8k
        TValue *rb = vRB(i);
1489
77.8k
        int ic = GETARG_sC(i);
1490
77.8k
        lua_Integer ib;
1491
77.8k
        if (tointegerns(rb, &ib)) {
1492
70.6k
          pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
1493
70.6k
        }
1494
77.8k
        vmbreak;
1495
77.8k
      }
1496
84.1k
      vmcase(OP_SHRI) {
1497
84.1k
        StkId ra = RA(i);
1498
84.1k
        TValue *rb = vRB(i);
1499
84.1k
        int ic = GETARG_sC(i);
1500
84.1k
        lua_Integer ib;
1501
84.1k
        if (tointegerns(rb, &ib)) {
1502
80.8k
          pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
1503
80.8k
        }
1504
84.1k
        vmbreak;
1505
84.1k
      }
1506
1.57M
      vmcase(OP_ADD) {
1507
4.73M
        op_arith(L, l_addi, luai_numadd);
1508
4.73M
        vmbreak;
1509
4.73M
      }
1510
4.73M
      vmcase(OP_SUB) {
1511
7.33M
        op_arith(L, l_subi, luai_numsub);
1512
7.33M
        vmbreak;
1513
7.33M
      }
1514
7.33M
      vmcase(OP_MUL) {
1515
4.50M
        op_arith(L, l_muli, luai_nummul);
1516
4.50M
        vmbreak;
1517
4.50M
      }
1518
4.50M
      vmcase(OP_MOD) {
1519
376k
        savestate(L, ci);  /* in case of division by 0 */
1520
1.12M
        op_arith(L, luaV_mod, luaV_modf);
1521
1.12M
        vmbreak;
1522
1.12M
      }
1523
4.44M
      vmcase(OP_POW) {
1524
8.88M
        op_arithf(L, luai_numpow);
1525
8.88M
        vmbreak;
1526
8.88M
      }
1527
8.88M
      vmcase(OP_DIV) {  /* float division (always with floats) */
1528
8.98M
        op_arithf(L, luai_numdiv);
1529
8.98M
        vmbreak;
1530
8.98M
      }
1531
8.98M
      vmcase(OP_IDIV) {  /* floor division */
1532
567k
        savestate(L, ci);  /* in case of division by 0 */
1533
1.70M
        op_arith(L, luaV_idiv, luai_numidiv);
1534
1.70M
        vmbreak;
1535
1.70M
      }
1536
1.70M
      vmcase(OP_BAND) {
1537
277k
        op_bitwise(L, l_band);
1538
277k
        vmbreak;
1539
277k
      }
1540
758k
      vmcase(OP_BOR) {
1541
1.51M
        op_bitwise(L, l_bor);
1542
1.51M
        vmbreak;
1543
1.51M
      }
1544
1.51M
      vmcase(OP_BXOR) {
1545
489k
        op_bitwise(L, l_bxor);
1546
489k
        vmbreak;
1547
489k
      }
1548
504k
      vmcase(OP_SHL) {
1549
1.00M
        op_bitwise(L, luaV_shiftl);
1550
1.00M
        vmbreak;
1551
1.00M
      }
1552
1.00M
      vmcase(OP_SHR) {
1553
421k
        op_bitwise(L, luaV_shiftr);
1554
421k
        vmbreak;
1555
421k
      }
1556
637k
      vmcase(OP_MMBIN) {
1557
637k
        StkId ra = RA(i);
1558
637k
        Instruction pi = *(pc - 2);  /* original arith. expression */
1559
637k
        TValue *rb = vRB(i);
1560
637k
        TMS tm = (TMS)GETARG_C(i);
1561
637k
        StkId result = RA(pi);
1562
637k
        lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
1563
637k
        Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
1564
637k
        vmbreak;
1565
637k
      }
1566
637k
      vmcase(OP_MMBINI) {
1567
96.0k
        StkId ra = RA(i);
1568
96.0k
        Instruction pi = *(pc - 2);  /* original arith. expression */
1569
96.0k
        int imm = GETARG_sB(i);
1570
96.0k
        TMS tm = (TMS)GETARG_C(i);
1571
96.0k
        int flip = GETARG_k(i);
1572
96.0k
        StkId result = RA(pi);
1573
96.0k
        Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
1574
96.0k
        vmbreak;
1575
96.0k
      }
1576
190k
      vmcase(OP_MMBINK) {
1577
190k
        StkId ra = RA(i);
1578
190k
        Instruction pi = *(pc - 2);  /* original arith. expression */
1579
190k
        TValue *imm = KB(i);
1580
190k
        TMS tm = (TMS)GETARG_C(i);
1581
190k
        int flip = GETARG_k(i);
1582
190k
        StkId result = RA(pi);
1583
190k
        Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
1584
190k
        vmbreak;
1585
190k
      }
1586
3.03M
      vmcase(OP_UNM) {
1587
3.03M
        StkId ra = RA(i);
1588
3.03M
        TValue *rb = vRB(i);
1589
3.03M
        lua_Number nb;
1590
3.03M
        if (ttisinteger(rb)) {
1591
1.74M
          lua_Integer ib = ivalue(rb);
1592
1.74M
          setivalue(s2v(ra), intop(-, 0, ib));
1593
1.74M
        }
1594
1.29M
        else if (tonumberns(rb, nb)) {
1595
1.25M
          setfltvalue(s2v(ra), luai_numunm(L, nb));
1596
1.25M
        }
1597
38.7k
        else
1598
38.7k
          Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
1599
3.03M
        vmbreak;
1600
3.03M
      }
1601
3.03M
      vmcase(OP_BNOT) {
1602
1.14M
        StkId ra = RA(i);
1603
1.14M
        TValue *rb = vRB(i);
1604
1.14M
        lua_Integer ib;
1605
1.14M
        if (tointegerns(rb, &ib)) {
1606
1.14M
          setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
1607
1.14M
        }
1608
3.28k
        else
1609
3.28k
          Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
1610
1.14M
        vmbreak;
1611
1.14M
      }
1612
1.14M
      vmcase(OP_NOT) {
1613
204k
        StkId ra = RA(i);
1614
204k
        TValue *rb = vRB(i);
1615
204k
        if (l_isfalse(rb))
1616
204k
          setbtvalue(s2v(ra));
1617
11.4k
        else
1618
204k
          setbfvalue(s2v(ra));
1619
204k
        vmbreak;
1620
204k
      }
1621
10.1M
      vmcase(OP_LEN) {
1622
10.1M
        StkId ra = RA(i);
1623
10.1M
        Protect(luaV_objlen(L, ra, vRB(i)));
1624
10.1M
        vmbreak;
1625
10.1M
      }
1626
13.2M
      vmcase(OP_CONCAT) {
1627
13.2M
        StkId ra = RA(i);
1628
13.2M
        int n = GETARG_B(i);  /* number of elements to concatenate */
1629
13.2M
        L->top.p = ra + n;  /* mark the end of concat operands */
1630
13.2M
        ProtectNT(luaV_concat(L, n));
1631
13.2M
        checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */
1632
13.2M
        vmbreak;
1633
13.2M
      }
1634
13.2M
      vmcase(OP_CLOSE) {
1635
2.12M
        StkId ra = RA(i);
1636
2.12M
        lua_assert(!GETARG_B(i));  /* 'close must be alive */
1637
2.12M
        Protect(luaF_close(L, ra, LUA_OK, 1));
1638
2.12M
        vmbreak;
1639
2.12M
      }
1640
2.12M
      vmcase(OP_TBC) {
1641
4.08k
        StkId ra = RA(i);
1642
        /* create new to-be-closed upvalue */
1643
4.08k
        halfProtect(luaF_newtbcupval(L, ra));
1644
4.08k
        vmbreak;
1645
4.08k
      }
1646
2.89M
      vmcase(OP_JMP) {
1647
2.89M
        dojump(ci, i, 0);
1648
2.89M
        vmbreak;
1649
2.89M
      }
1650
2.89M
      vmcase(OP_EQ) {
1651
1.72M
        StkId ra = RA(i);
1652
1.72M
        int cond;
1653
1.72M
        TValue *rb = vRB(i);
1654
1.72M
        Protect(cond = luaV_equalobj(L, s2v(ra), rb));
1655
1.72M
        docondjump();
1656
1.72M
        vmbreak;
1657
1.72M
      }
1658
1.72M
      vmcase(OP_LT) {
1659
4.98M
        op_order(L, l_lti, LTnum, lessthanothers);
1660
4.98M
        vmbreak;
1661
4.98M
      }
1662
4.98M
      vmcase(OP_LE) {
1663
3.74M
        op_order(L, l_lei, LEnum, lessequalothers);
1664
3.74M
        vmbreak;
1665
3.74M
      }
1666
11.5M
      vmcase(OP_EQK) {
1667
11.5M
        StkId ra = RA(i);
1668
11.5M
        TValue *rb = KB(i);
1669
        /* basic types do not use '__eq'; we can use raw equality */
1670
11.5M
        int cond = luaV_rawequalobj(s2v(ra), rb);
1671
11.5M
        docondjump();
1672
11.5M
        vmbreak;
1673
11.5M
      }
1674
11.5M
      vmcase(OP_EQI) {
1675
683k
        StkId ra = RA(i);
1676
683k
        int cond;
1677
683k
        int im = GETARG_sB(i);
1678
683k
        if (ttisinteger(s2v(ra)))
1679
615k
          cond = (ivalue(s2v(ra)) == im);
1680
67.9k
        else if (ttisfloat(s2v(ra)))
1681
25.4k
          cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
1682
42.4k
        else
1683
42.4k
          cond = 0;  /* other types cannot be equal to a number */
1684
1.36M
        docondjump();
1685
1.36M
        vmbreak;
1686
1.36M
      }
1687
2.12M
      vmcase(OP_LTI) {
1688
6.38M
        op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
1689
6.38M
        vmbreak;
1690
6.38M
      }
1691
6.38M
      vmcase(OP_LEI) {
1692
716k
        op_orderI(L, l_lei, luai_numle, 0, TM_LE);
1693
716k
        vmbreak;
1694
716k
      }
1695
2.09M
      vmcase(OP_GTI) {
1696
6.27M
        op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
1697
6.27M
        vmbreak;
1698
6.27M
      }
1699
6.27M
      vmcase(OP_GEI) {
1700
617k
        op_orderI(L, l_gei, luai_numge, 1, TM_LE);
1701
617k
        vmbreak;
1702
617k
      }
1703
6.60M
      vmcase(OP_TEST) {
1704
6.60M
        StkId ra = RA(i);
1705
6.60M
        int cond = !l_isfalse(s2v(ra));
1706
6.60M
        docondjump();
1707
6.60M
        vmbreak;
1708
6.60M
      }
1709
6.60M
      vmcase(OP_TESTSET) {
1710
384k
        StkId ra = RA(i);
1711
384k
        TValue *rb = vRB(i);
1712
384k
        if (l_isfalse(rb) == GETARG_k(i))
1713
368k
          pc++;
1714
15.4k
        else {
1715
15.4k
          setobj2s(L, ra, rb);
1716
15.4k
          donextjump(ci);
1717
15.4k
        }
1718
384k
        vmbreak;
1719
384k
      }
1720
71.4M
      vmcase(OP_CALL) {
1721
71.4M
        StkId ra = RA(i);
1722
71.4M
        CallInfo *newci;
1723
71.4M
        int b = GETARG_B(i);
1724
71.4M
        int nresults = GETARG_C(i) - 1;
1725
71.4M
        if (b != 0)  /* fixed number of arguments? */
1726
69.7M
          L->top.p = ra + b;  /* top signals number of arguments */
1727
        /* else previous instruction set top */
1728
71.4M
        savepc(ci);  /* in case of errors */
1729
71.4M
        if ((newci = luaD_precall(L, ra, nresults)) == NULL)
1730
49.8M
          updatetrap(ci);  /* C call; nothing else to be done */
1731
21.5M
        else {  /* Lua call: run function in this same C frame */
1732
21.5M
          ci = newci;
1733
21.5M
          goto startfunc;
1734
21.5M
        }
1735
71.4M
        vmbreak;
1736
49.8M
      }
1737
7.17M
      vmcase(OP_TAILCALL) {
1738
7.17M
        StkId ra = RA(i);
1739
7.17M
        int b = GETARG_B(i);  /* number of arguments + 1 (function) */
1740
7.17M
        int n;  /* number of results when calling a C function */
1741
7.17M
        int nparams1 = GETARG_C(i);
1742
        /* delta is virtual 'func' - real 'func' (vararg functions) */
1743
7.17M
        int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
1744
7.17M
        if (b != 0)
1745
7.17M
          L->top.p = ra + b;
1746
937
        else  /* previous instruction set top */
1747
937
          b = cast_int(L->top.p - ra);
1748
7.17M
        savepc(ci);  /* several calls here can raise errors */
1749
7.17M
        if (TESTARG_k(i)) {
1750
502k
          luaF_closeupval(L, base);  /* close upvalues from current call */
1751
502k
          lua_assert(L->tbclist.p < base);  /* no pending tbc variables */
1752
502k
          lua_assert(base == ci->func.p + 1);
1753
502k
        }
1754
7.17M
        if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0)  /* Lua function? */
1755
7.13M
          goto startfunc;  /* execute the callee */
1756
42.9k
        else {  /* C function? */
1757
42.9k
          ci->func.p -= delta;  /* restore 'func' (if vararg) */
1758
42.9k
          luaD_poscall(L, ci, n);  /* finish caller */
1759
42.9k
          updatetrap(ci);  /* 'luaD_poscall' can change hooks */
1760
42.9k
          goto ret;  /* caller returns after the tail call */
1761
42.9k
        }
1762
7.17M
      }
1763
599k
      vmcase(OP_RETURN) {
1764
599k
        StkId ra = RA(i);
1765
599k
        int n = GETARG_B(i) - 1;  /* number of results */
1766
599k
        int nparams1 = GETARG_C(i);
1767
599k
        if (n < 0)  /* not fixed? */
1768
1.51k
          n = cast_int(L->top.p - ra);  /* get what is available */
1769
599k
        savepc(ci);
1770
599k
        if (TESTARG_k(i)) {  /* may there be open upvalues? */
1771
138k
          ci->u2.nres = n;  /* save number of returns */
1772
138k
          if (L->top.p < ci->top.p)
1773
8.28k
            L->top.p = ci->top.p;
1774
138k
          luaF_close(L, base, CLOSEKTOP, 1);
1775
138k
          updatetrap(ci);
1776
138k
          updatestack(ci);
1777
138k
        }
1778
599k
        if (nparams1)  /* vararg function? */
1779
460k
          ci->func.p -= ci->u.l.nextraargs + nparams1;
1780
599k
        L->top.p = ra + n;  /* set call for 'luaD_poscall' */
1781
599k
        luaD_poscall(L, ci, n);
1782
599k
        updatetrap(ci);  /* 'luaD_poscall' can change hooks */
1783
599k
        goto ret;
1784
599k
      }
1785
12.6M
      vmcase(OP_RETURN0) {
1786
12.6M
        if (l_unlikely(L->hookmask)) {
1787
4.51M
          StkId ra = RA(i);
1788
4.51M
          L->top.p = ra;
1789
4.51M
          savepc(ci);
1790
4.51M
          luaD_poscall(L, ci, 0);  /* no hurry... */
1791
4.51M
          trap = 1;
1792
4.51M
        }
1793
8.12M
        else {  /* do the 'poscall' here */
1794
8.12M
          int nres = get_nresults(ci->callstatus);
1795
8.12M
          L->ci = ci->previous;  /* back to caller */
1796
8.12M
          L->top.p = base - 1;
1797
8.12M
          for (; l_unlikely(nres > 0); nres--)
1798
8.12M
            setnilvalue(s2v(L->top.p++));  /* all results are nil */
1799
8.12M
        }
1800
12.6M
        goto ret;
1801
599k
      }
1802
9.51M
      vmcase(OP_RETURN1) {
1803
9.51M
        if (l_unlikely(L->hookmask)) {
1804
78.2k
          StkId ra = RA(i);
1805
78.2k
          L->top.p = ra + 1;
1806
78.2k
          savepc(ci);
1807
78.2k
          luaD_poscall(L, ci, 1);  /* no hurry... */
1808
78.2k
          trap = 1;
1809
78.2k
        }
1810
9.43M
        else {  /* do the 'poscall' here */
1811
9.43M
          int nres = get_nresults(ci->callstatus);
1812
9.43M
          L->ci = ci->previous;  /* back to caller */
1813
9.43M
          if (nres == 0)
1814
115k
            L->top.p = base - 1;  /* asked for no results */
1815
9.31M
          else {
1816
9.31M
            StkId ra = RA(i);
1817
9.31M
            setobjs2s(L, base - 1, ra);  /* at least this result */
1818
9.31M
            L->top.p = base;
1819
9.31M
            for (; l_unlikely(nres > 1); nres--)
1820
9.31M
              setnilvalue(s2v(L->top.p++));  /* complete missing results */
1821
9.31M
          }
1822
9.43M
        }
1823
22.7M
       ret:  /* return from a Lua function */
1824
22.7M
        if (ci->callstatus & CIST_FRESH)
1825
18.3M
          return;  /* end this frame */
1826
4.41M
        else {
1827
4.41M
          ci = ci->previous;
1828
4.41M
          goto returning;  /* continue running caller in this frame */
1829
4.41M
        }
1830
22.7M
      }
1831
48.0M
      vmcase(OP_FORLOOP) {
1832
48.0M
        StkId ra = RA(i);
1833
48.0M
        if (ttisinteger(s2v(ra + 1))) {  /* integer loop? */
1834
45.9M
          lua_Unsigned count = l_castS2U(ivalue(s2v(ra)));
1835
45.9M
          if (count > 0) {  /* still more iterations? */
1836
45.6M
            lua_Integer step = ivalue(s2v(ra + 1));
1837
45.6M
            lua_Integer idx = ivalue(s2v(ra + 2));  /* control variable */
1838
45.6M
            chgivalue(s2v(ra), l_castU2S(count - 1));  /* update counter */
1839
45.6M
            idx = intop(+, idx, step);  /* add step to index */
1840
45.6M
            chgivalue(s2v(ra + 2), idx);  /* update control variable */
1841
45.6M
            pc -= GETARG_Bx(i);  /* jump back */
1842
45.6M
          }
1843
45.9M
        }
1844
2.13M
        else if (floatforloop(ra))  /* float loop */
1845
1.81M
          pc -= GETARG_Bx(i);  /* jump back */
1846
48.0M
        updatetrap(ci);  /* allows a signal to break the loop */
1847
48.0M
        vmbreak;
1848
48.0M
      }
1849
48.0M
      vmcase(OP_FORPREP) {
1850
1.89M
        StkId ra = RA(i);
1851
1.89M
        savestate(L, ci);  /* in case of errors */
1852
1.89M
        if (forprep(L, ra))
1853
1.25M
          pc += GETARG_Bx(i) + 1;  /* skip the loop */
1854
1.89M
        vmbreak;
1855
1.89M
      }
1856
2.53M
      vmcase(OP_TFORPREP) {
1857
       /* before: 'ra' has the iterator function, 'ra + 1' has the state,
1858
          'ra + 2' has the initial value for the control variable, and
1859
          'ra + 3' has the closing variable. This opcode then swaps the
1860
          control and the closing variables and marks the closing variable
1861
          as to-be-closed.
1862
       */
1863
2.53M
       StkId ra = RA(i);
1864
2.53M
       TValue temp;  /* to swap control and closing variables */
1865
2.53M
       setobj(L, &temp, s2v(ra + 3));
1866
2.53M
       setobjs2s(L, ra + 3, ra + 2);
1867
2.53M
       setobj2s(L, ra + 2, &temp);
1868
        /* create to-be-closed upvalue (if closing var. is not nil) */
1869
2.53M
        halfProtect(luaF_newtbcupval(L, ra + 2));
1870
2.53M
        pc += GETARG_Bx(i);  /* go to end of the loop */
1871
2.53M
        i = *(pc++);  /* fetch next instruction */
1872
2.53M
        lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
1873
2.53M
        goto l_tforcall;
1874
2.53M
      }
1875
2.53M
      vmcase(OP_TFORCALL) {
1876
4.35M
       l_tforcall: {
1877
        /* 'ra' has the iterator function, 'ra + 1' has the state,
1878
           'ra + 2' has the closing variable, and 'ra + 3' has the control
1879
           variable. The call will use the stack starting at 'ra + 3',
1880
           so that it preserves the first three values, and the first
1881
           return will be the new value for the control variable.
1882
        */
1883
4.35M
        StkId ra = RA(i);
1884
4.35M
        setobjs2s(L, ra + 5, ra + 3);  /* copy the control variable */
1885
4.35M
        setobjs2s(L, ra + 4, ra + 1);  /* copy state */
1886
4.35M
        setobjs2s(L, ra + 3, ra);  /* copy function */
1887
4.35M
        L->top.p = ra + 3 + 3;
1888
4.35M
        ProtectNT(luaD_call(L, ra + 3, GETARG_C(i)));  /* do the call */
1889
4.35M
        updatestack(ci);  /* stack may have changed */
1890
4.35M
        i = *(pc++);  /* go to next instruction */
1891
4.35M
        lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
1892
4.35M
        goto l_tforloop;
1893
4.35M
      }}
1894
4.35M
      vmcase(OP_TFORLOOP) {
1895
4.35M
       l_tforloop: {
1896
4.35M
        StkId ra = RA(i);
1897
4.35M
        if (!ttisnil(s2v(ra + 3)))  /* continue loop? */
1898
3.11M
          pc -= GETARG_Bx(i);  /* jump back */
1899
4.35M
        vmbreak;
1900
4.35M
      }}
1901
5.42M
      vmcase(OP_SETLIST) {
1902
5.42M
        StkId ra = RA(i);
1903
5.42M
        unsigned n = cast_uint(GETARG_vB(i));
1904
5.42M
        unsigned last = cast_uint(GETARG_vC(i));
1905
10.8M
        Table *h = hvalue(s2v(ra));
1906
10.8M
        if (n == 0)
1907
1.06M
          n = cast_uint(L->top.p - ra) - 1;  /* get up to the top */
1908
4.36M
        else
1909
4.36M
          L->top.p = ci->top.p;  /* correct top in case of emergency GC */
1910
10.8M
        last += n;
1911
10.8M
        if (TESTARG_k(i)) {
1912
48.4k
          last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1);
1913
48.4k
          pc++;
1914
48.4k
        }
1915
        /* when 'n' is known, table should have proper size */
1916
10.8M
        if (last > h->asize) {  /* needs more space? */
1917
          /* fixed-size sets should have space preallocated */
1918
1.05M
          lua_assert(GETARG_vB(i) == 0);
1919
1.05M
          luaH_resizearray(L, h, last);  /* preallocate it at once */
1920
1.05M
        }
1921
59.3M
        for (; n > 0; n--) {
1922
53.8M
          TValue *val = s2v(ra + n);
1923
53.8M
          obj2arr(h, last - 1, val);
1924
53.8M
          last--;
1925
53.8M
          luaC_barrierback(L, obj2gco(h), val);
1926
53.8M
        }
1927
5.42M
        vmbreak;
1928
5.42M
      }
1929
7.01M
      vmcase(OP_CLOSURE) {
1930
7.01M
        StkId ra = RA(i);
1931
7.01M
        Proto *p = cl->p->p[GETARG_Bx(i)];
1932
7.01M
        halfProtect(pushclosure(L, p, cl->upvals, base, ra));
1933
7.01M
        checkGC(L, ra + 1);
1934
7.01M
        vmbreak;
1935
7.01M
      }
1936
7.01M
      vmcase(OP_VARARG) {
1937
3.92M
        StkId ra = RA(i);
1938
3.92M
        int n = GETARG_C(i) - 1;  /* required results (-1 means all) */
1939
3.92M
        int vatab = GETARG_k(i) ? GETARG_B(i) : -1;
1940
3.92M
        Protect(luaT_getvarargs(L, ci, ra, n, vatab));
1941
3.92M
        vmbreak;
1942
3.92M
      }
1943
3.92M
      vmcase(OP_GETVARG) {
1944
1.59k
        StkId ra = RA(i);
1945
1.59k
        TValue *rc = vRC(i);
1946
1.59k
        luaT_getvararg(ci, ra, rc);
1947
1.59k
        vmbreak;
1948
1.59k
      }
1949
2.16k
      vmcase(OP_ERRNNIL) {
1950
2.16k
        TValue *ra = vRA(i);
1951
2.16k
        if (!ttisnil(ra))
1952
2.11k
          halfProtect(luaG_errnnil(L, cl, GETARG_Bx(i)));
1953
2.16k
        vmbreak;
1954
54
      }
1955
2.30M
      vmcase(OP_VARARGPREP) {
1956
2.30M
        ProtectNT(luaT_adjustvarargs(L, ci, cl->p));
1957
2.30M
        if (l_unlikely(trap)) {  /* previous "Protect" updated trap */
1958
79.0k
          luaD_hookcall(L, ci);
1959
79.0k
          L->oldpc = 1;  /* next opcode will be seen as a "new" line */
1960
79.0k
        }
1961
2.30M
        updatebase(ci);  /* function has new base after adjustment */
1962
2.30M
        vmbreak;
1963
2.30M
      }
1964
2.30M
      vmcase(OP_EXTRAARG) {
1965
0
        lua_assert(0);
1966
0
        vmbreak;
1967
0
      }
1968
0
    }
1969
0
  }
1970
103M
}
1971
1972
/* }================================================================== */