Coverage Report

Created: 2026-01-25 07:00

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
197M
#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
5
l_noret luaK_semerror (LexState *ls, const char *fmt, ...) {
44
5
  const char *msg;
45
5
  va_list argp;
46
5
  pushvfstring(ls->L, argp, fmt, msg);
47
5
  ls->t.token = 0;  /* remove "near <token>" from final message */
48
5
  ls->linenumber = ls->lastline;  /* back to line of last used token */
49
5
  luaX_syntaxerror(ls, msg);
50
5
}
51
52
53
/*
54
** If expression is a numeric constant, fills 'v' with its value
55
** and returns 1. Otherwise, returns 0.
56
*/
57
5.06M
static int tonumeral (const expdesc *e, TValue *v) {
58
5.06M
  if (hasjumps(e))
59
3.03k
    return 0;  /* not a numeral */
60
5.06M
  switch (e->k) {
61
613k
    case VKINT:
62
613k
      if (v) setivalue(v, e->u.ival);
63
613k
      return 1;
64
39.4k
    case VKFLT:
65
39.4k
      if (v) setfltvalue(v, e->u.nval);
66
39.4k
      return 1;
67
4.41M
    default: return 0;
68
5.06M
  }
69
5.06M
}
70
71
72
/*
73
** Get the constant value from a constant expression
74
*/
75
7.33M
static TValue *const2val (FuncState *fs, const expdesc *e) {
76
7.33M
  lua_assert(e->k == VCONST);
77
7.33M
  return &fs->ls->dyd->actvar.arr[e->u.info].k;
78
7.33M
}
79
80
81
/*
82
** If expression is a constant, fills 'v' with its value
83
** and returns 1. Otherwise, returns 0.
84
*/
85
1.04k
int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
86
1.04k
  if (hasjumps(e))
87
0
    return 0;  /* not a constant */
88
1.04k
  switch (e->k) {
89
28
    case VFALSE:
90
28
      setbfvalue(v);
91
28
      return 1;
92
0
    case VTRUE:
93
0
      setbtvalue(v);
94
0
      return 1;
95
690
    case VNIL:
96
690
      setnilvalue(v);
97
690
      return 1;
98
3
    case VKSTR: {
99
3
      setsvalue(fs->ls->L, v, e->u.strval);
100
3
      return 1;
101
3
    }
102
31
    case VCONST: {
103
31
      setobj(fs->ls->L, v, const2val(fs, e));
104
31
      return 1;
105
31
    }
106
291
    default: return tonumeral(e, v);
107
1.04k
  }
108
1.04k
}
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
47.9k
static Instruction *previousinstruction (FuncState *fs) {
118
47.9k
  static const Instruction invalidinstruction = ~(Instruction)0;
119
47.9k
  if (fs->pc > fs->lasttarget)
120
45.5k
    return &fs->f->code[fs->pc - 1];  /* previous instruction */
121
2.36k
  else
122
2.36k
    return cast(Instruction*, &invalidinstruction);
123
47.9k
}
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
3.12k
void luaK_nil (FuncState *fs, int from, int n) {
133
3.12k
  int l = from + n - 1;  /* last register to set nil */
134
3.12k
  Instruction *previous = previousinstruction(fs);
135
3.12k
  if (GET_OPCODE(*previous) == OP_LOADNIL) {  /* previous is LOADNIL? */
136
2
    int pfrom = GETARG_A(*previous);  /* get previous range */
137
2
    int pl = pfrom + GETARG_B(*previous);
138
2
    if ((pfrom <= from && from <= pl + 1) ||
139
2
        (from <= pfrom && pfrom <= l + 1)) {  /* can connect both? */
140
2
      if (pfrom < from) from = pfrom;  /* from = min(from, pfrom) */
141
2
      if (pl > l) l = pl;  /* l = max(l, pl) */
142
2
      SETARG_A(*previous, from);
143
2
      SETARG_B(*previous, l - from);
144
2
      return;
145
2
    }  /* else go through */
146
2
  }
147
3.12k
  luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0);  /* else no optimization */
148
3.12k
}
149
150
151
/*
152
** Gets the destination address of a jump instruction. Used to traverse
153
** a list of jumps.
154
*/
155
22.5M
static int getjump (FuncState *fs, int pc) {
156
22.5M
  int offset = GETARG_sJ(fs->f->code[pc]);
157
22.5M
  if (offset == NO_JUMP)  /* point to itself represents end of list */
158
22.3M
    return NO_JUMP;  /* end of list */
159
223k
  else
160
223k
    return (pc+1)+offset;  /* turn offset into absolute position */
161
22.5M
}
162
163
164
/*
165
** Fix jump instruction at position 'pc' to jump to 'dest'.
166
** (Jump addresses are relative in Lua)
167
*/
168
30.7M
static void fixjump (FuncState *fs, int pc, int dest) {
169
30.7M
  Instruction *jmp = &fs->f->code[pc];
170
30.7M
  int offset = dest - (pc + 1);
171
30.7M
  lua_assert(dest != NO_JUMP);
172
30.7M
  if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
173
0
    luaX_syntaxerror(fs->ls, "control structure too long");
174
30.7M
  lua_assert(GET_OPCODE(*jmp) == OP_JMP);
175
30.7M
  SETARG_sJ(*jmp, offset);
176
30.7M
}
177
178
179
/*
180
** Concatenate jump-list 'l2' into jump-list 'l1'
181
*/
182
22.3M
void luaK_concat (FuncState *fs, int *l1, int l2) {
183
22.3M
  if (l2 == NO_JUMP) return;  /* nothing to concatenate? */
184
22.3M
  else if (*l1 == NO_JUMP)  /* no original list? */
185
22.3M
    *l1 = l2;  /* 'l1' points to 'l2' */
186
7.26k
  else {
187
7.26k
    int list = *l1;
188
7.26k
    int next;
189
222k
    while ((next = getjump(fs, list)) != NO_JUMP)  /* find last element */
190
215k
      list = next;
191
7.26k
    fixjump(fs, list, l2);  /* last element links to 'l2' */
192
7.26k
  }
193
22.3M
}
194
195
196
/*
197
** Create a jump instruction and return its position, so its destination
198
** can be fixed later (with 'fixjump').
199
*/
200
22.3M
int luaK_jump (FuncState *fs) {
201
22.3M
  return codesJ(fs, OP_JMP, NO_JUMP, 0);
202
22.3M
}
203
204
205
/*
206
** Code a 'return' instruction
207
*/
208
158k
void luaK_ret (FuncState *fs, int first, int nret) {
209
158k
  OpCode op;
210
158k
  switch (nret) {
211
158k
    case 0: op = OP_RETURN0; break;
212
515
    case 1: op = OP_RETURN1; break;
213
196
    default: op = OP_RETURN; break;
214
158k
  }
215
158k
  luaY_checklimit(fs, nret + 1, MAXARG_B, "returns");
216
158k
  luaK_codeABC(fs, op, first, nret + 1, 0);
217
158k
}
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
22.2M
static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
225
22.2M
  luaK_codeABCk(fs, op, A, B, C, k);
226
22.2M
  return luaK_jump(fs);
227
22.2M
}
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
89.1M
int luaK_getlabel (FuncState *fs) {
235
89.1M
  fs->lasttarget = fs->pc;
236
89.1M
  return fs->pc;
237
89.1M
}
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
44.5M
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
246
44.5M
  Instruction *pi = &fs->f->code[pc];
247
44.5M
  if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
248
44.5M
    return pi-1;
249
4.38k
  else
250
4.38k
    return pi;
251
44.5M
}
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
22.3M
static int patchtestreg (FuncState *fs, int node, int reg) {
262
22.3M
  Instruction *i = getjumpcontrol(fs, node);
263
22.3M
  if (GET_OPCODE(*i) != OP_TESTSET)
264
22.2M
    return 0;  /* cannot patch other instructions */
265
9.34k
  if (reg != NO_REG && reg != GETARG_B(*i))
266
9.34k
    SETARG_A(*i, reg);
267
9.33k
  else {
268
     /* no register to put value or register already has the value;
269
        change instruction to simple test */
270
9.33k
    *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
271
9.33k
  }
272
9.34k
  return 1;
273
9.34k
}
274
275
276
/*
277
** Traverse a list of tests ensuring no one produces a value
278
*/
279
3.98k
static void removevalues (FuncState *fs, int list) {
280
4.83k
  for (; list != NO_JUMP; list = getjump(fs, list))
281
854
      patchtestreg(fs, list, NO_REG);
282
3.98k
}
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
66.8M
                          int dtarget) {
292
89.1M
  while (list != NO_JUMP) {
293
22.3M
    int next = getjump(fs, list);
294
22.3M
    if (patchtestreg(fs, list, reg))
295
9.11k
      fixjump(fs, list, vtarget);
296
22.2M
    else
297
22.2M
      fixjump(fs, list, dtarget);  /* jump to default target */
298
22.3M
    list = next;
299
22.3M
  }
300
66.8M
}
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
22.3M
void luaK_patchlist (FuncState *fs, int list, int target) {
309
22.3M
  lua_assert(target <= fs->pc);
310
22.3M
  patchlistaux(fs, list, target, NO_REG, target);
311
22.3M
}
312
313
314
22.3M
void luaK_patchtohere (FuncState *fs, int list) {
315
22.3M
  int hr = luaK_getlabel(fs);  /* mark "here" as a jump target */
316
22.3M
  luaK_patchlist(fs, list, hr);
317
22.3M
}
318
319
320
/* limit for difference between lines in relative line info. */
321
288M
#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
144M
static void savelineinfo (FuncState *fs, Proto *f, int line) {
332
144M
  int linedif = line - fs->previousline;
333
144M
  int pc = fs->pc - 1;  /* last instruction coded */
334
144M
  if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
335
1.13M
    luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
336
1.13M
                    f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
337
1.13M
    f->abslineinfo[fs->nabslineinfo].pc = pc;
338
1.13M
    f->abslineinfo[fs->nabslineinfo++].line = line;
339
1.13M
    linedif = ABSLINEINFO;  /* signal that there is absolute information */
340
1.13M
    fs->iwthabs = 1;  /* restart counter */
341
1.13M
  }
342
144M
  luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
343
144M
                  INT_MAX, "opcodes");
344
144M
  f->lineinfo[pc] = cast(ls_byte, linedif);
345
144M
  fs->previousline = line;  /* last line saved */
346
144M
}
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
3.63M
static void removelastlineinfo (FuncState *fs) {
356
3.63M
  Proto *f = fs->f;
357
3.63M
  int pc = fs->pc - 1;  /* last instruction coded */
358
3.63M
  if (f->lineinfo[pc] != ABSLINEINFO) {  /* relative line info? */
359
3.60M
    fs->previousline -= f->lineinfo[pc];  /* correct last line saved */
360
3.60M
    fs->iwthabs--;  /* undo previous increment */
361
3.60M
  }
362
29.3k
  else {  /* absolute line information */
363
29.3k
    lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
364
29.3k
    fs->nabslineinfo--;  /* remove it */
365
29.3k
    fs->iwthabs = MAXIWTHABS + 1;  /* force next line info to be absolute */
366
29.3k
  }
367
3.63M
}
368
369
370
/*
371
** Remove the last instruction created, correcting line information
372
** accordingly.
373
*/
374
36
static void removelastinstruction (FuncState *fs) {
375
36
  removelastlineinfo(fs);
376
36
  fs->pc--;
377
36
}
378
379
380
/*
381
** Emit instruction 'i', checking for array sizes and saving also its
382
** line information. Return 'i' position.
383
*/
384
140M
int luaK_code (FuncState *fs, Instruction i) {
385
140M
  Proto *f = fs->f;
386
  /* put new instruction in code array */
387
140M
  luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
388
140M
                  INT_MAX, "opcodes");
389
140M
  f->code[fs->pc++] = i;
390
140M
  savelineinfo(fs, f, fs->ls->lastline);
391
140M
  return fs->pc - 1;  /* index of new instruction */
392
140M
}
393
394
395
/*
396
** Format and emit an 'iABC' instruction. (Assertions check consistency
397
** of parameters versus opcode.)
398
*/
399
102M
int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
400
102M
  lua_assert(getOpMode(o) == iABC);
401
102M
  lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
402
102M
             C <= MAXARG_C && (k & ~1) == 0);
403
102M
  return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
404
102M
}
405
406
407
24.4k
int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
408
24.4k
  lua_assert(getOpMode(o) == ivABC);
409
24.4k
  lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
410
24.4k
             C <= MAXARG_vC && (k & ~1) == 0);
411
24.4k
  return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
412
24.4k
}
413
414
415
/*
416
** Format and emit an 'iABx' instruction.
417
*/
418
12.7M
int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) {
419
12.7M
  lua_assert(getOpMode(o) == iABx);
420
12.7M
  lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
421
12.7M
  return luaK_code(fs, CREATE_ABx(o, A, Bc));
422
12.7M
}
423
424
425
/*
426
** Format and emit an 'iAsBx' instruction.
427
*/
428
970k
static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
429
970k
  int b = Bc + OFFSET_sBx;
430
970k
  lua_assert(getOpMode(o) == iAsBx);
431
970k
  lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
432
970k
  return luaK_code(fs, CREATE_ABx(o, A, b));
433
970k
}
434
435
436
/*
437
** Format and emit an 'isJ' instruction.
438
*/
439
22.3M
static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
440
22.3M
  int j = sj + OFFSET_sJ;
441
22.3M
  lua_assert(getOpMode(o) == isJ);
442
22.3M
  lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
443
22.3M
  return luaK_code(fs, CREATE_sJ(o, j, k));
444
22.3M
}
445
446
447
/*
448
** Emit an "extra argument" instruction (format 'iAx')
449
*/
450
1.48M
static int codeextraarg (FuncState *fs, int A) {
451
1.48M
  lua_assert(A <= MAXARG_Ax);
452
1.48M
  return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
453
1.48M
}
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
12.5M
static int luaK_codek (FuncState *fs, int reg, int k) {
462
12.5M
  if (k <= MAXARG_Bx)
463
11.1M
    return luaK_codeABx(fs, OP_LOADK, reg, k);
464
1.47M
  else {
465
1.47M
    int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
466
1.47M
    codeextraarg(fs, k);
467
1.47M
    return p;
468
1.47M
  }
469
12.5M
}
470
471
472
/*
473
** Check register-stack level, keeping track of its maximum size
474
** in field 'maxstacksize'
475
*/
476
70.2M
void luaK_checkstack (FuncState *fs, int n) {
477
70.2M
  int newstack = fs->freereg + n;
478
70.2M
  if (newstack > fs->f->maxstacksize) {
479
11.4k
    luaY_checklimit(fs, newstack, MAX_FSTACK, "registers");
480
11.4k
    fs->f->maxstacksize = cast_byte(newstack);
481
11.4k
  }
482
70.2M
}
483
484
485
/*
486
** Reserve 'n' registers in register stack
487
*/
488
70.2M
void luaK_reserveregs (FuncState *fs, int n) {
489
70.2M
  luaK_checkstack(fs, n);
490
70.2M
  fs->freereg =  cast_byte(fs->freereg + n);
491
70.2M
}
492
493
494
/*
495
** Free register 'reg', if it is neither a constant index nor
496
** a local variable.
497
)
498
*/
499
69.8M
static void freereg (FuncState *fs, int reg) {
500
69.8M
  if (reg >= luaY_nvarstack(fs)) {
501
69.2M
    fs->freereg--;
502
69.2M
    lua_assert(reg == fs->freereg);
503
69.2M
  }
504
69.8M
}
505
506
507
/*
508
** Free two registers in proper order
509
*/
510
31.0M
static void freeregs (FuncState *fs, int r1, int r2) {
511
31.0M
  if (r1 > r2) {
512
22.1M
    freereg(fs, r1);
513
22.1M
    freereg(fs, r2);
514
22.1M
  }
515
8.97M
  else {
516
8.97M
    freereg(fs, r2);
517
8.97M
    freereg(fs, r1);
518
8.97M
  }
519
31.0M
}
520
521
522
/*
523
** Free register used by expression 'e' (if any)
524
*/
525
70.5M
static void freeexp (FuncState *fs, expdesc *e) {
526
70.5M
  if (e->k == VNONRELOC)
527
449k
    freereg(fs, e->u.info);
528
70.5M
}
529
530
531
/*
532
** Free registers used by expressions 'e1' and 'e2' (if any) in proper
533
** order.
534
*/
535
23.8M
static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
536
23.8M
  int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
537
23.8M
  int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
538
23.8M
  freeregs(fs, r1, r2);
539
23.8M
}
540
541
542
/*
543
** Add constant 'v' to prototype's list of constants (field 'k').
544
*/
545
4.83M
static int addk (FuncState *fs, Proto *f, TValue *v) {
546
4.83M
  lua_State *L = fs->ls->L;
547
4.83M
  int oldsize = f->sizek;
548
4.83M
  int k = fs->nk;
549
4.83M
  luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
550
12.0M
  while (oldsize < f->sizek)
551
7.18M
    setnilvalue(&f->k[oldsize++]);
552
4.83M
  setobj(L, &f->k[k], v);
553
4.83M
  fs->nk++;
554
4.83M
  luaC_barrier(L, f, v);
555
4.83M
  return k;
556
4.83M
}
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
24.6M
static int k2proto (FuncState *fs, TValue *key, TValue *v) {
566
24.6M
  TValue val;
567
24.6M
  Proto *f = fs->f;
568
24.6M
  int tag = luaH_get(fs->kcache, key, &val);  /* query scanner table */
569
24.6M
  if (!tagisempty(tag)) {  /* is there an index there? */
570
24.5M
    int k = cast_int(ivalue(&val));
571
    /* collisions can happen only for float keys */
572
24.5M
    lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v));
573
24.5M
    return k;  /* reuse index */
574
24.5M
  }
575
52.6k
  else {  /* constant not found; create a new entry */
576
52.6k
    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
52.6k
    setivalue(&val, k);
580
52.6k
    luaH_set(fs->ls->L, fs->kcache, key, &val);
581
52.6k
    return k;
582
52.6k
  }
583
24.6M
}
584
585
586
/*
587
** Add a string to list of constants and return its index.
588
*/
589
23.9M
static int stringK (FuncState *fs, TString *s) {
590
23.9M
  TValue o;
591
23.9M
  setsvalue(fs->ls->L, &o, s);
592
23.9M
  return k2proto(fs, &o, &o);  /* use string itself as key */
593
23.9M
}
594
595
596
/*
597
** Add an integer to list of constants and return its index.
598
*/
599
112k
static int luaK_intK (FuncState *fs, lua_Integer n) {
600
112k
  TValue o;
601
112k
  setivalue(&o, n);
602
112k
  return k2proto(fs, &o, &o);  /* use integer itself as key */
603
112k
}
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
5.37M
static int luaK_numberK (FuncState *fs, lua_Number r) {
618
5.37M
  TValue o, kv;
619
5.37M
  setfltvalue(&o, r);  /* value as a TValue */
620
5.37M
  if (r == 0) {  /* handle zero as a special case */
621
134
    setpvalue(&kv, fs);  /* use FuncState as index */
622
134
    return k2proto(fs, &kv, &o);  /* cannot collide */
623
134
  }
624
5.37M
  else {
625
5.37M
    const int nbm = l_floatatt(MANT_DIG);
626
5.37M
    const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
627
5.37M
    const lua_Number k =  r * (1 + q);  /* key */
628
5.37M
    lua_Integer ik;
629
5.37M
    setfltvalue(&kv, k);  /* key as a TValue */
630
5.37M
    if (!luaV_flttointeger(k, &ik, F2Ieq)) {  /* not an integer value? */
631
587k
      int n = k2proto(fs, &kv, &o);  /* use key */
632
587k
      if (luaV_rawequalobj(&fs->f->k[n], &o))  /* correct value? */
633
587k
        return n;
634
587k
    }
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.78M
    return addk(fs, fs->f, &o);
638
5.37M
  }
639
5.37M
}
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
97
static int boolT (FuncState *fs) {
656
97
  TValue o;
657
97
  setbtvalue(&o);
658
97
  return k2proto(fs, &o, &o);  /* use boolean itself as key */
659
97
}
660
661
662
/*
663
** Add nil to list of constants and return its index.
664
*/
665
39
static int nilK (FuncState *fs) {
666
39
  TValue k, v;
667
39
  setnilvalue(&v);
668
  /* cannot use nil as key; instead use table itself */
669
39
  sethvalue(fs->ls->L, &k, fs->kcache);
670
39
  return k2proto(fs, &k, &v);
671
39
}
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
416k
static int fitsC (lua_Integer i) {
680
416k
  return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
681
416k
}
682
683
684
/*
685
** Check whether 'i' can be stored in an 'sBx' operand.
686
*/
687
6.33M
static int fitsBx (lua_Integer i) {
688
6.33M
  return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
689
6.33M
}
690
691
692
171k
void luaK_int (FuncState *fs, int reg, lua_Integer i) {
693
171k
  if (fitsBx(i))
694
163k
    codeAsBx(fs, OP_LOADI, reg, cast_int(i));
695
8.06k
  else
696
8.06k
    luaK_codek(fs, reg, luaK_intK(fs, i));
697
171k
}
698
699
700
6.17M
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
701
6.17M
  lua_Integer fi;
702
6.17M
  if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
703
806k
    codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
704
5.36M
  else
705
5.36M
    luaK_codek(fs, reg, luaK_numberK(fs, f));
706
6.17M
}
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
25
void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) {
716
25
  luaK_exp2anyreg(fs, var);
717
25
  luaK_fixline(fs, line);
718
25
  k = (k >= MAXARG_Bx) ? 0 : k + 1;
719
25
  luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k);
720
25
  luaK_fixline(fs, line);
721
25
  freeexp(fs, var);
722
25
}
723
724
725
/*
726
** Convert a constant in 'v' into an expression description 'e'
727
*/
728
7.33M
static void const2exp (TValue *v, expdesc *e) {
729
7.33M
  switch (ttypetag(v)) {
730
1.22k
    case LUA_VNUMINT:
731
1.22k
      e->k = VKINT; e->u.ival = ivalue(v);
732
1.22k
      break;
733
6.15M
    case LUA_VNUMFLT:
734
6.15M
      e->k = VKFLT; e->u.nval = fltvalue(v);
735
6.15M
      break;
736
1.17M
    case LUA_VFALSE:
737
1.17M
      e->k = VFALSE;
738
1.17M
      break;
739
0
    case LUA_VTRUE:
740
0
      e->k = VTRUE;
741
0
      break;
742
199
    case LUA_VNIL:
743
199
      e->k = VNIL;
744
199
      break;
745
71
    case LUA_VSHRSTR:  case LUA_VLNGSTR:
746
71
      e->k = VKSTR; e->u.strval = tsvalue(v);
747
71
      break;
748
0
    default: lua_assert(0);
749
7.33M
  }
750
7.33M
}
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
313
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
758
313
  Instruction *pc = &getinstruction(fs, e);
759
313
  luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results");
760
313
  if (e->k == VCALL)  /* expression is an open function call? */
761
313
    SETARG_C(*pc, nresults + 1);
762
58
  else {
763
58
    lua_assert(e->k == VVARARG);
764
58
    SETARG_C(*pc, nresults + 1);
765
58
    SETARG_A(*pc, fs->freereg);
766
58
    luaK_reserveregs(fs, 1);
767
58
  }
768
313
}
769
770
771
/*
772
** Convert a VKSTR to a VK
773
*/
774
23.9M
static int str2K (FuncState *fs, expdesc *e) {
775
23.9M
  lua_assert(e->k == VKSTR);
776
23.9M
  e->u.info = stringK(fs, e->u.strval);
777
23.9M
  e->k = VK;
778
23.9M
  return e->u.info;
779
23.9M
}
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
46.6k
void luaK_setoneret (FuncState *fs, expdesc *e) {
793
46.6k
  if (e->k == VCALL) {  /* expression is an open function call? */
794
    /* already returns 1 value */
795
13.2k
    lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
796
13.2k
    e->k = VNONRELOC;  /* result has fixed position */
797
13.2k
    e->u.info = GETARG_A(getinstruction(fs, e));
798
13.2k
  }
799
33.3k
  else if (e->k == VVARARG) {
800
27
    SETARG_C(getinstruction(fs, e), 2);
801
27
    e->k = VRELOC;  /* can relocate its simple result */
802
27
  }
803
46.6k
}
804
805
/*
806
** Change a vararg parameter into a regular local variable
807
*/
808
308
void luaK_vapar2local (FuncState *fs, expdesc *var) {
809
308
  needvatab(fs->f);  /* function will need a vararg table */
810
  /* now a vararg parameter is equivalent to a regular local variable */
811
308
  var->k = VLOCAL;
812
308
}
813
814
815
/*
816
** Ensure that expression 'e' is not a variable (nor a <const>).
817
** (Expression still may have jump lists.)
818
*/
819
281M
void luaK_dischargevars (FuncState *fs, expdesc *e) {
820
281M
  switch (e->k) {
821
7.33M
    case VCONST: {
822
7.33M
      const2exp(const2val(fs, e), e);
823
7.33M
      break;
824
0
    }
825
3
    case VVARGVAR: {
826
3
      luaK_vapar2local(fs, e);  /* turn it into a local variable */
827
3
    }  /* FALLTHROUGH */
828
2.94k
    case VLOCAL: {  /* already in a register */
829
2.94k
      int temp = e->u.var.ridx;
830
2.94k
      e->u.info = temp;  /* (can't do a direct assignment; values overlap) */
831
2.94k
      e->k = VNONRELOC;  /* becomes a non-relocatable value */
832
2.94k
      break;
833
3
    }
834
7.10M
    case VUPVAL: {  /* move value to some (pending) register */
835
7.10M
      e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
836
7.10M
      e->k = VRELOC;
837
7.10M
      break;
838
3
    }
839
9.45M
    case VINDEXUP: {
840
9.45M
      e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
841
9.45M
      e->k = VRELOC;
842
9.45M
      break;
843
3
    }
844
18
    case VINDEXI: {
845
18
      freereg(fs, e->u.ind.t);
846
18
      e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
847
18
      e->k = VRELOC;
848
18
      break;
849
3
    }
850
7.21M
    case VINDEXSTR: {
851
7.21M
      freereg(fs, e->u.ind.t);
852
7.21M
      e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
853
7.21M
      e->k = VRELOC;
854
7.21M
      break;
855
3
    }
856
7.14M
    case VINDEXED: {
857
7.14M
      freeregs(fs, e->u.ind.t, e->u.ind.idx);
858
7.14M
      e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
859
7.14M
      e->k = VRELOC;
860
7.14M
      break;
861
3
    }
862
44.2k
    case VVARGIND: {
863
44.2k
      freeregs(fs, e->u.ind.t, e->u.ind.idx);
864
44.2k
      e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx);
865
44.2k
      e->k = VRELOC;
866
44.2k
      break;
867
3
    }
868
11.9k
    case VVARARG: case VCALL: {
869
11.9k
      luaK_setoneret(fs, e);
870
11.9k
      break;
871
27
    }
872
243M
    default: break;  /* there is one value available (somewhere) */
873
281M
  }
874
281M
}
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
70.1M
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
883
70.1M
  luaK_dischargevars(fs, e);
884
70.1M
  switch (e->k) {
885
2.31k
    case VNIL: {
886
2.31k
      luaK_nil(fs, reg, 1);
887
2.31k
      break;
888
0
    }
889
1.17M
    case VFALSE: {
890
1.17M
      luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
891
1.17M
      break;
892
0
    }
893
0
    case VTRUE: {
894
0
      luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
895
0
      break;
896
0
    }
897
23.5k
    case VKSTR: {
898
23.5k
      str2K(fs, e);
899
23.5k
    }  /* FALLTHROUGH */
900
7.20M
    case VK: {
901
7.20M
      luaK_codek(fs, reg, e->u.info);
902
7.20M
      break;
903
23.5k
    }
904
6.17M
    case VKFLT: {
905
6.17M
      luaK_float(fs, reg, e->u.nval);
906
6.17M
      break;
907
23.5k
    }
908
171k
    case VKINT: {
909
171k
      luaK_int(fs, reg, e->u.ival);
910
171k
      break;
911
23.5k
    }
912
33.0M
    case VRELOC: {
913
33.0M
      Instruction *pc = &getinstruction(fs, e);
914
33.0M
      SETARG_A(*pc, reg);  /* instruction will put result in 'reg' */
915
33.0M
      break;
916
23.5k
    }
917
10.1k
    case VNONRELOC: {
918
10.1k
      if (reg != e->u.info)
919
1.28k
        luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
920
10.1k
      break;
921
23.5k
    }
922
22.2M
    default: {
923
22.2M
      lua_assert(e->k == VJMP);
924
22.2M
      return;  /* nothing to do... */
925
22.2M
    }
926
70.1M
  }
927
47.8M
  e->u.info = reg;
928
47.8M
  e->k = VNONRELOC;
929
47.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
10.8k
static void discharge2anyreg (FuncState *fs, expdesc *e) {
938
10.8k
  if (e->k != VNONRELOC) {  /* no fixed register yet? */
939
9.45k
    luaK_reserveregs(fs, 1);  /* get a register */
940
9.45k
    discharge2reg(fs, e, fs->freereg-1);  /* put value there */
941
9.45k
  }
942
10.8k
}
943
944
945
44.5M
static int code_loadbool (FuncState *fs, int A, OpCode op) {
946
44.5M
  luaK_getlabel(fs);  /* those instructions may be jump targets */
947
44.5M
  return luaK_codeABC(fs, op, A, 0, 0);
948
44.5M
}
949
950
951
/*
952
** check whether list has any jump that do not produce a value
953
** or produce an inverted value
954
*/
955
22.2M
static int need_value (FuncState *fs, int list) {
956
22.2M
  for (; list != NO_JUMP; list = getjump(fs, list)) {
957
22.2M
    Instruction i = *getjumpcontrol(fs, list);
958
22.2M
    if (GET_OPCODE(i) != OP_TESTSET) return 1;
959
22.2M
  }
960
11.9k
  return 0;  /* not found */
961
22.2M
}
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
70.0M
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
972
70.0M
  discharge2reg(fs, e, reg);
973
70.0M
  if (e->k == VJMP)  /* expression itself is a test? */
974
22.2M
    luaK_concat(fs, &e->t, e->u.info);  /* put this jump in 't' list */
975
70.0M
  if (hasjumps(e)) {
976
22.2M
    int final;  /* position after whole expression */
977
22.2M
    int p_f = NO_JUMP;  /* position of an eventual LOAD false */
978
22.2M
    int p_t = NO_JUMP;  /* position of an eventual LOAD true */
979
22.2M
    if (need_value(fs, e->t) || need_value(fs, e->f)) {
980
22.2M
      int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
981
22.2M
      p_f = code_loadbool(fs, reg, OP_LFALSESKIP);  /* skip next inst. */
982
22.2M
      p_t = code_loadbool(fs, reg, OP_LOADTRUE);
983
      /* jump around these booleans if 'e' is not a test */
984
22.2M
      luaK_patchtohere(fs, fj);
985
22.2M
    }
986
22.2M
    final = luaK_getlabel(fs);
987
22.2M
    patchlistaux(fs, e->f, final, reg, p_f);
988
22.2M
    patchlistaux(fs, e->t, final, reg, p_t);
989
22.2M
  }
990
70.0M
  e->f = e->t = NO_JUMP;
991
70.0M
  e->u.info = reg;
992
70.0M
  e->k = VNONRELOC;
993
70.0M
}
994
995
996
/*
997
** Ensures final expression result is in next available register.
998
*/
999
70.0M
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
1000
70.0M
  luaK_dischargevars(fs, e);
1001
70.0M
  freeexp(fs, e);
1002
70.0M
  luaK_reserveregs(fs, 1);
1003
70.0M
  exp2reg(fs, e, fs->freereg - 1);
1004
70.0M
}
1005
1006
1007
/*
1008
** Ensures final expression result is in some (any) register
1009
** and return that register.
1010
*/
1011
92.9M
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
1012
92.9M
  luaK_dischargevars(fs, e);
1013
92.9M
  if (e->k == VNONRELOC) {  /* expression already has a register? */
1014
23.9M
    if (!hasjumps(e))  /* no jumps? */
1015
23.9M
      return e->u.info;  /* result is already in a register */
1016
4.28k
    if (e->u.info >= luaY_nvarstack(fs)) {  /* reg. is not a local? */
1017
3.99k
      exp2reg(fs, e, e->u.info);  /* put final result in it */
1018
3.99k
      return e->u.info;
1019
3.99k
    }
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
4.28k
  }
1024
69.0M
  luaK_exp2nextreg(fs, e);  /* default: use next available register */
1025
69.0M
  return e->u.info;
1026
92.9M
}
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
23.8M
void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
1034
23.8M
  if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e))
1035
7.35M
    luaK_exp2anyreg(fs, e);
1036
23.8M
}
1037
1038
1039
/*
1040
** Ensures final expression result is either in a register
1041
** or it is a constant.
1042
*/
1043
4.94k
void luaK_exp2val (FuncState *fs, expdesc *e) {
1044
4.94k
  if (e->k == VJMP || hasjumps(e))
1045
676
    luaK_exp2anyreg(fs, e);
1046
4.26k
  else
1047
4.26k
    luaK_dischargevars(fs, e);
1048
4.94k
}
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
156k
static int luaK_exp2K (FuncState *fs, expdesc *e) {
1056
156k
  if (!hasjumps(e)) {
1057
155k
    int info;
1058
155k
    switch (e->k) {  /* move constants to 'k' */
1059
97
      case VTRUE: info = boolT(fs); break;
1060
5
      case VFALSE: info = boolF(fs); break;
1061
39
      case VNIL: info = nilK(fs); break;
1062
104k
      case VKINT: info = luaK_intK(fs, e->u.ival); break;
1063
10.5k
      case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
1064
3.53k
      case VKSTR: info = stringK(fs, e->u.strval); break;
1065
0
      case VK: info = e->u.info; break;
1066
37.1k
      default: return 0;  /* not a constant */
1067
155k
    }
1068
118k
    if (info <= MAXINDEXRK) {  /* does constant fit in 'argC'? */
1069
116k
      e->k = VK;  /* make expression a 'K' expression */
1070
116k
      e->u.info = info;
1071
116k
      return 1;
1072
116k
    }
1073
118k
  }
1074
  /* else, expression doesn't fit; leave it unchanged */
1075
3.03k
  return 0;
1076
156k
}
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
45.0k
static int exp2RK (FuncState *fs, expdesc *e) {
1086
45.0k
  if (luaK_exp2K(fs, e))
1087
7.20k
    return 1;
1088
37.8k
  else {  /* not a constant in the right range: put it in a register */
1089
37.8k
    luaK_exp2anyreg(fs, e);
1090
37.8k
    return 0;
1091
37.8k
  }
1092
45.0k
}
1093
1094
1095
static void codeABRK (FuncState *fs, OpCode o, int A, int B,
1096
40.4k
                      expdesc *ec) {
1097
40.4k
  int k = exp2RK(fs, ec);
1098
40.4k
  luaK_codeABCk(fs, o, A, B, ec->u.info, k);
1099
40.4k
}
1100
1101
1102
/*
1103
** Generate code to store result of expression 'ex' into variable 'var'.
1104
*/
1105
41.4k
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
1106
41.4k
  switch (var->k) {
1107
193
    case VLOCAL: {
1108
193
      freeexp(fs, ex);
1109
193
      exp2reg(fs, ex, var->u.var.ridx);  /* compute 'ex' into proper place */
1110
193
      return;
1111
0
    }
1112
872
    case VUPVAL: {
1113
872
      int e = luaK_exp2anyreg(fs, ex);
1114
872
      luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
1115
872
      break;
1116
0
    }
1117
25.1k
    case VINDEXUP: {
1118
25.1k
      codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
1119
25.1k
      break;
1120
0
    }
1121
5
    case VINDEXI: {
1122
5
      codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
1123
5
      break;
1124
0
    }
1125
10.4k
    case VINDEXSTR: {
1126
10.4k
      codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
1127
10.4k
      break;
1128
0
    }
1129
24
    case VVARGIND: {
1130
24
      needvatab(fs->f);  /* function will need a vararg table */
1131
      /* now, assignment is to a regular table */
1132
24
    }  /* FALLTHROUGH */
1133
4.76k
    case VINDEXED: {
1134
4.76k
      codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
1135
4.76k
      break;
1136
24
    }
1137
0
    default: lua_assert(0);  /* invalid var kind to store */
1138
41.4k
  }
1139
41.2k
  freeexp(fs, ex);
1140
41.2k
}
1141
1142
1143
/*
1144
** Negate condition 'e' (where 'e' is a comparison).
1145
*/
1146
1.54k
static void negatecondition (FuncState *fs, expdesc *e) {
1147
1.54k
  Instruction *pc = getjumpcontrol(fs, e->u.info);
1148
1.54k
  lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
1149
1.54k
                                           GET_OPCODE(*pc) != OP_TEST);
1150
1.54k
  SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
1151
1.54k
}
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
9.84k
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
1161
9.84k
  if (e->k == VRELOC) {
1162
5.62k
    Instruction ie = getinstruction(fs, e);
1163
5.62k
    if (GET_OPCODE(ie) == OP_NOT) {
1164
36
      removelastinstruction(fs);  /* remove previous OP_NOT */
1165
36
      return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
1166
36
    }
1167
    /* else go through */
1168
5.62k
  }
1169
9.80k
  discharge2anyreg(fs, e);
1170
9.80k
  freeexp(fs, e);
1171
9.80k
  return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
1172
9.84k
}
1173
1174
1175
/*
1176
** Emit code to go through if 'e' is true, jump otherwise.
1177
*/
1178
6.03k
void luaK_goiftrue (FuncState *fs, expdesc *e) {
1179
6.03k
  int pc;  /* pc of new jump */
1180
6.03k
  luaK_dischargevars(fs, e);
1181
6.03k
  switch (e->k) {
1182
1.22k
    case VJMP: {  /* condition? */
1183
1.22k
      negatecondition(fs, e);  /* jump when it is false */
1184
1.22k
      pc = e->u.info;  /* save jump position */
1185
1.22k
      break;
1186
0
    }
1187
856
    case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1188
856
      pc = NO_JUMP;  /* always true; do nothing */
1189
856
      break;
1190
75
    }
1191
3.95k
    default: {
1192
3.95k
      pc = jumponcond(fs, e, 0);  /* jump when false */
1193
3.95k
      break;
1194
75
    }
1195
6.03k
  }
1196
6.03k
  luaK_concat(fs, &e->f, pc);  /* insert new jump in false list */
1197
6.03k
  luaK_patchtohere(fs, e->t);  /* true list jumps to here (to go through) */
1198
6.03k
  e->t = NO_JUMP;
1199
6.03k
}
1200
1201
1202
/*
1203
** Emit code to go through if 'e' is false, jump otherwise.
1204
*/
1205
14.4k
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
1206
14.4k
  int pc;  /* pc of new jump */
1207
14.4k
  luaK_dischargevars(fs, e);
1208
14.4k
  switch (e->k) {
1209
8.55k
    case VJMP: {
1210
8.55k
      pc = e->u.info;  /* already jump if true */
1211
8.55k
      break;
1212
0
    }
1213
1
    case VNIL: case VFALSE: {
1214
1
      pc = NO_JUMP;  /* always false; do nothing */
1215
1
      break;
1216
0
    }
1217
5.89k
    default: {
1218
5.89k
      pc = jumponcond(fs, e, 1);  /* jump if true */
1219
5.89k
      break;
1220
0
    }
1221
14.4k
  }
1222
14.4k
  luaK_concat(fs, &e->t, pc);  /* insert new jump in 't' list */
1223
14.4k
  luaK_patchtohere(fs, e->f);  /* false list jumps to here (to go through) */
1224
14.4k
  e->f = NO_JUMP;
1225
14.4k
}
1226
1227
1228
/*
1229
** Code 'not e', doing constant folding.
1230
*/
1231
1.99k
static void codenot (FuncState *fs, expdesc *e) {
1232
1.99k
  switch (e->k) {
1233
592
    case VNIL: case VFALSE: {
1234
592
      e->k = VTRUE;  /* true == not nil == not false */
1235
592
      break;
1236
592
    }
1237
39
    case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1238
39
      e->k = VFALSE;  /* false == not "x" == not 0.5 == not 1 == not true */
1239
39
      break;
1240
11
    }
1241
322
    case VJMP: {
1242
322
      negatecondition(fs, e);
1243
322
      break;
1244
11
    }
1245
566
    case VRELOC:
1246
1.03k
    case VNONRELOC: {
1247
1.03k
      discharge2anyreg(fs, e);
1248
1.03k
      freeexp(fs, e);
1249
1.03k
      e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
1250
1.03k
      e->k = VRELOC;
1251
1.03k
      break;
1252
566
    }
1253
0
    default: lua_assert(0);  /* cannot happen */
1254
1.99k
  }
1255
  /* interchange true and false lists */
1256
1.99k
  { int temp = e->f; e->f = e->t; e->t = temp; }
1257
1.99k
  removevalues(fs, e->f);  /* values are useless when negated */
1258
1.99k
  removevalues(fs, e->t);
1259
1.99k
}
1260
1261
1262
/*
1263
** Check whether expression 'e' is a short literal string
1264
*/
1265
40.3M
static int isKstr (FuncState *fs, expdesc *e) {
1266
40.3M
  return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK &&
1267
40.3M
          ttisshrstring(&fs->f->k[e->u.info]));
1268
40.3M
}
1269
1270
/*
1271
** Check whether expression 'e' is a literal integer.
1272
*/
1273
7.60M
static int isKint (expdesc *e) {
1274
7.60M
  return (e->k == VKINT && !hasjumps(e));
1275
7.60M
}
1276
1277
1278
/*
1279
** Check whether expression 'e' is a literal integer in
1280
** proper range to fit in register C
1281
*/
1282
7.14M
static int isCint (expdesc *e) {
1283
7.14M
  return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1284
7.14M
}
1285
1286
1287
/*
1288
** Check whether expression 'e' is a literal integer in
1289
** proper range to fit in register sC
1290
*/
1291
283k
static int isSCint (expdesc *e) {
1292
283k
  return isKint(e) && fitsC(e->u.ival);
1293
283k
}
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
66.8M
static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
1301
66.8M
  lua_Integer i;
1302
66.8M
  if (e->k == VKINT)
1303
300k
    i = e->u.ival;
1304
66.5M
  else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
1305
1.72k
    *isfloat = 1;
1306
66.5M
  else
1307
66.5M
    return 0;  /* not a number */
1308
302k
  if (!hasjumps(e) && fitsC(i)) {
1309
294k
    *pi = int2sC(cast_int(i));
1310
294k
    return 1;
1311
294k
  }
1312
7.89k
  else
1313
7.89k
    return 0;
1314
302k
}
1315
1316
1317
/*
1318
** Emit SELF instruction or equivalent: the code will convert
1319
** expression 'e' into 'e.key(e,'.
1320
*/
1321
77
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
1322
77
  int ereg, base;
1323
77
  luaK_exp2anyreg(fs, e);
1324
77
  ereg = e->u.info;  /* register where 'e' (the receiver) was placed */
1325
77
  freeexp(fs, e);
1326
77
  base = e->u.info = fs->freereg;  /* base register for op_self */
1327
77
  e->k = VNONRELOC;  /* self expression has a fixed register */
1328
77
  luaK_reserveregs(fs, 2);  /* method and 'self' produced by op_self */
1329
77
  lua_assert(key->k == VKSTR);
1330
  /* is method name a short string in a valid K index? */
1331
77
  if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) {
1332
    /* can use 'self' opcode */
1333
77
    luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0);
1334
77
  }
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
77
  freeexp(fs, key);
1341
77
}
1342
1343
1344
/* auxiliary function to define indexing expressions */
1345
23.8M
static void fillidxk (expdesc *t, int idx, expkind k) {
1346
23.8M
  t->u.ind.idx = cast_byte(idx);
1347
23.8M
  t->k = k;
1348
23.8M
}
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
23.8M
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1358
23.8M
  int keystr = -1;
1359
23.8M
  if (k->k == VKSTR)
1360
23.8M
    keystr = str2K(fs, k);
1361
23.8M
  lua_assert(!hasjumps(t) &&
1362
23.8M
             (t->k == VLOCAL || t->k == VVARGVAR ||
1363
23.8M
              t->k == VNONRELOC || t->k == VUPVAL));
1364
23.8M
  if (t->k == VUPVAL && !isKstr(fs, k))  /* upvalue indexed by non 'Kstr'? */
1365
7.01M
    luaK_exp2anyreg(fs, t);  /* put it in a register */
1366
23.8M
  if (t->k == VUPVAL) {
1367
9.47M
    lu_byte temp = cast_byte(t->u.info);  /* upvalue index */
1368
9.47M
    t->u.ind.t = temp;  /* (can't do a direct assignment; values overlap) */
1369
9.47M
    lua_assert(isKstr(fs, k));
1370
9.47M
    fillidxk(t, k->u.info, VINDEXUP);  /* literal short string */
1371
9.47M
  }
1372
14.4M
  else if (t->k == VVARGVAR) {  /* indexing the vararg parameter? */
1373
45.4k
    int kreg = luaK_exp2anyreg(fs, k);  /* put key in some register */
1374
45.4k
    lu_byte vreg = cast_byte(t->u.var.ridx);  /* register with vararg param. */
1375
45.4k
    lua_assert(vreg == fs->f->numparams);
1376
45.4k
    t->u.ind.t = vreg;  /* (avoid a direct assignment; values may overlap) */
1377
45.4k
    fillidxk(t, kreg, VVARGIND);  /* 't' represents 'vararg[k]' */
1378
45.4k
  }
1379
14.3M
  else {
1380
    /* register index of the table */
1381
14.3M
    t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
1382
14.3M
    if (isKstr(fs, k))
1383
7.22M
      fillidxk(t, k->u.info, VINDEXSTR);  /* literal short string */
1384
7.14M
    else if (isCint(k))  /* int. constant in proper range? */
1385
24
      fillidxk(t, cast_int(k->u.ival), VINDEXI);
1386
7.14M
    else
1387
7.14M
      fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED);  /* register */
1388
14.3M
  }
1389
23.8M
  t->u.ind.keystr = keystr;  /* string index in 'k' */
1390
23.8M
  t->u.ind.ro = 0;  /* by default, not read-only */
1391
23.8M
}
1392
1393
1394
/*
1395
** Return false if folding can raise an error.
1396
** Bitwise operations need operands convertible to integers; division
1397
** operations cannot have 0 as divisor.
1398
*/
1399
140k
static int validop (int op, TValue *v1, TValue *v2) {
1400
140k
  switch (op) {
1401
8.87k
    case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
1402
32.1k
    case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: {  /* conversion errors */
1403
32.1k
      lua_Integer i;
1404
32.1k
      return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
1405
30.1k
              luaV_tointegerns(v2, &i, LUA_FLOORN2I));
1406
11.4k
    }
1407
41.0k
    case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD:  /* division by 0 */
1408
41.0k
      return (nvalue(v2) != 0);
1409
67.5k
    default: return 1;  /* everything else is valid */
1410
140k
  }
1411
140k
}
1412
1413
1414
/*
1415
** Try to "constant-fold" an operation; return 1 iff successful.
1416
** (In this case, 'e1' has the final result.)
1417
*/
1418
static int constfolding (FuncState *fs, int op, expdesc *e1,
1419
2.02M
                                        const expdesc *e2) {
1420
2.02M
  TValue v1, v2, res;
1421
2.02M
  if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
1422
1.91M
    return 0;  /* non-numeric operands or not safe to fold */
1423
108k
  luaO_rawarith(fs->ls->L, op, &v1, &v2, &res);  /* does operation */
1424
108k
  if (ttisinteger(&res)) {
1425
41.1k
    e1->k = VKINT;
1426
41.1k
    e1->u.ival = ivalue(&res);
1427
41.1k
  }
1428
67.7k
  else {  /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
1429
67.7k
    lua_Number n = fltvalue(&res);
1430
67.7k
    if (luai_numisnan(n) || n == 0)
1431
46.1k
      return 0;
1432
21.5k
    e1->k = VKFLT;
1433
21.5k
    e1->u.nval = n;
1434
21.5k
  }
1435
62.6k
  return 1;
1436
108k
}
1437
1438
1439
/*
1440
** Convert a BinOpr to an OpCode  (ORDER OPR - ORDER OP)
1441
*/
1442
23.7M
l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1443
23.7M
  lua_assert(baser <= opr &&
1444
23.7M
            ((baser == OPR_ADD && opr <= OPR_SHR) ||
1445
23.7M
             (baser == OPR_LT && opr <= OPR_LE)));
1446
23.7M
  return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1447
23.7M
}
1448
1449
1450
/*
1451
** Convert a UnOpr to an OpCode  (ORDER OPR - ORDER OP)
1452
*/
1453
352k
l_sinline OpCode unopr2op (UnOpr opr) {
1454
352k
  return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1455
352k
                                       cast_int(OP_UNM));
1456
352k
}
1457
1458
1459
/*
1460
** Convert a BinOpr to a tag method  (ORDER OPR - ORDER TM)
1461
*/
1462
1.51M
l_sinline TMS binopr2TM (BinOpr opr) {
1463
1.51M
  lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1464
1.51M
  return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1465
1.51M
}
1466
1467
1468
/*
1469
** Emit code for unary expressions that "produce values"
1470
** (everything but 'not').
1471
** Expression to produce final result will be encoded in 'e'.
1472
*/
1473
352k
static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1474
352k
  int r = luaK_exp2anyreg(fs, e);  /* opcodes operate only on registers */
1475
352k
  freeexp(fs, e);
1476
352k
  e->u.info = luaK_codeABC(fs, op, 0, r, 0);  /* generate opcode */
1477
352k
  e->k = VRELOC;  /* all those operations are relocatable */
1478
352k
  luaK_fixline(fs, line);
1479
352k
}
1480
1481
1482
/*
1483
** Emit code for binary expressions that "produce values"
1484
** (everything but logical operators 'and'/'or' and comparison
1485
** operators).
1486
** Expression to produce final result will be encoded in 'e1'.
1487
*/
1488
static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
1489
                             OpCode op, int v2, int flip, int line,
1490
1.60M
                             OpCode mmop, TMS event) {
1491
1.60M
  int v1 = luaK_exp2anyreg(fs, e1);
1492
1.60M
  int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
1493
1.60M
  freeexps(fs, e1, e2);
1494
1.60M
  e1->u.info = pc;
1495
1.60M
  e1->k = VRELOC;  /* all those operations are relocatable */
1496
1.60M
  luaK_fixline(fs, line);
1497
1.60M
  luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip);  /* metamethod */
1498
1.60M
  luaK_fixline(fs, line);
1499
1.60M
}
1500
1501
1502
/*
1503
** Emit code for binary expressions that "produce values" over
1504
** two registers.
1505
*/
1506
static void codebinexpval (FuncState *fs, BinOpr opr,
1507
1.40M
                           expdesc *e1, expdesc *e2, int line) {
1508
1.40M
  OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1509
1.40M
  int v2 = luaK_exp2anyreg(fs, e2);  /* make sure 'e2' is in a register */
1510
  /* 'e1' must be already in a register or it is a constant */
1511
1.40M
  lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1512
1.40M
             e1->k == VNONRELOC || e1->k == VRELOC);
1513
1.40M
  lua_assert(OP_ADD <= op && op <= OP_SHR);
1514
1.40M
  finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1515
1.40M
}
1516
1517
1518
/*
1519
** Code binary operators with immediate operands.
1520
*/
1521
static void codebini (FuncState *fs, OpCode op,
1522
                       expdesc *e1, expdesc *e2, int flip, int line,
1523
79.9k
                       TMS event) {
1524
79.9k
  int v2 = int2sC(cast_int(e2->u.ival));  /* immediate operand */
1525
79.9k
  lua_assert(e2->k == VKINT);
1526
79.9k
  finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
1527
79.9k
}
1528
1529
1530
/*
1531
** Code binary operators with K operand.
1532
*/
1533
static void codebinK (FuncState *fs, BinOpr opr,
1534
108k
                      expdesc *e1, expdesc *e2, int flip, int line) {
1535
108k
  TMS event = binopr2TM(opr);
1536
108k
  int v2 = e2->u.info;  /* K index */
1537
108k
  OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1538
108k
  finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1539
108k
}
1540
1541
1542
/* Try to code a binary operator negating its second operand.
1543
** For the metamethod, 2nd operand must keep its original value.
1544
*/
1545
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
1546
179k
                             OpCode op, int line, TMS event) {
1547
179k
  if (!isKint(e2))
1548
161k
    return 0;  /* not an integer constant */
1549
17.1k
  else {
1550
17.1k
    lua_Integer i2 = e2->u.ival;
1551
17.1k
    if (!(fitsC(i2) && fitsC(-i2)))
1552
211
      return 0;  /* not in the proper range */
1553
16.9k
    else {  /* operating a small integer constant */
1554
16.9k
      int v2 = cast_int(i2);
1555
16.9k
      finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
1556
      /* correct metamethod argument */
1557
16.9k
      SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
1558
16.9k
      return 1;  /* successfully coded */
1559
16.9k
    }
1560
17.1k
  }
1561
179k
}
1562
1563
1564
22.1M
static void swapexps (expdesc *e1, expdesc *e2) {
1565
22.1M
  expdesc temp = *e1; *e1 = *e2; *e2 = temp;  /* swap 'e1' and 'e2' */
1566
22.1M
}
1567
1568
1569
/*
1570
** Code binary operators with no constant operand.
1571
*/
1572
static void codebinNoK (FuncState *fs, BinOpr opr,
1573
1.20M
                        expdesc *e1, expdesc *e2, int flip, int line) {
1574
1.20M
  if (flip)
1575
189
    swapexps(e1, e2);  /* back to original order */
1576
1.20M
  codebinexpval(fs, opr, e1, e2, line);  /* use standard operators */
1577
1.20M
}
1578
1579
1580
/*
1581
** Code arithmetic operators ('+', '-', ...). If second operand is a
1582
** constant in the proper range, use variant opcodes with K operands.
1583
*/
1584
static void codearith (FuncState *fs, BinOpr opr,
1585
1.15M
                       expdesc *e1, expdesc *e2, int flip, int line) {
1586
1.15M
  if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2))  /* K operand? */
1587
94.2k
    codebinK(fs, opr, e1, e2, flip, line);
1588
1.06M
  else  /* 'e2' is neither an immediate nor a K operand */
1589
1.06M
    codebinNoK(fs, opr, e1, e2, flip, line);
1590
1.15M
}
1591
1592
1593
/*
1594
** Code commutative operators ('+', '*'). If first operand is a
1595
** numeric constant, change order of operands to try to use an
1596
** immediate or K operator.
1597
*/
1598
static void codecommutative (FuncState *fs, BinOpr op,
1599
16.2k
                             expdesc *e1, expdesc *e2, int line) {
1600
16.2k
  int flip = 0;
1601
16.2k
  if (tonumeral(e1, NULL)) {  /* is first operand a numeric constant? */
1602
1.25k
    swapexps(e1, e2);  /* change order */
1603
1.25k
    flip = 1;
1604
1.25k
  }
1605
16.2k
  if (op == OPR_ADD && isSCint(e2))  /* immediate operand? */
1606
256
    codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
1607
15.9k
  else
1608
15.9k
    codearith(fs, op, e1, e2, flip, line);
1609
16.2k
}
1610
1611
1612
/*
1613
** Code bitwise operations; they are all commutative, so the function
1614
** tries to put an integer constant as the 2nd operand (a K operand).
1615
*/
1616
static void codebitwise (FuncState *fs, BinOpr opr,
1617
155k
                         expdesc *e1, expdesc *e2, int line) {
1618
155k
  int flip = 0;
1619
155k
  if (e1->k == VKINT) {
1620
5.88k
    swapexps(e1, e2);  /* 'e2' will be the constant operand */
1621
5.88k
    flip = 1;
1622
5.88k
  }
1623
155k
  if (e2->k == VKINT && luaK_exp2K(fs, e2))  /* K operand? */
1624
14.7k
    codebinK(fs, opr, e1, e2, flip, line);
1625
140k
  else  /* no constants */
1626
140k
    codebinNoK(fs, opr, e1, e2, flip, line);
1627
155k
}
1628
1629
1630
/*
1631
** Emit code for order comparisons. When using an immediate operand,
1632
** 'isfloat' tells whether the original value was a float.
1633
*/
1634
22.2M
static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1635
22.2M
  int r1, r2;
1636
22.2M
  int im;
1637
22.2M
  int isfloat = 0;
1638
22.2M
  OpCode op;
1639
22.2M
  if (isSCnumber(e2, &im, &isfloat)) {
1640
    /* use immediate operand */
1641
2.67k
    r1 = luaK_exp2anyreg(fs, e1);
1642
2.67k
    r2 = im;
1643
2.67k
    op = binopr2op(opr, OPR_LT, OP_LTI);
1644
2.67k
  }
1645
22.2M
  else if (isSCnumber(e1, &im, &isfloat)) {
1646
    /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
1647
288k
    r1 = luaK_exp2anyreg(fs, e2);
1648
288k
    r2 = im;
1649
288k
    op = binopr2op(opr, OPR_LT, OP_GTI);
1650
288k
  }
1651
21.9M
  else {  /* regular case, compare two registers */
1652
21.9M
    r1 = luaK_exp2anyreg(fs, e1);
1653
21.9M
    r2 = luaK_exp2anyreg(fs, e2);
1654
21.9M
    op = binopr2op(opr, OPR_LT, OP_LT);
1655
21.9M
  }
1656
22.2M
  freeexps(fs, e1, e2);
1657
22.2M
  e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
1658
22.2M
  e1->k = VJMP;
1659
22.2M
}
1660
1661
1662
/*
1663
** Emit code for equality comparisons ('==', '~=').
1664
** 'e1' was already put as RK by 'luaK_infix'.
1665
*/
1666
3.15k
static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1667
3.15k
  int r1, r2;
1668
3.15k
  int im;
1669
3.15k
  int isfloat = 0;  /* not needed here, but kept for symmetry */
1670
3.15k
  OpCode op;
1671
3.15k
  if (e1->k != VNONRELOC) {
1672
882
    lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
1673
882
    swapexps(e1, e2);
1674
882
  }
1675
3.15k
  r1 = luaK_exp2anyreg(fs, e1);  /* 1st expression must be in register */
1676
3.15k
  if (isSCnumber(e2, &im, &isfloat)) {
1677
777
    op = OP_EQI;
1678
777
    r2 = im;  /* immediate operand */
1679
777
  }
1680
2.38k
  else if (exp2RK(fs, e2)) {  /* 2nd expression is constant? */
1681
994
    op = OP_EQK;
1682
994
    r2 = e2->u.info;  /* constant index */
1683
994
  }
1684
1.38k
  else {
1685
1.38k
    op = OP_EQ;  /* will compare two registers */
1686
1.38k
    r2 = luaK_exp2anyreg(fs, e2);
1687
1.38k
  }
1688
3.15k
  freeexps(fs, e1, e2);
1689
3.15k
  e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
1690
3.15k
  e1->k = VJMP;
1691
3.15k
}
1692
1693
1694
/*
1695
** Apply prefix operation 'op' to expression 'e'.
1696
*/
1697
379k
void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
1698
379k
  static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1699
379k
  luaK_dischargevars(fs, e);
1700
379k
  switch (opr) {
1701
373k
    case OPR_MINUS: case OPR_BNOT:  /* use 'ef' as fake 2nd operand */
1702
373k
      if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef))
1703
24.9k
        break;
1704
      /* else */ /* FALLTHROUGH */
1705
352k
    case OPR_LEN:
1706
352k
      codeunexpval(fs, unopr2op(opr), e, line);
1707
352k
      break;
1708
1.99k
    case OPR_NOT: codenot(fs, e); break;
1709
0
    default: lua_assert(0);
1710
379k
  }
1711
379k
}
1712
1713
1714
/*
1715
** Process 1st operand 'v' of binary operation 'op' before reading
1716
** 2nd operand.
1717
*/
1718
24.0M
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1719
24.0M
  luaK_dischargevars(fs, v);
1720
24.0M
  switch (op) {
1721
4.29k
    case OPR_AND: {
1722
4.29k
      luaK_goiftrue(fs, v);  /* go ahead only if 'v' is true */
1723
4.29k
      break;
1724
0
    }
1725
14.4k
    case OPR_OR: {
1726
14.4k
      luaK_goiffalse(fs, v);  /* go ahead only if 'v' is false */
1727
14.4k
      break;
1728
0
    }
1729
44.8k
    case OPR_CONCAT: {
1730
44.8k
      luaK_exp2nextreg(fs, v);  /* operand must be on the stack */
1731
44.8k
      break;
1732
0
    }
1733
171k
    case OPR_ADD: case OPR_SUB:
1734
1.05M
    case OPR_MUL: case OPR_DIV: case OPR_IDIV:
1735
1.20M
    case OPR_MOD: case OPR_POW:
1736
1.36M
    case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1737
1.64M
    case OPR_SHL: case OPR_SHR: {
1738
1.64M
      if (!tonumeral(v, NULL))
1739
1.45M
        luaK_exp2anyreg(fs, v);
1740
      /* else keep numeral, which may be folded or used as an immediate
1741
         operand */
1742
1.64M
      break;
1743
1.37M
    }
1744
3.18k
    case OPR_EQ: case OPR_NE: {
1745
3.18k
      if (!tonumeral(v, NULL))
1746
2.29k
        exp2RK(fs, v);
1747
      /* else keep numeral, which may be an immediate operand */
1748
3.18k
      break;
1749
1.83k
    }
1750
173k
    case OPR_LT: case OPR_LE:
1751
22.2M
    case OPR_GT: case OPR_GE: {
1752
22.2M
      int dummy, dummy2;
1753
22.2M
      if (!isSCnumber(v, &dummy, &dummy2))
1754
22.2M
        luaK_exp2anyreg(fs, v);
1755
      /* else keep numeral, which may be an immediate operand */
1756
22.2M
      break;
1757
22.2M
    }
1758
0
    default: lua_assert(0);
1759
24.0M
  }
1760
24.0M
}
1761
1762
/*
1763
** Create code for '(e1 .. e2)'.
1764
** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
1765
** because concatenation is right associative), merge both CONCATs.
1766
*/
1767
44.8k
static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
1768
44.8k
  Instruction *ie2 = previousinstruction(fs);
1769
44.8k
  if (GET_OPCODE(*ie2) == OP_CONCAT) {  /* is 'e2' a concatenation? */
1770
1.79k
    int n = GETARG_B(*ie2);  /* # of elements concatenated in 'e2' */
1771
1.79k
    lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
1772
1.79k
    freeexp(fs, e2);
1773
1.79k
    SETARG_A(*ie2, e1->u.info);  /* correct first element ('e1') */
1774
1.79k
    SETARG_B(*ie2, n + 1);  /* will concatenate one more element */
1775
1.79k
  }
1776
43.0k
  else {  /* 'e2' is not a concatenation */
1777
43.0k
    luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0);  /* new concat opcode */
1778
43.0k
    freeexp(fs, e2);
1779
43.0k
    luaK_fixline(fs, line);
1780
43.0k
  }
1781
44.8k
}
1782
1783
1784
/*
1785
** Finalize code for binary operation, after reading 2nd operand.
1786
*/
1787
void luaK_posfix (FuncState *fs, BinOpr opr,
1788
23.9M
                  expdesc *e1, expdesc *e2, int line) {
1789
23.9M
  luaK_dischargevars(fs, e2);
1790
23.9M
  if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2))
1791
37.7k
    return;  /* done by folding */
1792
23.9M
  switch (opr) {
1793
3.99k
    case OPR_AND: {
1794
3.99k
      lua_assert(e1->t == NO_JUMP);  /* list closed by 'luaK_infix' */
1795
3.99k
      luaK_concat(fs, &e2->f, e1->f);
1796
3.99k
      *e1 = *e2;
1797
3.99k
      break;
1798
3.99k
    }
1799
14.1k
    case OPR_OR: {
1800
14.1k
      lua_assert(e1->f == NO_JUMP);  /* list closed by 'luaK_infix' */
1801
14.1k
      luaK_concat(fs, &e2->t, e1->t);
1802
14.1k
      *e1 = *e2;
1803
14.1k
      break;
1804
14.1k
    }
1805
44.8k
    case OPR_CONCAT: {  /* e1 .. e2 */
1806
44.8k
      luaK_exp2nextreg(fs, e2);
1807
44.8k
      codeconcat(fs, e1, e2, line);
1808
44.8k
      break;
1809
14.1k
    }
1810
16.2k
    case OPR_ADD: case OPR_MUL: {
1811
16.2k
      codecommutative(fs, opr, e1, e2, line);
1812
16.2k
      break;
1813
2.39k
    }
1814
166k
    case OPR_SUB: {
1815
166k
      if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
1816
15.9k
        break; /* coded as (r1 + -I) */
1817
      /* ELSE */
1818
166k
    }  /* FALLTHROUGH */
1819
1.14M
    case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
1820
1.14M
      codearith(fs, opr, e1, e2, 0, line);
1821
1.14M
      break;
1822
1.04M
    }
1823
155k
    case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
1824
155k
      codebitwise(fs, opr, e1, e2, line);
1825
155k
      break;
1826
8.78k
    }
1827
13.7k
    case OPR_SHL: {
1828
13.7k
      if (isSCint(e1)) {
1829
1.20k
        swapexps(e1, e2);
1830
1.20k
        codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL);  /* I << r2 */
1831
1.20k
      }
1832
12.4k
      else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
1833
1.06k
        /* coded as (r1 >> -I) */;
1834
1.06k
      }
1835
11.4k
      else  /* regular case (two registers) */
1836
11.4k
       codebinexpval(fs, opr, e1, e2, line);
1837
13.7k
      break;
1838
8.78k
    }
1839
267k
    case OPR_SHR: {
1840
267k
      if (isSCint(e2))
1841
78.4k
        codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR);  /* r1 >> I */
1842
188k
      else  /* regular case (two registers) */
1843
188k
        codebinexpval(fs, opr, e1, e2, line);
1844
267k
      break;
1845
8.78k
    }
1846
3.15k
    case OPR_EQ: case OPR_NE: {
1847
3.15k
      codeeq(fs, opr, e1, e2);
1848
3.15k
      break;
1849
1.83k
    }
1850
22.1M
    case OPR_GT: case OPR_GE: {
1851
      /* '(a > b)' <=> '(b < a)';  '(a >= b)' <=> '(b <= a)' */
1852
22.1M
      swapexps(e1, e2);
1853
22.1M
      opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1854
22.1M
    }  /* FALLTHROUGH */
1855
22.2M
    case OPR_LT: case OPR_LE: {
1856
22.2M
      codeorder(fs, opr, e1, e2);
1857
22.2M
      break;
1858
22.2M
    }
1859
0
    default: lua_assert(0);
1860
23.9M
  }
1861
23.9M
}
1862
1863
1864
/*
1865
** Change line information associated with current position, by removing
1866
** previous info and adding it again with new line.
1867
*/
1868
3.63M
void luaK_fixline (FuncState *fs, int line) {
1869
3.63M
  removelastlineinfo(fs);
1870
3.63M
  savelineinfo(fs, fs->f, line);
1871
3.63M
}
1872
1873
1874
5.42k
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
1875
5.42k
  Instruction *inst = &fs->f->code[pc];
1876
5.42k
  int extra = asize / (MAXARG_vC + 1);  /* higher bits of array size */
1877
5.42k
  int rc = asize % (MAXARG_vC + 1);  /* lower bits of array size */
1878
5.42k
  int k = (extra > 0);  /* true iff needs extra argument */
1879
5.42k
  hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0;
1880
5.42k
  *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k);
1881
5.42k
  *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
1882
5.42k
}
1883
1884
1885
/*
1886
** Emit a SETLIST instruction.
1887
** 'base' is register that keeps table;
1888
** 'nelems' is #table plus those to be stored now;
1889
** 'tostore' is number of values (in registers 'base + 1',...) to add to
1890
** table (or LUA_MULTRET to add up to stack top).
1891
*/
1892
18.8k
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
1893
18.8k
  lua_assert(tostore != 0);
1894
18.8k
  if (tostore == LUA_MULTRET)
1895
6
    tostore = 0;
1896
18.8k
  if (nelems <= MAXARG_vC)
1897
3.78k
    luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
1898
15.0k
  else {
1899
15.0k
    int extra = nelems / (MAXARG_vC + 1);
1900
15.0k
    nelems %= (MAXARG_vC + 1);
1901
15.0k
    luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
1902
15.0k
    codeextraarg(fs, extra);
1903
15.0k
  }
1904
18.8k
  fs->freereg = cast_byte(base + 1);  /* free registers with list values */
1905
18.8k
}
1906
1907
1908
/*
1909
** return the final target of a jump (skipping jumps to jumps)
1910
*/
1911
8.39M
static int finaltarget (Instruction *code, int i) {
1912
8.39M
  int count;
1913
16.7M
  for (count = 0; count < 100; count++) {  /* avoid infinite loops */
1914
16.7M
    Instruction pc = code[i];
1915
16.7M
    if (GET_OPCODE(pc) != OP_JMP)
1916
8.39M
      break;
1917
8.39M
    else
1918
8.39M
      i += GETARG_sJ(pc) + 1;
1919
16.7M
  }
1920
8.39M
  return i;
1921
8.39M
}
1922
1923
1924
/*
1925
** Do a final pass over the code of a function, doing small peephole
1926
** optimizations and adjustments.
1927
*/
1928
#include "lopnames.h"
1929
158k
void luaK_finish (FuncState *fs) {
1930
158k
  int i;
1931
158k
  Proto *p = fs->f;
1932
158k
  if (p->flag & PF_VATAB)  /* will it use a vararg table? */
1933
251
    p->flag &= cast_byte(~PF_VAHID);  /* then it will not use hidden args. */
1934
46.6M
  for (i = 0; i < fs->pc; i++) {
1935
46.5M
    Instruction *pc = &p->code[i];
1936
    /* avoid "not used" warnings when assert is off (for 'onelua.c') */
1937
46.5M
    (void)luaP_isOT; (void)luaP_isIT;
1938
46.5M
    lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
1939
46.5M
    switch (GET_OPCODE(*pc)) {
1940
158k
      case OP_RETURN0: case OP_RETURN1: {
1941
158k
        if (!(fs->needclose || (p->flag & PF_VAHID)))
1942
158k
          break;  /* no extra work */
1943
        /* else use OP_RETURN to do the extra work */
1944
501
        SET_OPCODE(*pc, OP_RETURN);
1945
501
      }  /* FALLTHROUGH */
1946
697
      case OP_RETURN: case OP_TAILCALL: {
1947
697
        if (fs->needclose)
1948
697
          SETARG_k(*pc, 1);  /* signal that it needs to close */
1949
697
        if (p->flag & PF_VAHID)  /* does it use hidden arguments? */
1950
697
          SETARG_C(*pc, p->numparams + 1);  /* signal that */
1951
697
        break;
1952
599
      }
1953
44.2k
      case OP_GETVARG: {
1954
44.2k
        if (p->flag & PF_VATAB)  /* function has a vararg table? */
1955
469
          SET_OPCODE(*pc, OP_GETTABLE);  /* must get vararg there */
1956
44.2k
        break;
1957
599
      }
1958
39
      case OP_VARARG: {
1959
39
        if (p->flag & PF_VATAB)  /* function has a vararg table? */
1960
39
          SETARG_k(*pc, 1);  /* must get vararg there */
1961
39
        break;
1962
599
      }
1963
8.39M
      case OP_JMP: {  /* to optimize jumps to jumps */
1964
8.39M
        int target = finaltarget(p->code, i);
1965
8.39M
        fixjump(fs, i, target);  /* jump directly to final target */
1966
8.39M
        break;
1967
599
      }
1968
37.9M
      default: break;
1969
46.5M
    }
1970
46.5M
  }
1971
158k
}