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/lcode.c
Line
Count
Source
1
/*
2
** $Id: lcode.c $
3
** Code generator for Lua
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lcode_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <float.h>
14
#include <limits.h>
15
#include <math.h>
16
#include <stdlib.h>
17
18
#include "lua.h"
19
20
#include "lcode.h"
21
#include "ldebug.h"
22
#include "ldo.h"
23
#include "lgc.h"
24
#include "llex.h"
25
#include "lmem.h"
26
#include "lobject.h"
27
#include "lopcodes.h"
28
#include "lparser.h"
29
#include "lstring.h"
30
#include "ltable.h"
31
#include "lvm.h"
32
33
34
/* (note that expressions VJMP also have jumps.) */
35
176M
#define hasjumps(e) ((e)->t != (e)->f)
36
37
38
static int codesJ (FuncState *fs, OpCode o, int sj, int k);
39
40
41
42
/* semantic error */
43
1
l_noret luaK_semerror (LexState *ls, const char *fmt, ...) {
44
1
  const char *msg;
45
1
  va_list argp;
46
1
  pushvfstring(ls->L, argp, fmt, msg);
47
1
  ls->t.token = 0;  /* remove "near <token>" from final message */
48
1
  ls->linenumber = ls->lastline;  /* back to line of last used token */
49
1
  luaX_syntaxerror(ls, msg);
50
1
}
51
52
53
/*
54
** If expression is a numeric constant, fills 'v' with its value
55
** and returns 1. Otherwise, returns 0.
56
*/
57
9.90M
static int tonumeral (const expdesc *e, TValue *v) {
58
9.90M
  if (hasjumps(e))
59
5.11k
    return 0;  /* not a numeral */
60
9.90M
  switch (e->k) {
61
673k
    case VKINT:
62
673k
      if (v) setivalue(v, e->u.ival);
63
673k
      return 1;
64
56.7k
    case VKFLT:
65
56.7k
      if (v) setfltvalue(v, e->u.nval);
66
56.7k
      return 1;
67
9.17M
    default: return 0;
68
9.90M
  }
69
9.90M
}
70
71
72
/*
73
** Get the constant value from a constant expression
74
*/
75
6.30M
static TValue *const2val (FuncState *fs, const expdesc *e) {
76
6.30M
  lua_assert(e->k == VCONST);
77
6.30M
  return &fs->ls->dyd->actvar.arr[e->u.info].k;
78
6.30M
}
79
80
81
/*
82
** If expression is a constant, fills 'v' with its value
83
** and returns 1. Otherwise, returns 0.
84
*/
85
423
int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
86
423
  if (hasjumps(e))
87
6
    return 0;  /* not a constant */
88
417
  switch (e->k) {
89
30
    case VFALSE:
90
30
      setbfvalue(v);
91
30
      return 1;
92
0
    case VTRUE:
93
0
      setbtvalue(v);
94
0
      return 1;
95
27
    case VNIL:
96
27
      setnilvalue(v);
97
27
      return 1;
98
0
    case VKSTR: {
99
0
      setsvalue(fs->ls->L, v, e->u.strval);
100
0
      return 1;
101
0
    }
102
54
    case VCONST: {
103
54
      setobj(fs->ls->L, v, const2val(fs, e));
104
54
      return 1;
105
54
    }
106
306
    default: return tonumeral(e, v);
107
417
  }
108
417
}
109
110
111
/*
112
** Return the previous instruction of the current code. If there
113
** may be a jump target between the current instruction and the
114
** previous one, return an invalid instruction (to avoid wrong
115
** optimizations).
116
*/
117
13.4k
static Instruction *previousinstruction (FuncState *fs) {
118
13.4k
  static const Instruction invalidinstruction = ~(Instruction)0;
119
13.4k
  if (fs->pc > fs->lasttarget)
120
5.26k
    return &fs->f->code[fs->pc - 1];  /* previous instruction */
121
8.15k
  else
122
8.15k
    return cast(Instruction*, &invalidinstruction);
123
13.4k
}
124
125
126
/*
127
** Create a OP_LOADNIL instruction, but try to optimize: if the previous
128
** instruction is also OP_LOADNIL and ranges are compatible, adjust
129
** range of previous instruction instead of emitting a new one. (For
130
** instance, 'local a; local b' will generate a single opcode.)
131
*/
132
9.00k
void luaK_nil (FuncState *fs, int from, int n) {
133
9.00k
  int l = from + n - 1;  /* last register to set nil */
134
9.00k
  Instruction *previous = previousinstruction(fs);
135
9.00k
  if (GET_OPCODE(*previous) == OP_LOADNIL) {  /* previous is LOADNIL? */
136
0
    int pfrom = GETARG_A(*previous);  /* get previous range */
137
0
    int pl = pfrom + GETARG_B(*previous);
138
0
    if ((pfrom <= from && from <= pl + 1) ||
139
0
        (from <= pfrom && pfrom <= l + 1)) {  /* can connect both? */
140
0
      if (pfrom < from) from = pfrom;  /* from = min(from, pfrom) */
141
0
      if (pl > l) l = pl;  /* l = max(l, pl) */
142
0
      SETARG_A(*previous, from);
143
0
      SETARG_B(*previous, l - from);
144
0
      return;
145
0
    }  /* else go through */
146
0
  }
147
9.00k
  luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0);  /* else no optimization */
148
9.00k
}
149
150
151
/*
152
** Gets the destination address of a jump instruction. Used to traverse
153
** a list of jumps.
154
*/
155
19.1M
static int getjump (FuncState *fs, int pc) {
156
19.1M
  int offset = GETARG_sJ(fs->f->code[pc]);
157
19.1M
  if (offset == NO_JUMP)  /* point to itself represents end of list */
158
18.0M
    return NO_JUMP;  /* end of list */
159
1.15M
  else
160
1.15M
    return (pc+1)+offset;  /* turn offset into absolute position */
161
19.1M
}
162
163
164
/*
165
** Fix jump instruction at position 'pc' to jump to 'dest'.
166
** (Jump addresses are relative in Lua)
167
*/
168
24.7M
static void fixjump (FuncState *fs, int pc, int dest) {
169
24.7M
  Instruction *jmp = &fs->f->code[pc];
170
24.7M
  int offset = dest - (pc + 1);
171
24.7M
  lua_assert(dest != NO_JUMP);
172
24.7M
  if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
173
0
    luaX_syntaxerror(fs->ls, "control structure too long");
174
24.7M
  lua_assert(GET_OPCODE(*jmp) == OP_JMP);
175
24.7M
  SETARG_sJ(*jmp, offset);
176
24.7M
}
177
178
179
/*
180
** Concatenate jump-list 'l2' into jump-list 'l1'
181
*/
182
18.0M
void luaK_concat (FuncState *fs, int *l1, int l2) {
183
18.0M
  if (l2 == NO_JUMP) return;  /* nothing to concatenate? */
184
18.0M
  else if (*l1 == NO_JUMP)  /* no original list? */
185
18.0M
    *l1 = l2;  /* 'l1' points to 'l2' */
186
25.3k
  else {
187
25.3k
    int list = *l1;
188
25.3k
    int next;
189
1.15M
    while ((next = getjump(fs, list)) != NO_JUMP)  /* find last element */
190
1.13M
      list = next;
191
25.3k
    fixjump(fs, list, l2);  /* last element links to 'l2' */
192
25.3k
  }
193
18.0M
}
194
195
196
/*
197
** Create a jump instruction and return its position, so its destination
198
** can be fixed later (with 'fixjump').
199
*/
200
18.0M
int luaK_jump (FuncState *fs) {
201
18.0M
  return codesJ(fs, OP_JMP, NO_JUMP, 0);
202
18.0M
}
203
204
205
/*
206
** Code a 'return' instruction
207
*/
208
707
void luaK_ret (FuncState *fs, int first, int nret) {
209
707
  OpCode op;
210
707
  switch (nret) {
211
590
    case 0: op = OP_RETURN0; break;
212
115
    case 1: op = OP_RETURN1; break;
213
2
    default: op = OP_RETURN; break;
214
707
  }
215
707
  luaY_checklimit(fs, nret + 1, MAXARG_B, "returns");
216
707
  luaK_codeABC(fs, op, first, nret + 1, 0);
217
707
}
218
219
220
/*
221
** Code a "conditional jump", that is, a test or comparison opcode
222
** followed by a jump. Return jump position.
223
*/
224
18.0M
static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
225
18.0M
  luaK_codeABCk(fs, op, A, B, C, k);
226
18.0M
  return luaK_jump(fs);
227
18.0M
}
228
229
230
/*
231
** returns current 'pc' and marks it as a jump target (to avoid wrong
232
** optimizations with consecutive instructions not in the same basic block).
233
*/
234
71.9M
int luaK_getlabel (FuncState *fs) {
235
71.9M
  fs->lasttarget = fs->pc;
236
71.9M
  return fs->pc;
237
71.9M
}
238
239
240
/*
241
** Returns the position of the instruction "controlling" a given
242
** jump (that is, its condition), or the jump itself if it is
243
** unconditional.
244
*/
245
35.9M
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
246
35.9M
  Instruction *pi = &fs->f->code[pc];
247
35.9M
  if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
248
35.9M
    return pi-1;
249
7.49k
  else
250
7.49k
    return pi;
251
35.9M
}
252
253
254
/*
255
** Patch destination register for a TESTSET instruction.
256
** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
257
** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
258
** register. Otherwise, change instruction to a simple 'TEST' (produces
259
** no register value)
260
*/
261
18.0M
static int patchtestreg (FuncState *fs, int node, int reg) {
262
18.0M
  Instruction *i = getjumpcontrol(fs, node);
263
18.0M
  if (GET_OPCODE(*i) != OP_TESTSET)
264
17.9M
    return 0;  /* cannot patch other instructions */
265
19.8k
  if (reg != NO_REG && reg != GETARG_B(*i))
266
19.8k
    SETARG_A(*i, reg);
267
19.8k
  else {
268
     /* no register to put value or register already has the value;
269
        change instruction to simple test */
270
19.8k
    *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
271
19.8k
  }
272
19.8k
  return 1;
273
19.8k
}
274
275
276
/*
277
** Traverse a list of tests ensuring no one produces a value
278
*/
279
5.23k
static void removevalues (FuncState *fs, int list) {
280
7.66k
  for (; list != NO_JUMP; list = getjump(fs, list))
281
2.43k
      patchtestreg(fs, list, NO_REG);
282
5.23k
}
283
284
285
/*
286
** Traverse a list of tests, patching their destination address and
287
** registers: tests producing values jump to 'vtarget' (and put their
288
** values in 'reg'), other tests jump to 'dtarget'.
289
*/
290
static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
291
53.9M
                          int dtarget) {
292
71.9M
  while (list != NO_JUMP) {
293
18.0M
    int next = getjump(fs, list);
294
18.0M
    if (patchtestreg(fs, list, reg))
295
19.4k
      fixjump(fs, list, vtarget);
296
17.9M
    else
297
17.9M
      fixjump(fs, list, dtarget);  /* jump to default target */
298
18.0M
    list = next;
299
18.0M
  }
300
53.9M
}
301
302
303
/*
304
** Path all jumps in 'list' to jump to 'target'.
305
** (The assert means that we cannot fix a jump to a forward address
306
** because we only know addresses once code is generated.)
307
*/
308
18.0M
void luaK_patchlist (FuncState *fs, int list, int target) {
309
18.0M
  lua_assert(target <= fs->pc);
310
18.0M
  patchlistaux(fs, list, target, NO_REG, target);
311
18.0M
}
312
313
314
18.0M
void luaK_patchtohere (FuncState *fs, int list) {
315
18.0M
  int hr = luaK_getlabel(fs);  /* mark "here" as a jump target */
316
18.0M
  luaK_patchlist(fs, list, hr);
317
18.0M
}
318
319
320
/* limit for difference between lines in relative line info. */
321
242M
#define LIMLINEDIFF 0x80
322
323
324
/*
325
** Save line info for a new instruction. If difference from last line
326
** does not fit in a byte, of after that many instructions, save a new
327
** absolute line info; (in that case, the special value 'ABSLINEINFO'
328
** in 'lineinfo' signals the existence of this absolute information.)
329
** Otherwise, store the difference from last line in 'lineinfo'.
330
*/
331
121M
static void savelineinfo (FuncState *fs, Proto *f, int line) {
332
121M
  int linedif = line - fs->previousline;
333
121M
  int pc = fs->pc - 1;  /* last instruction coded */
334
121M
  if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
335
947k
    luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
336
947k
                    f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
337
947k
    f->abslineinfo[fs->nabslineinfo].pc = pc;
338
947k
    f->abslineinfo[fs->nabslineinfo++].line = line;
339
947k
    linedif = ABSLINEINFO;  /* signal that there is absolute information */
340
947k
    fs->iwthabs = 1;  /* restart counter */
341
947k
  }
342
121M
  luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
343
121M
                  INT_MAX, "opcodes");
344
121M
  f->lineinfo[pc] = cast(ls_byte, linedif);
345
121M
  fs->previousline = line;  /* last line saved */
346
121M
}
347
348
349
/*
350
** Remove line information from the last instruction.
351
** If line information for that instruction is absolute, set 'iwthabs'
352
** above its max to force the new (replacing) instruction to have
353
** absolute line info, too.
354
*/
355
6.69M
static void removelastlineinfo (FuncState *fs) {
356
6.69M
  Proto *f = fs->f;
357
6.69M
  int pc = fs->pc - 1;  /* last instruction coded */
358
6.69M
  if (f->lineinfo[pc] != ABSLINEINFO) {  /* relative line info? */
359
6.63M
    fs->previousline -= f->lineinfo[pc];  /* correct last line saved */
360
6.63M
    fs->iwthabs--;  /* undo previous increment */
361
6.63M
  }
362
52.4k
  else {  /* absolute line information */
363
52.4k
    lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
364
52.4k
    fs->nabslineinfo--;  /* remove it */
365
52.4k
    fs->iwthabs = MAXIWTHABS + 1;  /* force next line info to be absolute */
366
52.4k
  }
367
6.69M
}
368
369
370
/*
371
** Remove the last instruction created, correcting line information
372
** accordingly.
373
*/
374
24
static void removelastinstruction (FuncState *fs) {
375
24
  removelastlineinfo(fs);
376
24
  fs->pc--;
377
24
}
378
379
380
/*
381
** Emit instruction 'i', checking for array sizes and saving also its
382
** line information. Return 'i' position.
383
*/
384
114M
int luaK_code (FuncState *fs, Instruction i) {
385
114M
  Proto *f = fs->f;
386
  /* put new instruction in code array */
387
114M
  luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
388
114M
                  INT_MAX, "opcodes");
389
114M
  f->code[fs->pc++] = i;
390
114M
  savelineinfo(fs, f, fs->ls->lastline);
391
114M
  return fs->pc - 1;  /* index of new instruction */
392
114M
}
393
394
395
/*
396
** Format and emit an 'iABC' instruction. (Assertions check consistency
397
** of parameters versus opcode.)
398
*/
399
87.2M
int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
400
87.2M
  lua_assert(getOpMode(o) == iABC);
401
87.2M
  lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
402
87.2M
             C <= MAXARG_C && (k & ~1) == 0);
403
87.2M
  return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
404
87.2M
}
405
406
407
33.5k
int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
408
33.5k
  lua_assert(getOpMode(o) == ivABC);
409
33.5k
  lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
410
33.5k
             C <= MAXARG_vC && (k & ~1) == 0);
411
33.5k
  return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
412
33.5k
}
413
414
415
/*
416
** Format and emit an 'iABx' instruction.
417
*/
418
7.69M
int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) {
419
7.69M
  lua_assert(getOpMode(o) == iABx);
420
7.69M
  lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
421
7.69M
  return luaK_code(fs, CREATE_ABx(o, A, Bc));
422
7.69M
}
423
424
425
/*
426
** Format and emit an 'iAsBx' instruction.
427
*/
428
181k
static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
429
181k
  int b = Bc + OFFSET_sBx;
430
181k
  lua_assert(getOpMode(o) == iAsBx);
431
181k
  lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
432
181k
  return luaK_code(fs, CREATE_ABx(o, A, b));
433
181k
}
434
435
436
/*
437
** Format and emit an 'isJ' instruction.
438
*/
439
18.0M
static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
440
18.0M
  int j = sj + OFFSET_sJ;
441
18.0M
  lua_assert(getOpMode(o) == isJ);
442
18.0M
  lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
443
18.0M
  return luaK_code(fs, CREATE_sJ(o, j, k));
444
18.0M
}
445
446
447
/*
448
** Emit an "extra argument" instruction (format 'iAx')
449
*/
450
1.30M
static int codeextraarg (FuncState *fs, int A) {
451
1.30M
  lua_assert(A <= MAXARG_Ax);
452
1.30M
  return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
453
1.30M
}
454
455
456
/*
457
** Emit a "load constant" instruction, using either 'OP_LOADK'
458
** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
459
** instruction with "extra argument".
460
*/
461
7.69M
static int luaK_codek (FuncState *fs, int reg, int k) {
462
7.69M
  if (k <= MAXARG_Bx)
463
6.41M
    return luaK_codeABx(fs, OP_LOADK, reg, k);
464
1.27M
  else {
465
1.27M
    int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
466
1.27M
    codeextraarg(fs, k);
467
1.27M
    return p;
468
1.27M
  }
469
7.69M
}
470
471
472
/*
473
** Check register-stack level, keeping track of its maximum size
474
** in field 'maxstacksize'
475
*/
476
55.8M
void luaK_checkstack (FuncState *fs, int n) {
477
55.8M
  int newstack = fs->freereg + n;
478
55.8M
  if (newstack > fs->f->maxstacksize) {
479
8.20k
    luaY_checklimit(fs, newstack, MAX_FSTACK, "registers");
480
8.20k
    fs->f->maxstacksize = cast_byte(newstack);
481
8.20k
  }
482
55.8M
}
483
484
485
/*
486
** Reserve 'n' registers in register stack
487
*/
488
55.8M
void luaK_reserveregs (FuncState *fs, int n) {
489
55.8M
  luaK_checkstack(fs, n);
490
55.8M
  fs->freereg =  cast_byte(fs->freereg + n);
491
55.8M
}
492
493
494
/*
495
** Free register 'reg', if it is neither a constant index nor
496
** a local variable.
497
)
498
*/
499
54.9M
static void freereg (FuncState *fs, int reg) {
500
54.9M
  if (reg >= luaY_nvarstack(fs)) {
501
54.4M
    fs->freereg--;
502
54.4M
    lua_assert(reg == fs->freereg);
503
54.4M
  }
504
54.9M
}
505
506
507
/*
508
** Free two registers in proper order
509
*/
510
24.3M
static void freeregs (FuncState *fs, int r1, int r2) {
511
24.3M
  if (r1 > r2) {
512
17.3M
    freereg(fs, r1);
513
17.3M
    freereg(fs, r2);
514
17.3M
  }
515
6.95M
  else {
516
6.95M
    freereg(fs, r2);
517
6.95M
    freereg(fs, r1);
518
6.95M
  }
519
24.3M
}
520
521
522
/*
523
** Free register used by expression 'e' (if any)
524
*/
525
55.9M
static void freeexp (FuncState *fs, expdesc *e) {
526
55.9M
  if (e->k == VNONRELOC)
527
102k
    freereg(fs, e->u.info);
528
55.9M
}
529
530
531
/*
532
** Free registers used by expressions 'e1' and 'e2' (if any) in proper
533
** order.
534
*/
535
21.2M
static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
536
21.2M
  int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
537
21.2M
  int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
538
21.2M
  freeregs(fs, r1, r2);
539
21.2M
}
540
541
542
/*
543
** Add constant 'v' to prototype's list of constants (field 'k').
544
*/
545
4.63M
static int addk (FuncState *fs, Proto *f, TValue *v) {
546
4.63M
  lua_State *L = fs->ls->L;
547
4.63M
  int oldsize = f->sizek;
548
4.63M
  int k = fs->nk;
549
4.63M
  luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
550
12.1M
  while (oldsize < f->sizek)
551
7.51M
    setnilvalue(&f->k[oldsize++]);
552
4.63M
  setobj(L, &f->k[k], v);
553
4.63M
  fs->nk++;
554
4.63M
  luaC_barrier(L, f, v);
555
4.63M
  return k;
556
4.63M
}
557
558
559
/*
560
** Use scanner's table to cache position of constants in constant list
561
** and try to reuse constants. Because some values should not be used
562
** as keys (nil cannot be a key, integer keys can collapse with float
563
** keys), the caller must provide a useful 'key' for indexing the cache.
564
*/
565
21.7M
static int k2proto (FuncState *fs, TValue *key, TValue *v) {
566
21.7M
  TValue val;
567
21.7M
  Proto *f = fs->f;
568
21.7M
  int tag = luaH_get(fs->kcache, key, &val);  /* query scanner table */
569
21.7M
  if (!tagisempty(tag)) {  /* is there an index there? */
570
21.7M
    int k = cast_int(ivalue(&val));
571
    /* collisions can happen only for float keys */
572
21.7M
    lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v));
573
21.7M
    return k;  /* reuse index */
574
21.7M
  }
575
26.5k
  else {  /* constant not found; create a new entry */
576
26.5k
    int k = addk(fs, f, v);
577
    /* cache it for reuse; numerical value does not need GC barrier;
578
       table is not a metatable, so it does not need to invalidate cache */
579
26.5k
    setivalue(&val, k);
580
26.5k
    luaH_set(fs->ls->L, fs->kcache, key, &val);
581
26.5k
    return k;
582
26.5k
  }
583
21.7M
}
584
585
586
/*
587
** Add a string to list of constants and return its index.
588
*/
589
21.6M
static int stringK (FuncState *fs, TString *s) {
590
21.6M
  TValue o;
591
21.6M
  setsvalue(fs->ls->L, &o, s);
592
21.6M
  return k2proto(fs, &o, &o);  /* use string itself as key */
593
21.6M
}
594
595
596
/*
597
** Add an integer to list of constants and return its index.
598
*/
599
103k
static int luaK_intK (FuncState *fs, lua_Integer n) {
600
103k
  TValue o;
601
103k
  setivalue(&o, n);
602
103k
  return k2proto(fs, &o, &o);  /* use integer itself as key */
603
103k
}
604
605
/*
606
** Add a float to list of constants and return its index. Floats
607
** with integral values need a different key, to avoid collision
608
** with actual integers. To that end, we add to the number its smaller
609
** power-of-two fraction that is still significant in its scale.
610
** (For doubles, the fraction would be 2^-52).
611
** This method is not bulletproof: different numbers may generate the
612
** same key (e.g., very large numbers will overflow to 'inf') and for
613
** floats larger than 2^53 the result is still an integer. For those
614
** cases, just generate a new entry. At worst, this only wastes an entry
615
** with a duplicate.
616
*/
617
4.64M
static int luaK_numberK (FuncState *fs, lua_Number r) {
618
4.64M
  TValue o, kv;
619
4.64M
  setfltvalue(&o, r);  /* value as a TValue */
620
4.64M
  if (r == 0) {  /* handle zero as a special case */
621
846
    setpvalue(&kv, fs);  /* use FuncState as index */
622
846
    return k2proto(fs, &kv, &o);  /* cannot collide */
623
846
  }
624
4.64M
  else {
625
4.64M
    const int nbm = l_floatatt(MANT_DIG);
626
4.64M
    const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
627
4.64M
    const lua_Number k =  r * (1 + q);  /* key */
628
4.64M
    lua_Integer ik;
629
4.64M
    setfltvalue(&kv, k);  /* key as a TValue */
630
4.64M
    if (!luaV_flttointeger(k, &ik, F2Ieq)) {  /* not an integer value? */
631
29.0k
      int n = k2proto(fs, &kv, &o);  /* use key */
632
29.0k
      if (luaV_rawequalobj(&fs->f->k[n], &o))  /* correct value? */
633
29.0k
        return n;
634
29.0k
    }
635
    /* else, either key is still an integer or there was a collision;
636
       anyway, do not try to reuse constant; instead, create a new one */
637
4.61M
    return addk(fs, fs->f, &o);
638
4.64M
  }
639
4.64M
}
640
641
642
/*
643
** Add a false to list of constants and return its index.
644
*/
645
5
static int boolF (FuncState *fs) {
646
5
  TValue o;
647
5
  setbfvalue(&o);
648
5
  return k2proto(fs, &o, &o);  /* use boolean itself as key */
649
5
}
650
651
652
/*
653
** Add a true to list of constants and return its index.
654
*/
655
103
static int boolT (FuncState *fs) {
656
103
  TValue o;
657
103
  setbtvalue(&o);
658
103
  return k2proto(fs, &o, &o);  /* use boolean itself as key */
659
103
}
660
661
662
/*
663
** Add nil to list of constants and return its index.
664
*/
665
0
static int nilK (FuncState *fs) {
666
0
  TValue k, v;
667
0
  setnilvalue(&v);
668
  /* cannot use nil as key; instead use table itself */
669
0
  sethvalue(fs->ls->L, &k, fs->kcache);
670
0
  return k2proto(fs, &k, &v);
671
0
}
672
673
674
/*
675
** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
676
** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
677
** overflows in the hidden addition inside 'int2sC'.
678
*/
679
471k
static int fitsC (lua_Integer i) {
680
471k
  return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
681
471k
}
682
683
684
/*
685
** Check whether 'i' can be stored in an 'sBx' operand.
686
*/
687
4.80M
static int fitsBx (lua_Integer i) {
688
4.80M
  return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
689
4.80M
}
690
691
692
184k
void luaK_int (FuncState *fs, int reg, lua_Integer i) {
693
184k
  if (fitsBx(i))
694
178k
    codeAsBx(fs, OP_LOADI, reg, cast_int(i));
695
6.01k
  else
696
6.01k
    luaK_codek(fs, reg, luaK_intK(fs, i));
697
184k
}
698
699
700
4.62M
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
701
4.62M
  lua_Integer fi;
702
4.62M
  if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
703
2.34k
    codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
704
4.62M
  else
705
4.62M
    luaK_codek(fs, reg, luaK_numberK(fs, f));
706
4.62M
}
707
708
709
/*
710
** Get the value of 'var' in a register and generate an opcode to check
711
** whether that register is nil. 'k' is the index of the variable name
712
** in the list of constants. If its value cannot be encoded in Bx, a 0
713
** will use '?' for the name.
714
*/
715
0
void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) {
716
0
  luaK_exp2anyreg(fs, var);
717
0
  luaK_fixline(fs, line);
718
0
  k = (k >= MAXARG_Bx) ? 0 : k + 1;
719
0
  luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k);
720
0
  luaK_fixline(fs, line);
721
0
  freeexp(fs, var);
722
0
}
723
724
725
/*
726
** Convert a constant in 'v' into an expression description 'e'
727
*/
728
6.30M
static void const2exp (TValue *v, expdesc *e) {
729
6.30M
  switch (ttypetag(v)) {
730
9.20k
    case LUA_VNUMINT:
731
9.20k
      e->k = VKINT; e->u.ival = ivalue(v);
732
9.20k
      break;
733
4.60M
    case LUA_VNUMFLT:
734
4.60M
      e->k = VKFLT; e->u.nval = fltvalue(v);
735
4.60M
      break;
736
1.69M
    case LUA_VFALSE:
737
1.69M
      e->k = VFALSE;
738
1.69M
      break;
739
0
    case LUA_VTRUE:
740
0
      e->k = VTRUE;
741
0
      break;
742
86
    case LUA_VNIL:
743
86
      e->k = VNIL;
744
86
      break;
745
0
    case LUA_VSHRSTR:  case LUA_VLNGSTR:
746
0
      e->k = VKSTR; e->u.strval = tsvalue(v);
747
0
      break;
748
0
    default: lua_assert(0);
749
6.30M
  }
750
6.30M
}
751
752
753
/*
754
** Fix an expression to return the number of results 'nresults'.
755
** 'e' must be a multi-ret expression (function call or vararg).
756
*/
757
356
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
758
356
  Instruction *pc = &getinstruction(fs, e);
759
356
  luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results");
760
356
  if (e->k == VCALL)  /* expression is an open function call? */
761
356
    SETARG_C(*pc, nresults + 1);
762
94
  else {
763
94
    lua_assert(e->k == VVARARG);
764
94
    SETARG_C(*pc, nresults + 1);
765
94
    SETARG_A(*pc, fs->freereg);
766
94
    luaK_reserveregs(fs, 1);
767
94
  }
768
356
}
769
770
771
/*
772
** Convert a VKSTR to a VK
773
*/
774
21.6M
static int str2K (FuncState *fs, expdesc *e) {
775
21.6M
  lua_assert(e->k == VKSTR);
776
21.6M
  e->u.info = stringK(fs, e->u.strval);
777
21.6M
  e->k = VK;
778
21.6M
  return e->u.info;
779
21.6M
}
780
781
782
/*
783
** Fix an expression to return one result.
784
** If expression is not a multi-ret expression (function call or
785
** vararg), it already returns one result, so nothing needs to be done.
786
** Function calls become VNONRELOC expressions (as its result comes
787
** fixed in the base register of the call), while vararg expressions
788
** become VRELOC (as OP_VARARG puts its results where it wants).
789
** (Calls are created returning one result, so that does not need
790
** to be fixed.)
791
*/
792
43.7k
void luaK_setoneret (FuncState *fs, expdesc *e) {
793
43.7k
  if (e->k == VCALL) {  /* expression is an open function call? */
794
    /* already returns 1 value */
795
34.1k
    lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
796
34.1k
    e->k = VNONRELOC;  /* result has fixed position */
797
34.1k
    e->u.info = GETARG_A(getinstruction(fs, e));
798
34.1k
  }
799
9.63k
  else if (e->k == VVARARG) {
800
298
    SETARG_C(getinstruction(fs, e), 2);
801
298
    e->k = VRELOC;  /* can relocate its simple result */
802
298
  }
803
43.7k
}
804
805
/*
806
** Change a vararg parameter into a regular local variable
807
*/
808
0
void luaK_vapar2local (FuncState *fs, expdesc *var) {
809
0
  fs->f->flag |= PF_VATAB;  /* function will need a vararg table */
810
  /* now a vararg parameter is equivalent to a regular local variable */
811
0
  var->k = VLOCAL;
812
0
}
813
814
815
/*
816
** Ensure that expression 'e' is not a variable (nor a <const>).
817
** (Expression still may have jump lists.)
818
*/
819
230M
void luaK_dischargevars (FuncState *fs, expdesc *e) {
820
230M
  switch (e->k) {
821
6.30M
    case VCONST: {
822
6.30M
      const2exp(const2val(fs, e), e);
823
6.30M
      break;
824
0
    }
825
0
    case VVARGVAR: {
826
0
      luaK_vapar2local(fs, e);  /* turn it into a local variable */
827
0
    }  /* FALLTHROUGH */
828
4.46k
    case VLOCAL: {  /* already in a register */
829
4.46k
      int temp = e->u.var.ridx;
830
4.46k
      e->u.info = temp;  /* (can't do a direct assignment; values overlap) */
831
4.46k
      e->k = VNONRELOC;  /* becomes a non-relocatable value */
832
4.46k
      break;
833
0
    }
834
3.31M
    case VUPVAL: {  /* move value to some (pending) register */
835
3.31M
      e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
836
3.31M
      e->k = VRELOC;
837
3.31M
      break;
838
0
    }
839
12.3M
    case VINDEXUP: {
840
12.3M
      e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
841
12.3M
      e->k = VRELOC;
842
12.3M
      break;
843
0
    }
844
12
    case VINDEXI: {
845
12
      freereg(fs, e->u.ind.t);
846
12
      e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
847
12
      e->k = VRELOC;
848
12
      break;
849
0
    }
850
6.21M
    case VINDEXSTR: {
851
6.21M
      freereg(fs, e->u.ind.t);
852
6.21M
      e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
853
6.21M
      e->k = VRELOC;
854
6.21M
      break;
855
0
    }
856
3.03M
    case VINDEXED: {
857
3.03M
      freeregs(fs, e->u.ind.t, e->u.ind.idx);
858
3.03M
      e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
859
3.03M
      e->k = VRELOC;
860
3.03M
      break;
861
0
    }
862
0
    case VVARGIND: {
863
0
      freeregs(fs, e->u.ind.t, e->u.ind.idx);
864
0
      e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx);
865
0
      e->k = VRELOC;
866
0
      break;
867
0
    }
868
33.3k
    case VVARARG: case VCALL: {
869
33.3k
      luaK_setoneret(fs, e);
870
33.3k
      break;
871
298
    }
872
198M
    default: break;  /* there is one value available (somewhere) */
873
230M
  }
874
230M
}
875
876
877
/*
878
** Ensure expression value is in register 'reg', making 'e' a
879
** non-relocatable expression.
880
** (Expression still may have jump lists.)
881
*/
882
55.8M
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
883
55.8M
  luaK_dischargevars(fs, e);
884
55.8M
  switch (e->k) {
885
8.21k
    case VNIL: {
886
8.21k
      luaK_nil(fs, reg, 1);
887
8.21k
      break;
888
0
    }
889
1.69M
    case VFALSE: {
890
1.69M
      luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
891
1.69M
      break;
892
0
    }
893
14
    case VTRUE: {
894
14
      luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
895
14
      break;
896
0
    }
897
28.0k
    case VKSTR: {
898
28.0k
      str2K(fs, e);
899
28.0k
    }  /* FALLTHROUGH */
900
3.06M
    case VK: {
901
3.06M
      luaK_codek(fs, reg, e->u.info);
902
3.06M
      break;
903
28.0k
    }
904
4.62M
    case VKFLT: {
905
4.62M
      luaK_float(fs, reg, e->u.nval);
906
4.62M
      break;
907
28.0k
    }
908
184k
    case VKINT: {
909
184k
      luaK_int(fs, reg, e->u.ival);
910
184k
      break;
911
28.0k
    }
912
28.2M
    case VRELOC: {
913
28.2M
      Instruction *pc = &getinstruction(fs, e);
914
28.2M
      SETARG_A(*pc, reg);  /* instruction will put result in 'reg' */
915
28.2M
      break;
916
28.0k
    }
917
15.8k
    case VNONRELOC: {
918
15.8k
      if (reg != e->u.info)
919
1.27k
        luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
920
15.8k
      break;
921
28.0k
    }
922
17.9M
    default: {
923
17.9M
      lua_assert(e->k == VJMP);
924
17.9M
      return;  /* nothing to do... */
925
17.9M
    }
926
55.8M
  }
927
37.8M
  e->u.info = reg;
928
37.8M
  e->k = VNONRELOC;
929
37.8M
}
930
931
932
/*
933
** Ensure expression value is in a register, making 'e' a
934
** non-relocatable expression.
935
** (Expression still may have jump lists.)
936
*/
937
22.2k
static void discharge2anyreg (FuncState *fs, expdesc *e) {
938
22.2k
  if (e->k != VNONRELOC) {  /* no fixed register yet? */
939
18.6k
    luaK_reserveregs(fs, 1);  /* get a register */
940
18.6k
    discharge2reg(fs, e, fs->freereg-1);  /* put value there */
941
18.6k
  }
942
22.2k
}
943
944
945
35.9M
static int code_loadbool (FuncState *fs, int A, OpCode op) {
946
35.9M
  luaK_getlabel(fs);  /* those instructions may be jump targets */
947
35.9M
  return luaK_codeABC(fs, op, A, 0, 0);
948
35.9M
}
949
950
951
/*
952
** check whether list has any jump that do not produce a value
953
** or produce an inverted value
954
*/
955
17.9M
static int need_value (FuncState *fs, int list) {
956
17.9M
  for (; list != NO_JUMP; list = getjump(fs, list)) {
957
17.9M
    Instruction i = *getjumpcontrol(fs, list);
958
17.9M
    if (GET_OPCODE(i) != OP_TESTSET) return 1;
959
17.9M
  }
960
24.5k
  return 0;  /* not found */
961
17.9M
}
962
963
964
/*
965
** Ensures final expression result (which includes results from its
966
** jump lists) is in register 'reg'.
967
** If expression has jumps, need to patch these jumps either to
968
** its final position or to "load" instructions (for those tests
969
** that do not produce values).
970
*/
971
55.8M
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
972
55.8M
  discharge2reg(fs, e, reg);
973
55.8M
  if (e->k == VJMP)  /* expression itself is a test? */
974
17.9M
    luaK_concat(fs, &e->t, e->u.info);  /* put this jump in 't' list */
975
55.8M
  if (hasjumps(e)) {
976
17.9M
    int final;  /* position after whole expression */
977
17.9M
    int p_f = NO_JUMP;  /* position of an eventual LOAD false */
978
17.9M
    int p_t = NO_JUMP;  /* position of an eventual LOAD true */
979
17.9M
    if (need_value(fs, e->t) || need_value(fs, e->f)) {
980
17.9M
      int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
981
17.9M
      p_f = code_loadbool(fs, reg, OP_LFALSESKIP);  /* skip next inst. */
982
17.9M
      p_t = code_loadbool(fs, reg, OP_LOADTRUE);
983
      /* jump around these booleans if 'e' is not a test */
984
17.9M
      luaK_patchtohere(fs, fj);
985
17.9M
    }
986
17.9M
    final = luaK_getlabel(fs);
987
17.9M
    patchlistaux(fs, e->f, final, reg, p_f);
988
17.9M
    patchlistaux(fs, e->t, final, reg, p_t);
989
17.9M
  }
990
55.8M
  e->f = e->t = NO_JUMP;
991
55.8M
  e->u.info = reg;
992
55.8M
  e->k = VNONRELOC;
993
55.8M
}
994
995
996
/*
997
** Ensures final expression result is in next available register.
998
*/
999
55.8M
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
1000
55.8M
  luaK_dischargevars(fs, e);
1001
55.8M
  freeexp(fs, e);
1002
55.8M
  luaK_reserveregs(fs, 1);
1003
55.8M
  exp2reg(fs, e, fs->freereg - 1);
1004
55.8M
}
1005
1006
1007
/*
1008
** Ensures final expression result is in some (any) register
1009
** and return that register.
1010
*/
1011
75.4M
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
1012
75.4M
  luaK_dischargevars(fs, e);
1013
75.4M
  if (e->k == VNONRELOC) {  /* expression already has a register? */
1014
21.1M
    if (!hasjumps(e))  /* no jumps? */
1015
21.1M
      return e->u.info;  /* result is already in a register */
1016
9.88k
    if (e->u.info >= luaY_nvarstack(fs)) {  /* reg. is not a local? */
1017
9.53k
      exp2reg(fs, e, e->u.info);  /* put final result in it */
1018
9.53k
      return e->u.info;
1019
9.53k
    }
1020
    /* else expression has jumps and cannot change its register
1021
       to hold the jump values, because it is a local variable.
1022
       Go through to the default case. */
1023
9.88k
  }
1024
54.3M
  luaK_exp2nextreg(fs, e);  /* default: use next available register */
1025
54.3M
  return e->u.info;
1026
75.4M
}
1027
1028
1029
/*
1030
** Ensures final expression result is either in a register,
1031
** in an upvalue, or it is the vararg parameter.
1032
*/
1033
21.6M
void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
1034
21.6M
  if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e))
1035
6.32M
    luaK_exp2anyreg(fs, e);
1036
21.6M
}
1037
1038
1039
/*
1040
** Ensures final expression result is either in a register
1041
** or it is a constant.
1042
*/
1043
2.74k
void luaK_exp2val (FuncState *fs, expdesc *e) {
1044
2.74k
  if (e->k == VJMP || hasjumps(e))
1045
2.07k
    luaK_exp2anyreg(fs, e);
1046
672
  else
1047
672
    luaK_dischargevars(fs, e);
1048
2.74k
}
1049
1050
1051
/*
1052
** Try to make 'e' a K expression with an index in the range of R/K
1053
** indices. Return true iff succeeded.
1054
*/
1055
128k
static int luaK_exp2K (FuncState *fs, expdesc *e) {
1056
128k
  if (!hasjumps(e)) {
1057
126k
    int info;
1058
126k
    switch (e->k) {  /* move constants to 'k' */
1059
103
      case VTRUE: info = boolT(fs); break;
1060
5
      case VFALSE: info = boolF(fs); break;
1061
0
      case VNIL: info = nilK(fs); break;
1062
97.6k
      case VKINT: info = luaK_intK(fs, e->u.ival); break;
1063
17.8k
      case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
1064
814
      case VKSTR: info = stringK(fs, e->u.strval); break;
1065
1
      case VK: info = e->u.info; break;
1066
10.0k
      default: return 0;  /* not a constant */
1067
126k
    }
1068
116k
    if (info <= MAXINDEXRK) {  /* does constant fit in 'argC'? */
1069
111k
      e->k = VK;  /* make expression a 'K' expression */
1070
111k
      e->u.info = info;
1071
111k
      return 1;
1072
111k
    }
1073
116k
  }
1074
  /* else, expression doesn't fit; leave it unchanged */
1075
7.05k
  return 0;
1076
128k
}
1077
1078
1079
/*
1080
** Ensures final expression result is in a valid R/K index
1081
** (that is, it is either in a register or in 'k' with an index
1082
** in the range of R/K indices).
1083
** Returns 1 iff expression is K.
1084
*/
1085
16.2k
static int exp2RK (FuncState *fs, expdesc *e) {
1086
16.2k
  if (luaK_exp2K(fs, e))
1087
3.97k
    return 1;
1088
12.2k
  else {  /* not a constant in the right range: put it in a register */
1089
12.2k
    luaK_exp2anyreg(fs, e);
1090
12.2k
    return 0;
1091
12.2k
  }
1092
16.2k
}
1093
1094
1095
static void codeABRK (FuncState *fs, OpCode o, int A, int B,
1096
11.9k
                      expdesc *ec) {
1097
11.9k
  int k = exp2RK(fs, ec);
1098
11.9k
  luaK_codeABCk(fs, o, A, B, ec->u.info, k);
1099
11.9k
}
1100
1101
1102
/*
1103
** Generate code to store result of expression 'ex' into variable 'var'.
1104
*/
1105
16.3k
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
1106
16.3k
  switch (var->k) {
1107
237
    case VLOCAL: {
1108
237
      freeexp(fs, ex);
1109
237
      exp2reg(fs, ex, var->u.var.ridx);  /* compute 'ex' into proper place */
1110
237
      return;
1111
0
    }
1112
4.12k
    case VUPVAL: {
1113
4.12k
      int e = luaK_exp2anyreg(fs, ex);
1114
4.12k
      luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
1115
4.12k
      break;
1116
0
    }
1117
7.85k
    case VINDEXUP: {
1118
7.85k
      codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
1119
7.85k
      break;
1120
0
    }
1121
10
    case VINDEXI: {
1122
10
      codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
1123
10
      break;
1124
0
    }
1125
3.67k
    case VINDEXSTR: {
1126
3.67k
      codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
1127
3.67k
      break;
1128
0
    }
1129
0
    case VVARGIND: {
1130
0
      fs->f->flag |= PF_VATAB;  /* function will need a vararg table */
1131
      /* now, assignment is to a regular table */
1132
0
    }  /* FALLTHROUGH */
1133
461
    case VINDEXED: {
1134
461
      codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
1135
461
      break;
1136
0
    }
1137
0
    default: lua_assert(0);  /* invalid var kind to store */
1138
16.3k
  }
1139
16.1k
  freeexp(fs, ex);
1140
16.1k
}
1141
1142
1143
/*
1144
** Negate condition 'e' (where 'e' is a comparison).
1145
*/
1146
2.75k
static void negatecondition (FuncState *fs, expdesc *e) {
1147
2.75k
  Instruction *pc = getjumpcontrol(fs, e->u.info);
1148
2.75k
  lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
1149
2.75k
                                           GET_OPCODE(*pc) != OP_TEST);
1150
2.75k
  SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
1151
2.75k
}
1152
1153
1154
/*
1155
** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
1156
** is true, code will jump if 'e' is true.) Return jump position.
1157
** Optimize when 'e' is 'not' something, inverting the condition
1158
** and removing the 'not'.
1159
*/
1160
20.3k
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
1161
20.3k
  if (e->k == VRELOC) {
1162
16.2k
    Instruction ie = getinstruction(fs, e);
1163
16.2k
    if (GET_OPCODE(ie) == OP_NOT) {
1164
24
      removelastinstruction(fs);  /* remove previous OP_NOT */
1165
24
      return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
1166
24
    }
1167
    /* else go through */
1168
16.2k
  }
1169
20.3k
  discharge2anyreg(fs, e);
1170
20.3k
  freeexp(fs, e);
1171
20.3k
  return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
1172
20.3k
}
1173
1174
1175
/*
1176
** Emit code to go through if 'e' is true, jump otherwise.
1177
*/
1178
12.4k
void luaK_goiftrue (FuncState *fs, expdesc *e) {
1179
12.4k
  int pc;  /* pc of new jump */
1180
12.4k
  luaK_dischargevars(fs, e);
1181
12.4k
  switch (e->k) {
1182
2.13k
    case VJMP: {  /* condition? */
1183
2.13k
      negatecondition(fs, e);  /* jump when it is false */
1184
2.13k
      pc = e->u.info;  /* save jump position */
1185
2.13k
      break;
1186
0
    }
1187
102
    case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1188
102
      pc = NO_JUMP;  /* always true; do nothing */
1189
102
      break;
1190
102
    }
1191
10.2k
    default: {
1192
10.2k
      pc = jumponcond(fs, e, 0);  /* jump when false */
1193
10.2k
      break;
1194
102
    }
1195
12.4k
  }
1196
12.4k
  luaK_concat(fs, &e->f, pc);  /* insert new jump in false list */
1197
12.4k
  luaK_patchtohere(fs, e->t);  /* true list jumps to here (to go through) */
1198
12.4k
  e->t = NO_JUMP;
1199
12.4k
}
1200
1201
1202
/*
1203
** Emit code to go through if 'e' is false, jump otherwise.
1204
*/
1205
39.4k
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
1206
39.4k
  int pc;  /* pc of new jump */
1207
39.4k
  luaK_dischargevars(fs, e);
1208
39.4k
  switch (e->k) {
1209
29.2k
    case VJMP: {
1210
29.2k
      pc = e->u.info;  /* already jump if true */
1211
29.2k
      break;
1212
0
    }
1213
0
    case VNIL: case VFALSE: {
1214
0
      pc = NO_JUMP;  /* always false; do nothing */
1215
0
      break;
1216
0
    }
1217
10.1k
    default: {
1218
10.1k
      pc = jumponcond(fs, e, 1);  /* jump if true */
1219
10.1k
      break;
1220
0
    }
1221
39.4k
  }
1222
39.4k
  luaK_concat(fs, &e->t, pc);  /* insert new jump in 't' list */
1223
39.4k
  luaK_patchtohere(fs, e->f);  /* false list jumps to here (to go through) */
1224
39.4k
  e->f = NO_JUMP;
1225
39.4k
}
1226
1227
1228
/*
1229
** Code 'not e', doing constant folding.
1230
*/
1231
2.61k
static void codenot (FuncState *fs, expdesc *e) {
1232
2.61k
  switch (e->k) {
1233
0
    case VNIL: case VFALSE: {
1234
0
      e->k = VTRUE;  /* true == not nil == not false */
1235
0
      break;
1236
0
    }
1237
45
    case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1238
45
      e->k = VFALSE;  /* false == not "x" == not 0.5 == not 1 == not true */
1239
45
      break;
1240
15
    }
1241
615
    case VJMP: {
1242
615
      negatecondition(fs, e);
1243
615
      break;
1244
15
    }
1245
525
    case VRELOC:
1246
1.95k
    case VNONRELOC: {
1247
1.95k
      discharge2anyreg(fs, e);
1248
1.95k
      freeexp(fs, e);
1249
1.95k
      e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
1250
1.95k
      e->k = VRELOC;
1251
1.95k
      break;
1252
525
    }
1253
0
    default: lua_assert(0);  /* cannot happen */
1254
2.61k
  }
1255
  /* interchange true and false lists */
1256
2.61k
  { int temp = e->f; e->f = e->t; e->t = temp; }
1257
2.61k
  removevalues(fs, e->f);  /* values are useless when negated */
1258
2.61k
  removevalues(fs, e->t);
1259
2.61k
}
1260
1261
1262
/*
1263
** Check whether expression 'e' is a short literal string
1264
*/
1265
36.9M
static int isKstr (FuncState *fs, expdesc *e) {
1266
36.9M
  return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK &&
1267
36.9M
          ttisshrstring(&fs->f->k[e->u.info]));
1268
36.9M
}
1269
1270
/*
1271
** Check whether expression 'e' is a literal integer.
1272
*/
1273
3.46M
static int isKint (expdesc *e) {
1274
3.46M
  return (e->k == VKINT && !hasjumps(e));
1275
3.46M
}
1276
1277
1278
/*
1279
** Check whether expression 'e' is a literal integer in
1280
** proper range to fit in register C
1281
*/
1282
3.03M
static int isCint (expdesc *e) {
1283
3.03M
  return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1284
3.03M
}
1285
1286
1287
/*
1288
** Check whether expression 'e' is a literal integer in
1289
** proper range to fit in register sC
1290
*/
1291
343k
static int isSCint (expdesc *e) {
1292
343k
  return isKint(e) && fitsC(e->u.ival);
1293
343k
}
1294
1295
1296
/*
1297
** Check whether expression 'e' is a literal integer or float in
1298
** proper range to fit in a register (sB or sC).
1299
*/
1300
53.9M
static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
1301
53.9M
  lua_Integer i;
1302
53.9M
  if (e->k == VKINT)
1303
322k
    i = e->u.ival;
1304
53.6M
  else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
1305
3.74k
    *isfloat = 1;
1306
53.6M
  else
1307
53.6M
    return 0;  /* not a number */
1308
326k
  if (!hasjumps(e) && fitsC(i)) {
1309
317k
    *pi = int2sC(cast_int(i));
1310
317k
    return 1;
1311
317k
  }
1312
9.53k
  else
1313
9.53k
    return 0;
1314
326k
}
1315
1316
1317
/*
1318
** Emit SELF instruction or equivalent: the code will convert
1319
** expression 'e' into 'e.key(e,'.
1320
*/
1321
461
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
1322
461
  int ereg, base;
1323
461
  luaK_exp2anyreg(fs, e);
1324
461
  ereg = e->u.info;  /* register where 'e' (the receiver) was placed */
1325
461
  freeexp(fs, e);
1326
461
  base = e->u.info = fs->freereg;  /* base register for op_self */
1327
461
  e->k = VNONRELOC;  /* self expression has a fixed register */
1328
461
  luaK_reserveregs(fs, 2);  /* method and 'self' produced by op_self */
1329
461
  lua_assert(key->k == VKSTR);
1330
  /* is method name a short string in a valid K index? */
1331
461
  if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) {
1332
    /* can use 'self' opcode */
1333
461
    luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0);
1334
461
  }
1335
0
  else {  /* cannot use 'self' opcode; use move+gettable */
1336
0
    luaK_exp2anyreg(fs, key);  /* put method name in a register */
1337
0
    luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0);  /* copy self to base+1 */
1338
0
    luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info);  /* get method */
1339
0
  }
1340
461
  freeexp(fs, key);
1341
461
}
1342
1343
1344
/* auxiliary function to define indexing expressions */
1345
21.6M
static void fillidxk (expdesc *t, int idx, expkind k) {
1346
21.6M
  t->u.ind.idx = cast_byte(idx);
1347
21.6M
  t->k = k;
1348
21.6M
}
1349
1350
1351
/*
1352
** Create expression 't[k]'. 't' must have its final result already in a
1353
** register or upvalue. Upvalues can only be indexed by literal strings.
1354
** Keys can be literal strings in the constant table or arbitrary
1355
** values in registers.
1356
*/
1357
21.6M
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1358
21.6M
  int keystr = -1;
1359
21.6M
  if (k->k == VKSTR)
1360
21.6M
    keystr = str2K(fs, k);
1361
21.6M
  lua_assert(!hasjumps(t) &&
1362
21.6M
             (t->k == VLOCAL || t->k == VVARGVAR ||
1363
21.6M
              t->k == VNONRELOC || t->k == VUPVAL));
1364
21.6M
  if (t->k == VUPVAL && !isKstr(fs, k))  /* upvalue indexed by non 'Kstr'? */
1365
2.93M
    luaK_exp2anyreg(fs, t);  /* put it in a register */
1366
21.6M
  if (t->k == VUPVAL) {
1367
12.3M
    lu_byte temp = cast_byte(t->u.info);  /* upvalue index */
1368
12.3M
    t->u.ind.t = temp;  /* (can't do a direct assignment; values overlap) */
1369
12.3M
    lua_assert(isKstr(fs, k));
1370
12.3M
    fillidxk(t, k->u.info, VINDEXUP);  /* literal short string */
1371
12.3M
  }
1372
9.25M
  else if (t->k == VVARGVAR) {  /* indexing the vararg parameter? */
1373
0
    lua_assert(t->u.ind.t == fs->f->numparams);
1374
0
    t->u.ind.t = cast_byte(t->u.var.ridx);
1375
0
    fillidxk(t, luaK_exp2anyreg(fs, k), VVARGIND);  /* register */
1376
0
  }
1377
9.25M
  else {
1378
    /* register index of the table */
1379
9.25M
    t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
1380
9.25M
    if (isKstr(fs, k))
1381
6.21M
      fillidxk(t, k->u.info, VINDEXSTR);  /* literal short string */
1382
3.03M
    else if (isCint(k))  /* int. constant in proper range? */
1383
22
      fillidxk(t, cast_int(k->u.ival), VINDEXI);
1384
3.03M
    else
1385
3.03M
      fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED);  /* register */
1386
9.25M
  }
1387
21.6M
  t->u.ind.keystr = keystr;  /* string index in 'k' */
1388
21.6M
  t->u.ind.ro = 0;  /* by default, not read-only */
1389
21.6M
}
1390
1391
1392
/*
1393
** Return false if folding can raise an error.
1394
** Bitwise operations need operands convertible to integers; division
1395
** operations cannot have 0 as divisor.
1396
*/
1397
146k
static int validop (int op, TValue *v1, TValue *v2) {
1398
146k
  switch (op) {
1399
4.59k
    case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
1400
33.2k
    case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: {  /* conversion errors */
1401
33.2k
      lua_Integer i;
1402
33.2k
      return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
1403
31.1k
              luaV_tointegerns(v2, &i, LUA_FLOORN2I));
1404
8.24k
    }
1405
14.8k
    case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD:  /* division by 0 */
1406
14.8k
      return (nvalue(v2) != 0);
1407
98.0k
    default: return 1;  /* everything else is valid */
1408
146k
  }
1409
146k
}
1410
1411
1412
/*
1413
** Try to "constant-fold" an operation; return 1 iff successful.
1414
** (In this case, 'e1' has the final result.)
1415
*/
1416
static int constfolding (FuncState *fs, int op, expdesc *e1,
1417
3.42M
                                        const expdesc *e2) {
1418
3.42M
  TValue v1, v2, res;
1419
3.42M
  if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
1420
3.28M
    return 0;  /* non-numeric operands or not safe to fold */
1421
143k
  luaO_rawarith(fs->ls->L, op, &v1, &v2, &res);  /* does operation */
1422
143k
  if (ttisinteger(&res)) {
1423
45.1k
    e1->k = VKINT;
1424
45.1k
    e1->u.ival = ivalue(&res);
1425
45.1k
  }
1426
98.1k
  else {  /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
1427
98.1k
    lua_Number n = fltvalue(&res);
1428
98.1k
    if (luai_numisnan(n) || n == 0)
1429
63.5k
      return 0;
1430
34.5k
    e1->k = VKFLT;
1431
34.5k
    e1->u.nval = n;
1432
34.5k
  }
1433
79.6k
  return 1;
1434
143k
}
1435
1436
1437
/*
1438
** Convert a BinOpr to an OpCode  (ORDER OPR - ORDER OP)
1439
*/
1440
21.1M
l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1441
21.1M
  lua_assert(baser <= opr &&
1442
21.1M
            ((baser == OPR_ADD && opr <= OPR_SHR) ||
1443
21.1M
             (baser == OPR_LT && opr <= OPR_LE)));
1444
21.1M
  return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1445
21.1M
}
1446
1447
1448
/*
1449
** Convert a UnOpr to an OpCode  (ORDER OPR - ORDER OP)
1450
*/
1451
55.8k
l_sinline OpCode unopr2op (UnOpr opr) {
1452
55.8k
  return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1453
55.8k
                                       cast_int(OP_UNM));
1454
55.8k
}
1455
1456
1457
/*
1458
** Convert a BinOpr to a tag method  (ORDER OPR - ORDER TM)
1459
*/
1460
3.17M
l_sinline TMS binopr2TM (BinOpr opr) {
1461
3.17M
  lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1462
3.17M
  return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1463
3.17M
}
1464
1465
1466
/*
1467
** Emit code for unary expressions that "produce values"
1468
** (everything but 'not').
1469
** Expression to produce final result will be encoded in 'e'.
1470
*/
1471
55.8k
static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1472
55.8k
  int r = luaK_exp2anyreg(fs, e);  /* opcodes operate only on registers */
1473
55.8k
  freeexp(fs, e);
1474
55.8k
  e->u.info = luaK_codeABC(fs, op, 0, r, 0);  /* generate opcode */
1475
55.8k
  e->k = VRELOC;  /* all those operations are relocatable */
1476
55.8k
  luaK_fixline(fs, line);
1477
55.8k
}
1478
1479
1480
/*
1481
** Emit code for binary expressions that "produce values"
1482
** (everything but logical operators 'and'/'or' and comparison
1483
** operators).
1484
** Expression to produce final result will be encoded in 'e1'.
1485
*/
1486
static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
1487
                             OpCode op, int v2, int flip, int line,
1488
3.29M
                             OpCode mmop, TMS event) {
1489
3.29M
  int v1 = luaK_exp2anyreg(fs, e1);
1490
3.29M
  int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
1491
3.29M
  freeexps(fs, e1, e2);
1492
3.29M
  e1->u.info = pc;
1493
3.29M
  e1->k = VRELOC;  /* all those operations are relocatable */
1494
3.29M
  luaK_fixline(fs, line);
1495
3.29M
  luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip);  /* metamethod */
1496
3.29M
  luaK_fixline(fs, line);
1497
3.29M
}
1498
1499
1500
/*
1501
** Emit code for binary expressions that "produce values" over
1502
** two registers.
1503
*/
1504
static void codebinexpval (FuncState *fs, BinOpr opr,
1505
3.06M
                           expdesc *e1, expdesc *e2, int line) {
1506
3.06M
  OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1507
3.06M
  int v2 = luaK_exp2anyreg(fs, e2);  /* make sure 'e2' is in a register */
1508
  /* 'e1' must be already in a register or it is a constant */
1509
3.06M
  lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1510
3.06M
             e1->k == VNONRELOC || e1->k == VRELOC);
1511
3.06M
  lua_assert(OP_ADD <= op && op <= OP_SHR);
1512
3.06M
  finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1513
3.06M
}
1514
1515
1516
/*
1517
** Code binary operators with immediate operands.
1518
*/
1519
static void codebini (FuncState *fs, OpCode op,
1520
                       expdesc *e1, expdesc *e2, int flip, int line,
1521
92.4k
                       TMS event) {
1522
92.4k
  int v2 = int2sC(cast_int(e2->u.ival));  /* immediate operand */
1523
92.4k
  lua_assert(e2->k == VKINT);
1524
92.4k
  finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
1525
92.4k
}
1526
1527
1528
/*
1529
** Code binary operators with K operand.
1530
*/
1531
static void codebinK (FuncState *fs, BinOpr opr,
1532
107k
                      expdesc *e1, expdesc *e2, int flip, int line) {
1533
107k
  TMS event = binopr2TM(opr);
1534
107k
  int v2 = e2->u.info;  /* K index */
1535
107k
  OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1536
107k
  finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1537
107k
}
1538
1539
1540
/* Try to code a binary operator negating its second operand.
1541
** For the metamethod, 2nd operand must keep its original value.
1542
*/
1543
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
1544
79.2k
                             OpCode op, int line, TMS event) {
1545
79.2k
  if (!isKint(e2))
1546
51.7k
    return 0;  /* not an integer constant */
1547
27.4k
  else {
1548
27.4k
    lua_Integer i2 = e2->u.ival;
1549
27.4k
    if (!(fitsC(i2) && fitsC(-i2)))
1550
283
      return 0;  /* not in the proper range */
1551
27.1k
    else {  /* operating a small integer constant */
1552
27.1k
      int v2 = cast_int(i2);
1553
27.1k
      finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
1554
      /* correct metamethod argument */
1555
27.1k
      SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
1556
27.1k
      return 1;  /* successfully coded */
1557
27.1k
    }
1558
27.4k
  }
1559
79.2k
}
1560
1561
1562
17.3M
static void swapexps (expdesc *e1, expdesc *e2) {
1563
17.3M
  expdesc temp = *e1; *e1 = *e2; *e2 = temp;  /* swap 'e1' and 'e2' */
1564
17.3M
}
1565
1566
1567
/*
1568
** Code binary operators with no constant operand.
1569
*/
1570
static void codebinNoK (FuncState *fs, BinOpr opr,
1571
2.82M
                        expdesc *e1, expdesc *e2, int flip, int line) {
1572
2.82M
  if (flip)
1573
354
    swapexps(e1, e2);  /* back to original order */
1574
2.82M
  codebinexpval(fs, opr, e1, e2, line);  /* use standard operators */
1575
2.82M
}
1576
1577
1578
/*
1579
** Code arithmetic operators ('+', '-', ...). If second operand is a
1580
** constant in the proper range, use variant opcodes with K operands.
1581
*/
1582
static void codearith (FuncState *fs, BinOpr opr,
1583
2.84M
                       expdesc *e1, expdesc *e2, int flip, int line) {
1584
2.84M
  if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2))  /* K operand? */
1585
89.0k
    codebinK(fs, opr, e1, e2, flip, line);
1586
2.75M
  else  /* 'e2' is neither an immediate nor a K operand */
1587
2.75M
    codebinNoK(fs, opr, e1, e2, flip, line);
1588
2.84M
}
1589
1590
1591
/*
1592
** Code commutative operators ('+', '*'). If first operand is a
1593
** numeric constant, change order of operands to try to use an
1594
** immediate or K operator.
1595
*/
1596
static void codecommutative (FuncState *fs, BinOpr op,
1597
23.7k
                             expdesc *e1, expdesc *e2, int line) {
1598
23.7k
  int flip = 0;
1599
23.7k
  if (tonumeral(e1, NULL)) {  /* is first operand a numeric constant? */
1600
1.86k
    swapexps(e1, e2);  /* change order */
1601
1.86k
    flip = 1;
1602
1.86k
  }
1603
23.7k
  if (op == OPR_ADD && isSCint(e2))  /* immediate operand? */
1604
203
    codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
1605
23.5k
  else
1606
23.5k
    codearith(fs, op, e1, e2, flip, line);
1607
23.7k
}
1608
1609
1610
/*
1611
** Code bitwise operations; they are all commutative, so the function
1612
** tries to put an integer constant as the 2nd operand (a K operand).
1613
*/
1614
static void codebitwise (FuncState *fs, BinOpr opr,
1615
84.8k
                         expdesc *e1, expdesc *e2, int line) {
1616
84.8k
  int flip = 0;
1617
84.8k
  if (e1->k == VKINT) {
1618
6.87k
    swapexps(e1, e2);  /* 'e2' will be the constant operand */
1619
6.87k
    flip = 1;
1620
6.87k
  }
1621
84.8k
  if (e2->k == VKINT && luaK_exp2K(fs, e2))  /* K operand? */
1622
18.0k
    codebinK(fs, opr, e1, e2, flip, line);
1623
66.8k
  else  /* no constants */
1624
66.8k
    codebinNoK(fs, opr, e1, e2, flip, line);
1625
84.8k
}
1626
1627
1628
/*
1629
** Emit code for order comparisons. When using an immediate operand,
1630
** 'isfloat' tells whether the original value was a float.
1631
*/
1632
17.9M
static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1633
17.9M
  int r1, r2;
1634
17.9M
  int im;
1635
17.9M
  int isfloat = 0;
1636
17.9M
  OpCode op;
1637
17.9M
  if (isSCnumber(e2, &im, &isfloat)) {
1638
    /* use immediate operand */
1639
5.70k
    r1 = luaK_exp2anyreg(fs, e1);
1640
5.70k
    r2 = im;
1641
5.70k
    op = binopr2op(opr, OPR_LT, OP_LTI);
1642
5.70k
  }
1643
17.9M
  else if (isSCnumber(e1, &im, &isfloat)) {
1644
    /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
1645
304k
    r1 = luaK_exp2anyreg(fs, e2);
1646
304k
    r2 = im;
1647
304k
    op = binopr2op(opr, OPR_LT, OP_GTI);
1648
304k
  }
1649
17.6M
  else {  /* regular case, compare two registers */
1650
17.6M
    r1 = luaK_exp2anyreg(fs, e1);
1651
17.6M
    r2 = luaK_exp2anyreg(fs, e2);
1652
17.6M
    op = binopr2op(opr, OPR_LT, OP_LT);
1653
17.6M
  }
1654
17.9M
  freeexps(fs, e1, e2);
1655
17.9M
  e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
1656
17.9M
  e1->k = VJMP;
1657
17.9M
}
1658
1659
1660
/*
1661
** Emit code for equality comparisons ('==', '~=').
1662
** 'e1' was already put as RK by 'luaK_infix'.
1663
*/
1664
3.67k
static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1665
3.67k
  int r1, r2;
1666
3.67k
  int im;
1667
3.67k
  int isfloat = 0;  /* not needed here, but kept for symmetry */
1668
3.67k
  OpCode op;
1669
3.67k
  if (e1->k != VNONRELOC) {
1670
1.68k
    lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
1671
1.68k
    swapexps(e1, e2);
1672
1.68k
  }
1673
3.67k
  r1 = luaK_exp2anyreg(fs, e1);  /* 1st expression must be in register */
1674
3.67k
  if (isSCnumber(e2, &im, &isfloat)) {
1675
1.53k
    op = OP_EQI;
1676
1.53k
    r2 = im;  /* immediate operand */
1677
1.53k
  }
1678
2.13k
  else if (exp2RK(fs, e2)) {  /* 2nd expression is constant? */
1679
762
    op = OP_EQK;
1680
762
    r2 = e2->u.info;  /* constant index */
1681
762
  }
1682
1.37k
  else {
1683
1.37k
    op = OP_EQ;  /* will compare two registers */
1684
1.37k
    r2 = luaK_exp2anyreg(fs, e2);
1685
1.37k
  }
1686
3.67k
  freeexps(fs, e1, e2);
1687
3.67k
  e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
1688
3.67k
  e1->k = VJMP;
1689
3.67k
}
1690
1691
1692
/*
1693
** Apply prefix operation 'op' to expression 'e'.
1694
*/
1695
88.0k
void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
1696
88.0k
  static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1697
88.0k
  luaK_dischargevars(fs, e);
1698
88.0k
  switch (opr) {
1699
84.4k
    case OPR_MINUS: case OPR_BNOT:  /* use 'ef' as fake 2nd operand */
1700
84.4k
      if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef))
1701
29.6k
        break;
1702
      /* else */ /* FALLTHROUGH */
1703
55.8k
    case OPR_LEN:
1704
55.8k
      codeunexpval(fs, unopr2op(opr), e, line);
1705
55.8k
      break;
1706
2.61k
    case OPR_NOT: codenot(fs, e); break;
1707
0
    default: lua_assert(0);
1708
88.0k
  }
1709
88.0k
}
1710
1711
1712
/*
1713
** Process 1st operand 'v' of binary operation 'op' before reading
1714
** 2nd operand.
1715
*/
1716
21.3M
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1717
21.3M
  luaK_dischargevars(fs, v);
1718
21.3M
  switch (op) {
1719
11.6k
    case OPR_AND: {
1720
11.6k
      luaK_goiftrue(fs, v);  /* go ahead only if 'v' is true */
1721
11.6k
      break;
1722
0
    }
1723
39.4k
    case OPR_OR: {
1724
39.4k
      luaK_goiffalse(fs, v);  /* go ahead only if 'v' is false */
1725
39.4k
      break;
1726
0
    }
1727
4.51k
    case OPR_CONCAT: {
1728
4.51k
      luaK_exp2nextreg(fs, v);  /* operand must be on the stack */
1729
4.51k
      break;
1730
0
    }
1731
45.6k
    case OPR_ADD: case OPR_SUB:
1732
2.66M
    case OPR_MUL: case OPR_DIV: case OPR_IDIV:
1733
2.91M
    case OPR_MOD: case OPR_POW:
1734
3.00M
    case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1735
3.34M
    case OPR_SHL: case OPR_SHR: {
1736
3.34M
      if (!tonumeral(v, NULL))
1737
3.11M
        luaK_exp2anyreg(fs, v);
1738
      /* else keep numeral, which may be folded or used as an immediate
1739
         operand */
1740
3.34M
      break;
1741
3.04M
    }
1742
3.78k
    case OPR_EQ: case OPR_NE: {
1743
3.78k
      if (!tonumeral(v, NULL))
1744
2.09k
        exp2RK(fs, v);
1745
      /* else keep numeral, which may be an immediate operand */
1746
3.78k
      break;
1747
2.46k
    }
1748
655k
    case OPR_LT: case OPR_LE:
1749
17.9M
    case OPR_GT: case OPR_GE: {
1750
17.9M
      int dummy, dummy2;
1751
17.9M
      if (!isSCnumber(v, &dummy, &dummy2))
1752
17.9M
        luaK_exp2anyreg(fs, v);
1753
      /* else keep numeral, which may be an immediate operand */
1754
17.9M
      break;
1755
17.9M
    }
1756
0
    default: lua_assert(0);
1757
21.3M
  }
1758
21.3M
}
1759
1760
/*
1761
** Create code for '(e1 .. e2)'.
1762
** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
1763
** because concatenation is right associative), merge both CONCATs.
1764
*/
1765
4.41k
static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
1766
4.41k
  Instruction *ie2 = previousinstruction(fs);
1767
4.41k
  if (GET_OPCODE(*ie2) == OP_CONCAT) {  /* is 'e2' a concatenation? */
1768
746
    int n = GETARG_B(*ie2);  /* # of elements concatenated in 'e2' */
1769
746
    lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
1770
746
    freeexp(fs, e2);
1771
746
    SETARG_A(*ie2, e1->u.info);  /* correct first element ('e1') */
1772
746
    SETARG_B(*ie2, n + 1);  /* will concatenate one more element */
1773
746
  }
1774
3.67k
  else {  /* 'e2' is not a concatenation */
1775
3.67k
    luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0);  /* new concat opcode */
1776
3.67k
    freeexp(fs, e2);
1777
3.67k
    luaK_fixline(fs, line);
1778
3.67k
  }
1779
4.41k
}
1780
1781
1782
/*
1783
** Finalize code for binary operation, after reading 2nd operand.
1784
*/
1785
void luaK_posfix (FuncState *fs, BinOpr opr,
1786
21.3M
                  expdesc *e1, expdesc *e2, int line) {
1787
21.3M
  luaK_dischargevars(fs, e2);
1788
21.3M
  if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2))
1789
50.0k
    return;  /* done by folding */
1790
21.3M
  switch (opr) {
1791
11.3k
    case OPR_AND: {
1792
11.3k
      lua_assert(e1->t == NO_JUMP);  /* list closed by 'luaK_infix' */
1793
11.3k
      luaK_concat(fs, &e2->f, e1->f);
1794
11.3k
      *e1 = *e2;
1795
11.3k
      break;
1796
11.3k
    }
1797
39.1k
    case OPR_OR: {
1798
39.1k
      lua_assert(e1->f == NO_JUMP);  /* list closed by 'luaK_infix' */
1799
39.1k
      luaK_concat(fs, &e2->t, e1->t);
1800
39.1k
      *e1 = *e2;
1801
39.1k
      break;
1802
39.1k
    }
1803
4.41k
    case OPR_CONCAT: {  /* e1 .. e2 */
1804
4.41k
      luaK_exp2nextreg(fs, e2);
1805
4.41k
      codeconcat(fs, e1, e2, line);
1806
4.41k
      break;
1807
39.1k
    }
1808
23.7k
    case OPR_ADD: case OPR_MUL: {
1809
23.7k
      codecommutative(fs, opr, e1, e2, line);
1810
23.7k
      break;
1811
3.34k
    }
1812
37.9k
    case OPR_SUB: {
1813
37.9k
      if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
1814
24.7k
        break; /* coded as (r1 + -I) */
1815
      /* ELSE */
1816
37.9k
    }  /* FALLTHROUGH */
1817
2.82M
    case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
1818
2.82M
      codearith(fs, opr, e1, e2, 0, line);
1819
2.82M
      break;
1820
2.66M
    }
1821
84.8k
    case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
1822
84.8k
      codebitwise(fs, opr, e1, e2, line);
1823
84.8k
      break;
1824
13.5k
    }
1825
44.0k
    case OPR_SHL: {
1826
44.0k
      if (isSCint(e1)) {
1827
2.77k
        swapexps(e1, e2);
1828
2.77k
        codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL);  /* I << r2 */
1829
2.77k
      }
1830
41.3k
      else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
1831
2.38k
        /* coded as (r1 >> -I) */;
1832
2.38k
      }
1833
38.9k
      else  /* regular case (two registers) */
1834
38.9k
       codebinexpval(fs, opr, e1, e2, line);
1835
44.0k
      break;
1836
13.5k
    }
1837
296k
    case OPR_SHR: {
1838
296k
      if (isSCint(e2))
1839
89.4k
        codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR);  /* r1 >> I */
1840
206k
      else  /* regular case (two registers) */
1841
206k
        codebinexpval(fs, opr, e1, e2, line);
1842
296k
      break;
1843
13.5k
    }
1844
3.67k
    case OPR_EQ: case OPR_NE: {
1845
3.67k
      codeeq(fs, opr, e1, e2);
1846
3.67k
      break;
1847
2.44k
    }
1848
17.3M
    case OPR_GT: case OPR_GE: {
1849
      /* '(a > b)' <=> '(b < a)';  '(a >= b)' <=> '(b <= a)' */
1850
17.3M
      swapexps(e1, e2);
1851
17.3M
      opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1852
17.3M
    }  /* FALLTHROUGH */
1853
17.9M
    case OPR_LT: case OPR_LE: {
1854
17.9M
      codeorder(fs, opr, e1, e2);
1855
17.9M
      break;
1856
17.9M
    }
1857
0
    default: lua_assert(0);
1858
21.3M
  }
1859
21.3M
}
1860
1861
1862
/*
1863
** Change line information associated with current position, by removing
1864
** previous info and adding it again with new line.
1865
*/
1866
6.69M
void luaK_fixline (FuncState *fs, int line) {
1867
6.69M
  removelastlineinfo(fs);
1868
6.69M
  savelineinfo(fs, fs->f, line);
1869
6.69M
}
1870
1871
1872
5.93k
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
1873
5.93k
  Instruction *inst = &fs->f->code[pc];
1874
5.93k
  int extra = asize / (MAXARG_vC + 1);  /* higher bits of array size */
1875
5.93k
  int rc = asize % (MAXARG_vC + 1);  /* lower bits of array size */
1876
5.93k
  int k = (extra > 0);  /* true iff needs extra argument */
1877
5.93k
  hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0;
1878
5.93k
  *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k);
1879
5.93k
  *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
1880
5.93k
}
1881
1882
1883
/*
1884
** Emit a SETLIST instruction.
1885
** 'base' is register that keeps table;
1886
** 'nelems' is #table plus those to be stored now;
1887
** 'tostore' is number of values (in registers 'base + 1',...) to add to
1888
** table (or LUA_MULTRET to add up to stack top).
1889
*/
1890
27.5k
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
1891
27.5k
  lua_assert(tostore != 0);
1892
27.5k
  if (tostore == LUA_MULTRET)
1893
2
    tostore = 0;
1894
27.5k
  if (nelems <= MAXARG_vC)
1895
573
    luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
1896
27.0k
  else {
1897
27.0k
    int extra = nelems / (MAXARG_vC + 1);
1898
27.0k
    nelems %= (MAXARG_vC + 1);
1899
27.0k
    luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
1900
27.0k
    codeextraarg(fs, extra);
1901
27.0k
  }
1902
27.5k
  fs->freereg = cast_byte(base + 1);  /* free registers with list values */
1903
27.5k
}
1904
1905
1906
/*
1907
** return the final target of a jump (skipping jumps to jumps)
1908
*/
1909
6.67M
static int finaltarget (Instruction *code, int i) {
1910
6.67M
  int count;
1911
13.3M
  for (count = 0; count < 100; count++) {  /* avoid infinite loops */
1912
13.3M
    Instruction pc = code[i];
1913
13.3M
    if (GET_OPCODE(pc) != OP_JMP)
1914
6.67M
      break;
1915
6.69M
    else
1916
6.69M
      i += GETARG_sJ(pc) + 1;
1917
13.3M
  }
1918
6.67M
  return i;
1919
6.67M
}
1920
1921
1922
/*
1923
** Do a final pass over the code of a function, doing small peephole
1924
** optimizations and adjustments.
1925
*/
1926
#include "lopnames.h"
1927
532
void luaK_finish (FuncState *fs) {
1928
532
  int i;
1929
532
  Proto *p = fs->f;
1930
35.1M
  for (i = 0; i < fs->pc; i++) {
1931
35.1M
    Instruction *pc = &p->code[i];
1932
    /* avoid "not used" warnings when assert is off (for 'onelua.c') */
1933
35.1M
    (void)luaP_isOT; (void)luaP_isIT;
1934
35.1M
    lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
1935
35.1M
    switch (GET_OPCODE(*pc)) {
1936
542
      case OP_RETURN0: case OP_RETURN1: {
1937
542
        if (!(fs->needclose || (p->flag & PF_ISVARARG)))
1938
391
          break;  /* no extra work */
1939
        /* else use OP_RETURN to do the extra work */
1940
151
        SET_OPCODE(*pc, OP_RETURN);
1941
151
      }  /* FALLTHROUGH */
1942
153
      case OP_RETURN: case OP_TAILCALL: {
1943
153
        if (fs->needclose)
1944
153
          SETARG_k(*pc, 1);  /* signal that it needs to close */
1945
153
        if (p->flag & PF_ISVARARG)
1946
153
          SETARG_C(*pc, p->numparams + 1);  /* signal that it is vararg */
1947
153
        break;
1948
153
      }
1949
0
      case OP_GETVARG: {
1950
0
        if (p->flag & PF_VATAB)  /* function has a vararg table? */
1951
0
          SET_OPCODE(*pc, OP_GETTABLE);  /* must get vararg there */
1952
0
        break;
1953
153
      }
1954
6.67M
      case OP_JMP: {  /* to optimize jumps to jumps */
1955
6.67M
        int target = finaltarget(p->code, i);
1956
6.67M
        fixjump(fs, i, target);  /* jump directly to final target */
1957
6.67M
        break;
1958
153
      }
1959
28.4M
      default: break;
1960
35.1M
    }
1961
35.1M
  }
1962
532
}