Coverage Report

Created: 2026-04-11 06:45

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