Coverage Report

Created: 2025-08-25 07:03

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