Coverage Report

Created: 2023-09-11 06:55

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