Coverage Report

Created: 2025-08-29 06:37

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