Coverage Report

Created: 2025-11-24 06:26

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