Coverage Report

Created: 2025-10-27 06:39

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