Coverage Report

Created: 2024-04-23 06:32

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