Coverage Report

Created: 2025-07-11 06:33

/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.17G
#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
15.6k
l_noret luaK_semerror (LexState *ls, const char *fmt, ...) {
44
15.6k
  const char *msg;
45
15.6k
  va_list argp;
46
15.6k
  pushvfstring(ls->L, argp, fmt, msg);
47
15.6k
  ls->t.token = 0;  /* remove "near <token>" from final message */
48
15.6k
  luaX_syntaxerror(ls, msg);
49
15.6k
}
50
51
52
/*
53
** If expression is a numeric constant, fills 'v' with its value
54
** and returns 1. Otherwise, returns 0.
55
*/
56
75.5M
static int tonumeral (const expdesc *e, TValue *v) {
57
75.5M
  if (hasjumps(e))
58
93.0k
    return 0;  /* not a numeral */
59
75.4M
  switch (e->k) {
60
20.6M
    case VKINT:
61
20.6M
      if (v) setivalue(v, e->u.ival);
62
20.6M
      return 1;
63
5.16M
    case VKFLT:
64
5.16M
      if (v) setfltvalue(v, e->u.nval);
65
5.16M
      return 1;
66
49.6M
    default: return 0;
67
75.4M
  }
68
75.4M
}
69
70
71
/*
72
** Get the constant value from a constant expression
73
*/
74
35.2M
static TValue *const2val (FuncState *fs, const expdesc *e) {
75
35.2M
  lua_assert(e->k == VCONST);
76
35.2M
  return &fs->ls->dyd->actvar.arr[e->u.info].k;
77
35.2M
}
78
79
80
/*
81
** If expression is a constant, fills 'v' with its value
82
** and returns 1. Otherwise, returns 0.
83
*/
84
14.1k
int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
85
14.1k
  if (hasjumps(e))
86
845
    return 0;  /* not a constant */
87
13.3k
  switch (e->k) {
88
634
    case VFALSE:
89
634
      setbfvalue(v);
90
634
      return 1;
91
537
    case VTRUE:
92
537
      setbtvalue(v);
93
537
      return 1;
94
2.40k
    case VNIL:
95
2.40k
      setnilvalue(v);
96
2.40k
      return 1;
97
1.35k
    case VKSTR: {
98
1.35k
      setsvalue(fs->ls->L, v, e->u.strval);
99
1.35k
      return 1;
100
1.35k
    }
101
823
    case VCONST: {
102
823
      setobj(fs->ls->L, v, const2val(fs, e));
103
823
      return 1;
104
823
    }
105
7.56k
    default: return tonumeral(e, v);
106
13.3k
  }
107
13.3k
}
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
2.02M
static Instruction *previousinstruction (FuncState *fs) {
117
2.02M
  static const Instruction invalidinstruction = ~(Instruction)0;
118
2.02M
  if (fs->pc > fs->lasttarget)
119
1.75M
    return &fs->f->code[fs->pc - 1];  /* previous instruction */
120
270k
  else
121
270k
    return cast(Instruction*, &invalidinstruction);
122
2.02M
}
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.21M
void luaK_nil (FuncState *fs, int from, int n) {
132
1.21M
  int l = from + n - 1;  /* last register to set nil */
133
1.21M
  Instruction *previous = previousinstruction(fs);
134
1.21M
  if (GET_OPCODE(*previous) == OP_LOADNIL) {  /* previous is LOADNIL? */
135
53.1k
    int pfrom = GETARG_A(*previous);  /* get previous range */
136
53.1k
    int pl = pfrom + GETARG_B(*previous);
137
53.1k
    if ((pfrom <= from && from <= pl + 1) ||
138
53.1k
        (from <= pfrom && pfrom <= l + 1)) {  /* can connect both? */
139
44.9k
      if (pfrom < from) from = pfrom;  /* from = min(from, pfrom) */
140
44.9k
      if (pl > l) l = pl;  /* l = max(l, pl) */
141
44.9k
      SETARG_A(*previous, from);
142
44.9k
      SETARG_B(*previous, l - from);
143
44.9k
      return;
144
44.9k
    }  /* else go through */
145
53.1k
  }
146
1.17M
  luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0);  /* else no optimization */
147
1.17M
}
148
149
150
/*
151
** Gets the destination address of a jump instruction. Used to traverse
152
** a list of jumps.
153
*/
154
154M
static int getjump (FuncState *fs, int pc) {
155
154M
  int offset = GETARG_sJ(fs->f->code[pc]);
156
154M
  if (offset == NO_JUMP)  /* point to itself represents end of list */
157
105M
    return NO_JUMP;  /* end of list */
158
48.9M
  else
159
48.9M
    return (pc+1)+offset;  /* turn offset into absolute position */
160
154M
}
161
162
163
/*
164
** Fix jump instruction at position 'pc' to jump to 'dest'.
165
** (Jump addresses are relative in Lua)
166
*/
167
128M
static void fixjump (FuncState *fs, int pc, int dest) {
168
128M
  Instruction *jmp = &fs->f->code[pc];
169
128M
  int offset = dest - (pc + 1);
170
128M
  lua_assert(dest != NO_JUMP);
171
128M
  if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
172
0
    luaX_syntaxerror(fs->ls, "control structure too long");
173
128M
  lua_assert(GET_OPCODE(*jmp) == OP_JMP);
174
128M
  SETARG_sJ(*jmp, offset);
175
128M
}
176
177
178
/*
179
** Concatenate jump-list 'l2' into jump-list 'l1'
180
*/
181
106M
void luaK_concat (FuncState *fs, int *l1, int l2) {
182
106M
  if (l2 == NO_JUMP) return;  /* nothing to concatenate? */
183
106M
  else if (*l1 == NO_JUMP)  /* no original list? */
184
105M
    *l1 = l2;  /* 'l1' points to 'l2' */
185
611k
  else {
186
611k
    int list = *l1;
187
611k
    int next;
188
48.8M
    while ((next = getjump(fs, list)) != NO_JUMP)  /* find last element */
189
48.2M
      list = next;
190
611k
    fixjump(fs, list, l2);  /* last element links to 'l2' */
191
611k
  }
192
106M
}
193
194
195
/*
196
** Create a jump instruction and return its position, so its destination
197
** can be fixed later (with 'fixjump').
198
*/
199
105M
int luaK_jump (FuncState *fs) {
200
105M
  return codesJ(fs, OP_JMP, NO_JUMP, 0);
201
105M
}
202
203
204
/*
205
** Code a 'return' instruction
206
*/
207
5.22M
void luaK_ret (FuncState *fs, int first, int nret) {
208
5.22M
  OpCode op;
209
5.22M
  switch (nret) {
210
4.31M
    case 0: op = OP_RETURN0; break;
211
768k
    case 1: op = OP_RETURN1; break;
212
138k
    default: op = OP_RETURN; break;
213
5.22M
  }
214
5.22M
  luaY_checklimit(fs, nret + 1, MAXARG_B, "returns");
215
5.22M
  luaK_codeABC(fs, op, first, nret + 1, 0);
216
5.22M
}
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
105M
static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
224
105M
  luaK_codeABCk(fs, op, A, B, C, k);
225
105M
  return luaK_jump(fs);
226
105M
}
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
417M
int luaK_getlabel (FuncState *fs) {
234
417M
  fs->lasttarget = fs->pc;
235
417M
  return fs->pc;
236
417M
}
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
210M
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
245
210M
  Instruction *pi = &fs->f->code[pc];
246
210M
  if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
247
209M
    return pi-1;
248
494k
  else
249
494k
    return pi;
250
210M
}
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
105M
static int patchtestreg (FuncState *fs, int node, int reg) {
261
105M
  Instruction *i = getjumpcontrol(fs, node);
262
105M
  if (GET_OPCODE(*i) != OP_TESTSET)
263
105M
    return 0;  /* cannot patch other instructions */
264
492k
  if (reg != NO_REG && reg != GETARG_B(*i))
265
492k
    SETARG_A(*i, reg);
266
455k
  else {
267
     /* no register to put value or register already has the value;
268
        change instruction to simple test */
269
455k
    *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
270
455k
  }
271
492k
  return 1;
272
492k
}
273
274
275
/*
276
** Traverse a list of tests ensuring no one produces a value
277
*/
278
270k
static void removevalues (FuncState *fs, int list) {
279
383k
  for (; list != NO_JUMP; list = getjump(fs, list))
280
112k
      patchtestreg(fs, list, NO_REG);
281
270k
}
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
314M
                          int dtarget) {
291
420M
  while (list != NO_JUMP) {
292
105M
    int next = getjump(fs, list);
293
105M
    if (patchtestreg(fs, list, reg))
294
475k
      fixjump(fs, list, vtarget);
295
105M
    else
296
105M
      fixjump(fs, list, dtarget);  /* jump to default target */
297
105M
    list = next;
298
105M
  }
299
314M
}
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
106M
void luaK_patchlist (FuncState *fs, int list, int target) {
308
106M
  lua_assert(target <= fs->pc);
309
106M
  patchlistaux(fs, list, target, NO_REG, target);
310
106M
}
311
312
313
106M
void luaK_patchtohere (FuncState *fs, int list) {
314
106M
  int hr = luaK_getlabel(fs);  /* mark "here" as a jump target */
315
106M
  luaK_patchlist(fs, list, hr);
316
106M
}
317
318
319
/* limit for difference between lines in relative line info. */
320
1.54G
#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
773M
static void savelineinfo (FuncState *fs, Proto *f, int line) {
331
773M
  int linedif = line - fs->previousline;
332
773M
  int pc = fs->pc - 1;  /* last instruction coded */
333
773M
  if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
334
5.81M
    luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
335
5.81M
                    f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
336
5.81M
    f->abslineinfo[fs->nabslineinfo].pc = pc;
337
5.81M
    f->abslineinfo[fs->nabslineinfo++].line = line;
338
5.81M
    linedif = ABSLINEINFO;  /* signal that there is absolute information */
339
5.81M
    fs->iwthabs = 1;  /* restart counter */
340
5.81M
  }
341
773M
  luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
342
773M
                  INT_MAX, "opcodes");
343
773M
  f->lineinfo[pc] = cast(ls_byte, linedif);
344
773M
  fs->previousline = line;  /* last line saved */
345
773M
}
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
50.0M
static void removelastlineinfo (FuncState *fs) {
355
50.0M
  Proto *f = fs->f;
356
50.0M
  int pc = fs->pc - 1;  /* last instruction coded */
357
50.0M
  if (f->lineinfo[pc] != ABSLINEINFO) {  /* relative line info? */
358
49.5M
    fs->previousline -= f->lineinfo[pc];  /* correct last line saved */
359
49.5M
    fs->iwthabs--;  /* undo previous increment */
360
49.5M
  }
361
461k
  else {  /* absolute line information */
362
461k
    lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
363
461k
    fs->nabslineinfo--;  /* remove it */
364
461k
    fs->iwthabs = MAXIWTHABS + 1;  /* force next line info to be absolute */
365
461k
  }
366
50.0M
}
367
368
369
/*
370
** Remove the last instruction created, correcting line information
371
** accordingly.
372
*/
373
20.9k
static void removelastinstruction (FuncState *fs) {
374
20.9k
  removelastlineinfo(fs);
375
20.9k
  fs->pc--;
376
20.9k
}
377
378
379
/*
380
** Emit instruction 'i', checking for array sizes and saving also its
381
** line information. Return 'i' position.
382
*/
383
723M
int luaK_code (FuncState *fs, Instruction i) {
384
723M
  Proto *f = fs->f;
385
  /* put new instruction in code array */
386
723M
  luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
387
723M
                  INT_MAX, "opcodes");
388
723M
  f->code[fs->pc++] = i;
389
723M
  savelineinfo(fs, f, fs->ls->lastline);
390
723M
  return fs->pc - 1;  /* index of new instruction */
391
723M
}
392
393
394
/*
395
** Format and emit an 'iABC' instruction. (Assertions check consistency
396
** of parameters versus opcode.)
397
*/
398
532M
int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
399
532M
  lua_assert(getOpMode(o) == iABC);
400
532M
  lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
401
532M
             C <= MAXARG_C && (k & ~1) == 0);
402
532M
  return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
403
532M
}
404
405
406
3.38M
int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
407
3.38M
  lua_assert(getOpMode(o) == ivABC);
408
3.38M
  lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
409
3.38M
             C <= MAXARG_vC && (k & ~1) == 0);
410
3.38M
  return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
411
3.38M
}
412
413
414
/*
415
** Format and emit an 'iABx' instruction.
416
*/
417
61.1M
int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) {
418
61.1M
  lua_assert(getOpMode(o) == iABx);
419
61.1M
  lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
420
61.1M
  return luaK_code(fs, CREATE_ABx(o, A, Bc));
421
61.1M
}
422
423
424
/*
425
** Format and emit an 'iAsBx' instruction.
426
*/
427
9.56M
static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
428
9.56M
  int b = Bc + OFFSET_sBx;
429
9.56M
  lua_assert(getOpMode(o) == iAsBx);
430
9.56M
  lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
431
9.56M
  return luaK_code(fs, CREATE_ABx(o, A, b));
432
9.56M
}
433
434
435
/*
436
** Format and emit an 'isJ' instruction.
437
*/
438
105M
static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
439
105M
  int j = sj + OFFSET_sJ;
440
105M
  lua_assert(getOpMode(o) == isJ);
441
105M
  lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
442
105M
  return luaK_code(fs, CREATE_sJ(o, j, k));
443
105M
}
444
445
446
/*
447
** Emit an "extra argument" instruction (format 'iAx')
448
*/
449
7.92M
static int codeextraarg (FuncState *fs, int A) {
450
7.92M
  lua_assert(A <= MAXARG_Ax);
451
7.92M
  return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
452
7.92M
}
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
59.6M
static int luaK_codek (FuncState *fs, int reg, int k) {
461
59.6M
  if (k <= MAXARG_Bx)
462
51.8M
    return luaK_codeABx(fs, OP_LOADK, reg, k);
463
7.80M
  else {
464
7.80M
    int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
465
7.80M
    codeextraarg(fs, k);
466
7.80M
    return p;
467
7.80M
  }
468
59.6M
}
469
470
471
/*
472
** Check register-stack level, keeping track of its maximum size
473
** in field 'maxstacksize'
474
*/
475
352M
void luaK_checkstack (FuncState *fs, int n) {
476
352M
  int newstack = fs->freereg + n;
477
352M
  if (newstack > fs->f->maxstacksize) {
478
11.5M
    luaY_checklimit(fs, newstack, MAX_FSTACK, "registers");
479
11.5M
    fs->f->maxstacksize = cast_byte(newstack);
480
11.5M
  }
481
352M
}
482
483
484
/*
485
** Reserve 'n' registers in register stack
486
*/
487
352M
void luaK_reserveregs (FuncState *fs, int n) {
488
352M
  luaK_checkstack(fs, n);
489
352M
  fs->freereg =  cast_byte(fs->freereg + n);
490
352M
}
491
492
493
/*
494
** Free register 'reg', if it is neither a constant index nor
495
** a local variable.
496
)
497
*/
498
340M
static void freereg (FuncState *fs, int reg) {
499
340M
  if (reg >= luaY_nvarstack(fs)) {
500
323M
    fs->freereg--;
501
323M
    lua_assert(reg == fs->freereg);
502
323M
  }
503
340M
}
504
505
506
/*
507
** Free two registers in proper order
508
*/
509
147M
static void freeregs (FuncState *fs, int r1, int r2) {
510
147M
  if (r1 > r2) {
511
99.2M
    freereg(fs, r1);
512
99.2M
    freereg(fs, r2);
513
99.2M
  }
514
47.9M
  else {
515
47.9M
    freereg(fs, r2);
516
47.9M
    freereg(fs, r1);
517
47.9M
  }
518
147M
}
519
520
521
/*
522
** Free register used by expression 'e' (if any)
523
*/
524
365M
static void freeexp (FuncState *fs, expdesc *e) {
525
365M
  if (e->k == VNONRELOC)
526
23.3M
    freereg(fs, e->u.info);
527
365M
}
528
529
530
/*
531
** Free registers used by expressions 'e1' and 'e2' (if any) in proper
532
** order.
533
*/
534
123M
static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
535
123M
  int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
536
123M
  int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
537
123M
  freeregs(fs, r1, r2);
538
123M
}
539
540
541
/*
542
** Add constant 'v' to prototype's list of constants (field 'k').
543
*/
544
52.6M
static int addk (FuncState *fs, Proto *f, TValue *v) {
545
52.6M
  lua_State *L = fs->ls->L;
546
52.6M
  int oldsize = f->sizek;
547
52.6M
  int k = fs->nk;
548
52.6M
  luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
549
133M
  while (oldsize < f->sizek)
550
81.2M
    setnilvalue(&f->k[oldsize++]);
551
52.6M
  setobj(L, &f->k[k], v);
552
52.6M
  fs->nk++;
553
52.6M
  luaC_barrier(L, f, v);
554
52.6M
  return k;
555
52.6M
}
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
144M
static int k2proto (FuncState *fs, TValue *key, TValue *v) {
565
144M
  TValue val;
566
144M
  Proto *f = fs->f;
567
144M
  int tag = luaH_get(fs->kcache, key, &val);  /* query scanner table */
568
144M
  int k;
569
144M
  if (!tagisempty(tag)) {  /* is there an index there? */
570
119M
    k = cast_int(ivalue(&val));
571
    /* collisions can happen only for float keys */
572
119M
    lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v));
573
119M
    return k;  /* reuse index */
574
119M
  }
575
  /* constant not found; create a new entry */
576
25.2M
  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
25.2M
  setivalue(&val, k);
580
25.2M
  luaH_set(fs->ls->L, fs->kcache, key, &val);
581
25.2M
  return k;
582
144M
}
583
584
585
/*
586
** Add a string to list of constants and return its index.
587
*/
588
133M
static int stringK (FuncState *fs, TString *s) {
589
133M
  TValue o;
590
133M
  setsvalue(fs->ls->L, &o, s);
591
133M
  return k2proto(fs, &o, &o);  /* use string itself as key */
592
133M
}
593
594
595
/*
596
** Add an integer to list of constants and return its index.
597
*/
598
4.79M
static int luaK_intK (FuncState *fs, lua_Integer n) {
599
4.79M
  TValue o;
600
4.79M
  setivalue(&o, n);
601
4.79M
  return k2proto(fs, &o, &o);  /* use integer itself as key */
602
4.79M
}
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
33.5M
static int luaK_numberK (FuncState *fs, lua_Number r) {
616
33.5M
  TValue o, kv;
617
33.5M
  setfltvalue(&o, r);  /* value as a TValue */
618
33.5M
  if (r == 0) {  /* handle zero as a special case */
619
107k
    setpvalue(&kv, fs);  /* use FuncState as index */
620
107k
    return k2proto(fs, &kv, &o);  /* cannot collide */
621
107k
  }
622
33.4M
  else {
623
33.4M
    const int nbm = l_floatatt(MANT_DIG);
624
33.4M
    const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
625
33.4M
    const lua_Number k =  r * (1 + q);  /* key */
626
33.4M
    lua_Integer ik;
627
33.4M
    setfltvalue(&kv, k);  /* key as a TValue */
628
33.4M
    if (!luaV_flttointeger(k, &ik, F2Ieq)) {  /* not an integral value? */
629
6.05M
      int n = k2proto(fs, &kv, &o);  /* use key */
630
6.05M
      if (luaV_rawequalobj(&fs->f->k[n], &o))  /* correct value? */
631
6.05M
        return n;
632
6.05M
    }
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
27.3M
    return addk(fs, fs->f, &o);
636
33.4M
  }
637
33.5M
}
638
639
640
/*
641
** Add a false to list of constants and return its index.
642
*/
643
9.98k
static int boolF (FuncState *fs) {
644
9.98k
  TValue o;
645
9.98k
  setbfvalue(&o);
646
9.98k
  return k2proto(fs, &o, &o);  /* use boolean itself as key */
647
9.98k
}
648
649
650
/*
651
** Add a true to list of constants and return its index.
652
*/
653
19.3k
static int boolT (FuncState *fs) {
654
19.3k
  TValue o;
655
19.3k
  setbtvalue(&o);
656
19.3k
  return k2proto(fs, &o, &o);  /* use boolean itself as key */
657
19.3k
}
658
659
660
/*
661
** Add nil to list of constants and return its index.
662
*/
663
88.4k
static int nilK (FuncState *fs) {
664
88.4k
  TValue k, v;
665
88.4k
  setnilvalue(&v);
666
  /* cannot use nil as key; instead use table itself */
667
88.4k
  sethvalue(fs->ls->L, &k, fs->kcache);
668
88.4k
  return k2proto(fs, &k, &v);
669
88.4k
}
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
23.5M
static int fitsC (lua_Integer i) {
678
23.5M
  return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
679
23.5M
}
680
681
682
/*
683
** Check whether 'i' can be stored in an 'sBx' operand.
684
*/
685
37.2M
static int fitsBx (lua_Integer i) {
686
37.2M
  return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
687
37.2M
}
688
689
690
3.73M
void luaK_int (FuncState *fs, int reg, lua_Integer i) {
691
3.73M
  if (fitsBx(i))
692
3.41M
    codeAsBx(fs, OP_LOADI, reg, cast_int(i));
693
325k
  else
694
325k
    luaK_codek(fs, reg, luaK_intK(fs, i));
695
3.73M
}
696
697
698
35.6M
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
699
35.6M
  lua_Integer fi;
700
35.6M
  if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
701
6.15M
    codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
702
29.5M
  else
703
29.5M
    luaK_codek(fs, reg, luaK_numberK(fs, f));
704
35.6M
}
705
706
707
/*
708
** Convert a constant in 'v' into an expression description 'e'
709
*/
710
35.2M
static void const2exp (TValue *v, expdesc *e) {
711
35.2M
  switch (ttypetag(v)) {
712
48.7k
    case LUA_VNUMINT:
713
48.7k
      e->k = VKINT; e->u.ival = ivalue(v);
714
0
      break;
715
32.1M
    case LUA_VNUMFLT:
716
32.1M
      e->k = VKFLT; e->u.nval = fltvalue(v);
717
0
      break;
718
2.47M
    case LUA_VFALSE:
719
2.47M
      e->k = VFALSE;
720
2.47M
      break;
721
4.46k
    case LUA_VTRUE:
722
4.46k
      e->k = VTRUE;
723
4.46k
      break;
724
561k
    case LUA_VNIL:
725
561k
      e->k = VNIL;
726
561k
      break;
727
9.14k
    case LUA_VSHRSTR:  case LUA_VLNGSTR:
728
9.14k
      e->k = VKSTR; e->u.strval = tsvalue(v);
729
0
      break;
730
0
    default: lua_assert(0);
731
35.2M
  }
732
35.2M
}
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
455k
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
740
455k
  Instruction *pc = &getinstruction(fs, e);
741
455k
  luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results");
742
455k
  if (e->k == VCALL)  /* expression is an open function call? */
743
455k
    SETARG_C(*pc, nresults + 1);
744
84.3k
  else {
745
84.3k
    lua_assert(e->k == VVARARG);
746
84.3k
    SETARG_C(*pc, nresults + 1);
747
84.3k
    SETARG_A(*pc, fs->freereg);
748
84.3k
    luaK_reserveregs(fs, 1);
749
84.3k
  }
750
455k
}
751
752
753
/*
754
** Convert a VKSTR to a VK
755
*/
756
132M
static int str2K (FuncState *fs, expdesc *e) {
757
132M
  lua_assert(e->k == VKSTR);
758
132M
  e->u.info = stringK(fs, e->u.strval);
759
132M
  e->k = VK;
760
132M
  return e->u.info;
761
132M
}
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
4.69M
void luaK_setoneret (FuncState *fs, expdesc *e) {
775
4.69M
  if (e->k == VCALL) {  /* expression is an open function call? */
776
    /* already returns 1 value */
777
2.90M
    lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
778
2.90M
    e->k = VNONRELOC;  /* result has fixed position */
779
2.90M
    e->u.info = GETARG_A(getinstruction(fs, e));
780
2.90M
  }
781
1.78M
  else if (e->k == VVARARG) {
782
32.5k
    SETARG_C(getinstruction(fs, e), 2);
783
32.5k
    e->k = VRELOC;  /* can relocate its simple result */
784
32.5k
  }
785
4.69M
}
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.41G
void luaK_dischargevars (FuncState *fs, expdesc *e) {
793
1.41G
  switch (e->k) {
794
35.2M
    case VCONST: {
795
35.2M
      const2exp(const2val(fs, e), e);
796
35.2M
      break;
797
0
    }
798
9.31M
    case VLOCAL: {  /* already in a register */
799
9.31M
      int temp = e->u.var.ridx;
800
9.31M
      e->u.info = temp;  /* (can't do a direct assignment; values overlap) */
801
9.31M
      e->k = VNONRELOC;  /* becomes a non-relocatable value */
802
9.31M
      break;
803
0
    }
804
24.7M
    case VUPVAL: {  /* move value to some (pending) register */
805
24.7M
      e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
806
24.7M
      e->k = VRELOC;
807
24.7M
      break;
808
0
    }
809
66.9M
    case VINDEXUP: {
810
66.9M
      e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
811
66.9M
      e->k = VRELOC;
812
66.9M
      break;
813
0
    }
814
14.3k
    case VINDEXI: {
815
14.3k
      freereg(fs, e->u.ind.t);
816
14.3k
      e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
817
14.3k
      e->k = VRELOC;
818
14.3k
      break;
819
0
    }
820
22.5M
    case VINDEXSTR: {
821
22.5M
      freereg(fs, e->u.ind.t);
822
22.5M
      e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
823
22.5M
      e->k = VRELOC;
824
22.5M
      break;
825
0
    }
826
23.8M
    case VINDEXED: {
827
23.8M
      freeregs(fs, e->u.ind.t, e->u.ind.idx);
828
23.8M
      e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
829
23.8M
      e->k = VRELOC;
830
23.8M
      break;
831
0
    }
832
2.74M
    case VVARARG: case VCALL: {
833
2.74M
      luaK_setoneret(fs, e);
834
2.74M
      break;
835
28.4k
    }
836
1.22G
    default: break;  /* there is one value available (somewhere) */
837
1.41G
  }
838
1.41G
}
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
346M
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
847
346M
  luaK_dischargevars(fs, e);
848
346M
  switch (e->k) {
849
768k
    case VNIL: {
850
768k
      luaK_nil(fs, reg, 1);
851
768k
      break;
852
0
    }
853
2.55M
    case VFALSE: {
854
2.55M
      luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
855
2.55M
      break;
856
0
    }
857
102k
    case VTRUE: {
858
102k
      luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
859
102k
      break;
860
0
    }
861
3.01M
    case VKSTR: {
862
3.01M
      str2K(fs, e);
863
3.01M
    }  /* FALLTHROUGH */
864
29.7M
    case VK: {
865
29.7M
      luaK_codek(fs, reg, e->u.info);
866
29.7M
      break;
867
3.01M
    }
868
35.6M
    case VKFLT: {
869
35.6M
      luaK_float(fs, reg, e->u.nval);
870
35.6M
      break;
871
3.01M
    }
872
3.69M
    case VKINT: {
873
3.69M
      luaK_int(fs, reg, e->u.ival);
874
3.69M
      break;
875
3.01M
    }
876
165M
    case VRELOC: {
877
165M
      Instruction *pc = &getinstruction(fs, e);
878
165M
      SETARG_A(*pc, reg);  /* instruction will put result in 'reg' */
879
165M
      break;
880
3.01M
    }
881
5.30M
    case VNONRELOC: {
882
5.30M
      if (reg != e->u.info)
883
2.48M
        luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
884
5.30M
      break;
885
3.01M
    }
886
103M
    default: {
887
103M
      lua_assert(e->k == VJMP);
888
103M
      return;  /* nothing to do... */
889
103M
    }
890
346M
  }
891
243M
  e->u.info = reg;
892
243M
  e->k = VNONRELOC;
893
243M
}
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
627k
static void discharge2anyreg (FuncState *fs, expdesc *e) {
902
627k
  if (e->k != VNONRELOC) {  /* no fixed register yet? */
903
479k
    luaK_reserveregs(fs, 1);  /* get a register */
904
479k
    discharge2reg(fs, e, fs->freereg-1);  /* put value there */
905
479k
  }
906
627k
}
907
908
909
207M
static int code_loadbool (FuncState *fs, int A, OpCode op) {
910
207M
  luaK_getlabel(fs);  /* those instructions may be jump targets */
911
207M
  return luaK_codeABC(fs, op, A, 0, 0);
912
207M
}
913
914
915
/*
916
** check whether list has any jump that do not produce a value
917
** or produce an inverted value
918
*/
919
104M
static int need_value (FuncState *fs, int list) {
920
104M
  for (; list != NO_JUMP; list = getjump(fs, list)) {
921
103M
    Instruction i = *getjumpcontrol(fs, list);
922
103M
    if (GET_OPCODE(i) != OP_TESTSET) return 1;
923
103M
  }
924
594k
  return 0;  /* not found */
925
104M
}
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
346M
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
936
346M
  discharge2reg(fs, e, reg);
937
346M
  if (e->k == VJMP)  /* expression itself is a test? */
938
103M
    luaK_concat(fs, &e->t, e->u.info);  /* put this jump in 't' list */
939
346M
  if (hasjumps(e)) {
940
103M
    int final;  /* position after whole expression */
941
103M
    int p_f = NO_JUMP;  /* position of an eventual LOAD false */
942
103M
    int p_t = NO_JUMP;  /* position of an eventual LOAD true */
943
103M
    if (need_value(fs, e->t) || need_value(fs, e->f)) {
944
103M
      int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
945
103M
      p_f = code_loadbool(fs, reg, OP_LFALSESKIP);  /* skip next inst. */
946
103M
      p_t = code_loadbool(fs, reg, OP_LOADTRUE);
947
      /* jump around these booleans if 'e' is not a test */
948
103M
      luaK_patchtohere(fs, fj);
949
103M
    }
950
103M
    final = luaK_getlabel(fs);
951
103M
    patchlistaux(fs, e->f, final, reg, p_f);
952
103M
    patchlistaux(fs, e->t, final, reg, p_t);
953
103M
  }
954
346M
  e->f = e->t = NO_JUMP;
955
346M
  e->u.info = reg;
956
346M
  e->k = VNONRELOC;
957
346M
}
958
959
960
/*
961
** Ensures final expression result is in next available register.
962
*/
963
345M
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
964
345M
  luaK_dischargevars(fs, e);
965
345M
  freeexp(fs, e);
966
345M
  luaK_reserveregs(fs, 1);
967
345M
  exp2reg(fs, e, fs->freereg - 1);
968
345M
}
969
970
971
/*
972
** Ensures final expression result is in some (any) register
973
** and return that register.
974
*/
975
450M
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
976
450M
  luaK_dischargevars(fs, e);
977
450M
  if (e->k == VNONRELOC) {  /* expression already has a register? */
978
132M
    if (!hasjumps(e))  /* no jumps? */
979
132M
      return e->u.info;  /* result is already in a register */
980
149k
    if (e->u.info >= luaY_nvarstack(fs)) {  /* reg. is not a local? */
981
75.3k
      exp2reg(fs, e, e->u.info);  /* put final result in it */
982
75.3k
      return e->u.info;
983
75.3k
    }
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
149k
  }
988
318M
  luaK_exp2nextreg(fs, e);  /* default: use next available register */
989
318M
  return e->u.info;
990
450M
}
991
992
993
/*
994
** Ensures final expression result is either in a register
995
** or in an upvalue.
996
*/
997
125M
void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
998
125M
  if (e->k != VUPVAL || hasjumps(e))
999
24.8M
    luaK_exp2anyreg(fs, e);
1000
125M
}
1001
1002
1003
/*
1004
** Ensures final expression result is either in a register
1005
** or it is a constant.
1006
*/
1007
429k
void luaK_exp2val (FuncState *fs, expdesc *e) {
1008
429k
  if (e->k == VJMP || hasjumps(e))
1009
10.3k
    luaK_exp2anyreg(fs, e);
1010
418k
  else
1011
418k
    luaK_dischargevars(fs, e);
1012
429k
}
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
16.9M
static int luaK_exp2K (FuncState *fs, expdesc *e) {
1020
16.9M
  if (!hasjumps(e)) {
1021
16.9M
    int info;
1022
16.9M
    switch (e->k) {  /* move constants to 'k' */
1023
19.3k
      case VTRUE: info = boolT(fs); break;
1024
9.98k
      case VFALSE: info = boolF(fs); break;
1025
88.4k
      case VNIL: info = nilK(fs); break;
1026
4.46M
      case VKINT: info = luaK_intK(fs, e->u.ival); break;
1027
3.99M
      case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
1028
878k
      case VKSTR: info = stringK(fs, e->u.strval); break;
1029
12.9k
      case VK: info = e->u.info; break;
1030
7.43M
      default: return 0;  /* not a constant */
1031
16.9M
    }
1032
9.46M
    if (info <= MAXINDEXRK) {  /* does constant fit in 'argC'? */
1033
6.39M
      e->k = VK;  /* make expression a 'K' expression */
1034
6.39M
      e->u.info = info;
1035
6.39M
      return 1;
1036
6.39M
    }
1037
9.46M
  }
1038
  /* else, expression doesn't fit; leave it unchanged */
1039
3.12M
  return 0;
1040
16.9M
}
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
11.5M
static int exp2RK (FuncState *fs, expdesc *e) {
1050
11.5M
  if (luaK_exp2K(fs, e))
1051
1.30M
    return 1;
1052
10.2M
  else {  /* not a constant in the right range: put it in a register */
1053
10.2M
    luaK_exp2anyreg(fs, e);
1054
10.2M
    return 0;
1055
10.2M
  }
1056
11.5M
}
1057
1058
1059
static void codeABRK (FuncState *fs, OpCode o, int A, int B,
1060
9.91M
                      expdesc *ec) {
1061
9.91M
  int k = exp2RK(fs, ec);
1062
9.91M
  luaK_codeABCk(fs, o, A, B, ec->u.info, k);
1063
9.91M
}
1064
1065
1066
/*
1067
** Generate code to store result of expression 'ex' into variable 'var'.
1068
*/
1069
10.2M
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
1070
10.2M
  switch (var->k) {
1071
226k
    case VLOCAL: {
1072
226k
      freeexp(fs, ex);
1073
226k
      exp2reg(fs, ex, var->u.var.ridx);  /* compute 'ex' into proper place */
1074
226k
      return;
1075
0
    }
1076
94.1k
    case VUPVAL: {
1077
94.1k
      int e = luaK_exp2anyreg(fs, ex);
1078
94.1k
      luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
1079
94.1k
      break;
1080
0
    }
1081
4.11M
    case VINDEXUP: {
1082
4.11M
      codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
1083
4.11M
      break;
1084
0
    }
1085
19.2k
    case VINDEXI: {
1086
19.2k
      codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
1087
19.2k
      break;
1088
0
    }
1089
2.84M
    case VINDEXSTR: {
1090
2.84M
      codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
1091
2.84M
      break;
1092
0
    }
1093
2.93M
    case VINDEXED: {
1094
2.93M
      codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
1095
2.93M
      break;
1096
0
    }
1097
0
    default: lua_assert(0);  /* invalid var kind to store */
1098
10.2M
  }
1099
10.0M
  freeexp(fs, ex);
1100
10.0M
}
1101
1102
1103
/*
1104
** Negate condition 'e' (where 'e' is a comparison).
1105
*/
1106
799k
static void negatecondition (FuncState *fs, expdesc *e) {
1107
799k
  Instruction *pc = getjumpcontrol(fs, e->u.info);
1108
799k
  lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
1109
799k
                                           GET_OPCODE(*pc) != OP_TEST);
1110
799k
  SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
1111
799k
}
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
563k
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
1121
563k
  if (e->k == VRELOC) {
1122
348k
    Instruction ie = getinstruction(fs, e);
1123
348k
    if (GET_OPCODE(ie) == OP_NOT) {
1124
20.9k
      removelastinstruction(fs);  /* remove previous OP_NOT */
1125
20.9k
      return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
1126
20.9k
    }
1127
    /* else go through */
1128
348k
  }
1129
542k
  discharge2anyreg(fs, e);
1130
542k
  freeexp(fs, e);
1131
542k
  return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
1132
563k
}
1133
1134
1135
/*
1136
** Emit code to go through if 'e' is true, jump otherwise.
1137
*/
1138
1.12M
void luaK_goiftrue (FuncState *fs, expdesc *e) {
1139
1.12M
  int pc;  /* pc of new jump */
1140
1.12M
  luaK_dischargevars(fs, e);
1141
1.12M
  switch (e->k) {
1142
783k
    case VJMP: {  /* condition? */
1143
783k
      negatecondition(fs, e);  /* jump when it is false */
1144
783k
      pc = e->u.info;  /* save jump position */
1145
783k
      break;
1146
0
    }
1147
118k
    case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1148
118k
      pc = NO_JUMP;  /* always true; do nothing */
1149
118k
      break;
1150
100k
    }
1151
227k
    default: {
1152
227k
      pc = jumponcond(fs, e, 0);  /* jump when false */
1153
227k
      break;
1154
100k
    }
1155
1.12M
  }
1156
1.12M
  luaK_concat(fs, &e->f, pc);  /* insert new jump in false list */
1157
1.12M
  luaK_patchtohere(fs, e->t);  /* true list jumps to here (to go through) */
1158
1.12M
  e->t = NO_JUMP;
1159
1.12M
}
1160
1161
1162
/*
1163
** Emit code to go through if 'e' is false, jump otherwise.
1164
*/
1165
732k
void luaK_goiffalse (FuncState *fs, expdesc *e) {
1166
732k
  int pc;  /* pc of new jump */
1167
732k
  luaK_dischargevars(fs, e);
1168
732k
  switch (e->k) {
1169
385k
    case VJMP: {
1170
385k
      pc = e->u.info;  /* already jump if true */
1171
385k
      break;
1172
0
    }
1173
10.7k
    case VNIL: case VFALSE: {
1174
10.7k
      pc = NO_JUMP;  /* always false; do nothing */
1175
10.7k
      break;
1176
5.18k
    }
1177
336k
    default: {
1178
336k
      pc = jumponcond(fs, e, 1);  /* jump if true */
1179
336k
      break;
1180
5.18k
    }
1181
732k
  }
1182
732k
  luaK_concat(fs, &e->t, pc);  /* insert new jump in 't' list */
1183
732k
  luaK_patchtohere(fs, e->f);  /* false list jumps to here (to go through) */
1184
732k
  e->f = NO_JUMP;
1185
732k
}
1186
1187
1188
/*
1189
** Code 'not e', doing constant folding.
1190
*/
1191
135k
static void codenot (FuncState *fs, expdesc *e) {
1192
135k
  switch (e->k) {
1193
8.27k
    case VNIL: case VFALSE: {
1194
8.27k
      e->k = VTRUE;  /* true == not nil == not false */
1195
8.27k
      break;
1196
1.91k
    }
1197
26.5k
    case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1198
26.5k
      e->k = VFALSE;  /* false == not "x" == not 0.5 == not 1 == not true */
1199
26.5k
      break;
1200
24.1k
    }
1201
15.5k
    case VJMP: {
1202
15.5k
      negatecondition(fs, e);
1203
15.5k
      break;
1204
24.1k
    }
1205
42.7k
    case VRELOC:
1206
84.8k
    case VNONRELOC: {
1207
84.8k
      discharge2anyreg(fs, e);
1208
84.8k
      freeexp(fs, e);
1209
84.8k
      e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
1210
84.8k
      e->k = VRELOC;
1211
84.8k
      break;
1212
42.7k
    }
1213
0
    default: lua_assert(0);  /* cannot happen */
1214
135k
  }
1215
  /* interchange true and false lists */
1216
135k
  { int temp = e->f; e->f = e->t; e->t = temp; }
1217
135k
  removevalues(fs, e->f);  /* values are useless when negated */
1218
135k
  removevalues(fs, e->t);
1219
135k
}
1220
1221
1222
/*
1223
** Check whether expression 'e' is a short literal string
1224
*/
1225
230M
static int isKstr (FuncState *fs, expdesc *e) {
1226
230M
  return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
1227
230M
          ttisshrstring(&fs->f->k[e->u.info]));
1228
230M
}
1229
1230
/*
1231
** Check whether expression 'e' is a literal integer.
1232
*/
1233
31.5M
static int isKint (expdesc *e) {
1234
31.5M
  return (e->k == VKINT && !hasjumps(e));
1235
31.5M
}
1236
1237
1238
/*
1239
** Check whether expression 'e' is a literal integer in
1240
** proper range to fit in register C
1241
*/
1242
26.9M
static int isCint (expdesc *e) {
1243
26.9M
  return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
1244
26.9M
}
1245
1246
1247
/*
1248
** Check whether expression 'e' is a literal integer in
1249
** proper range to fit in register sC
1250
*/
1251
2.52M
static int isSCint (expdesc *e) {
1252
2.52M
  return isKint(e) && fitsC(e->u.ival);
1253
2.52M
}
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
312M
static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
1261
312M
  lua_Integer i;
1262
312M
  if (e->k == VKINT)
1263
1.88M
    i = e->u.ival;
1264
310M
  else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
1265
20.1M
    *isfloat = 1;
1266
290M
  else
1267
290M
    return 0;  /* not a number */
1268
22.0M
  if (!hasjumps(e) && fitsC(i)) {
1269
2.16M
    *pi = int2sC(cast_int(i));
1270
2.16M
    return 1;
1271
2.16M
  }
1272
19.8M
  else
1273
19.8M
    return 0;
1274
22.0M
}
1275
1276
1277
/*
1278
** Emit SELF instruction or equivalent: the code will convert
1279
** expression 'e' into 'e.key(e,'.
1280
*/
1281
294k
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
1282
294k
  int ereg, base;
1283
294k
  luaK_exp2anyreg(fs, e);
1284
294k
  ereg = e->u.info;  /* register where 'e' (the receiver) was placed */
1285
294k
  freeexp(fs, e);
1286
294k
  base = e->u.info = fs->freereg;  /* base register for op_self */
1287
294k
  e->k = VNONRELOC;  /* self expression has a fixed register */
1288
294k
  luaK_reserveregs(fs, 2);  /* method and 'self' produced by op_self */
1289
294k
  lua_assert(key->k == VKSTR);
1290
  /* is method name a short string in a valid K index? */
1291
294k
  if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) {
1292
    /* can use 'self' opcode */
1293
290k
    luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0);
1294
290k
  }
1295
4.21k
  else {  /* cannot use 'self' opcode; use move+gettable */
1296
4.21k
    luaK_exp2anyreg(fs, key);  /* put method name in a register */
1297
4.21k
    luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0);  /* copy self to base+1 */
1298
4.21k
    luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info);  /* get method */
1299
4.21k
  }
1300
294k
  freeexp(fs, key);
1301
294k
}
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
129M
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1311
129M
  int keystr = -1;
1312
129M
  if (k->k == VKSTR)
1313
129M
    keystr = str2K(fs, k);
1314
129M
  lua_assert(!hasjumps(t) &&
1315
129M
             (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
1316
129M
  if (t->k == VUPVAL && !isKstr(fs, k))  /* upvalue indexed by non 'Kstr'? */
1317
22.5M
    luaK_exp2anyreg(fs, t);  /* put it in a register */
1318
129M
  if (t->k == VUPVAL) {
1319
77.6M
    lu_byte temp = cast_byte(t->u.info);  /* upvalue index */
1320
77.6M
    t->u.ind.t = temp;  /* (can't do a direct assignment; values overlap) */
1321
77.6M
    lua_assert(isKstr(fs, k));
1322
77.6M
    t->u.ind.idx = cast_short(k->u.info);  /* literal short string */
1323
77.6M
    t->k = VINDEXUP;
1324
77.6M
  }
1325
52.3M
  else {
1326
    /* register index of the table */
1327
52.3M
    t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
1328
52.3M
    if (isKstr(fs, k)) {
1329
25.4M
      t->u.ind.idx = cast_short(k->u.info);  /* literal short string */
1330
25.4M
      t->k = VINDEXSTR;
1331
25.4M
    }
1332
26.9M
    else if (isCint(k)) {  /* int. constant in proper range? */
1333
36.8k
      t->u.ind.idx = cast_short(k->u.ival);
1334
36.8k
      t->k = VINDEXI;
1335
36.8k
    }
1336
26.8M
    else {
1337
26.8M
      t->u.ind.idx = cast_short(luaK_exp2anyreg(fs, k));  /* register */
1338
26.8M
      t->k = VINDEXED;
1339
26.8M
    }
1340
52.3M
  }
1341
129M
  t->u.ind.keystr = keystr;  /* string index in 'k' */
1342
129M
  t->u.ind.ro = 0;  /* by default, not read-only */
1343
129M
}
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.92M
static int validop (int op, TValue *v1, TValue *v2) {
1352
6.92M
  switch (op) {
1353
155k
    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
351k
    }
1359
1.11M
    case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD:  /* division by 0 */
1360
1.11M
      return (nvalue(v2) != 0);
1361
4.22M
    default: return 1;  /* everything else is valid */
1362
6.92M
  }
1363
6.92M
}
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
29.9M
                                        const expdesc *e2) {
1372
29.9M
  TValue v1, v2, res;
1373
29.9M
  if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
1374
23.2M
    return 0;  /* non-numeric operands or not safe to fold */
1375
6.69M
  luaO_rawarith(fs->ls->L, op, &v1, &v2, &res);  /* does operation */
1376
6.69M
  if (ttisinteger(&res)) {
1377
2.53M
    e1->k = VKINT;
1378
2.53M
    e1->u.ival = ivalue(&res);
1379
2.53M
  }
1380
4.16M
  else {  /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
1381
4.16M
    lua_Number n = fltvalue(&res);
1382
4.16M
    if (luai_numisnan(n) || n == 0)
1383
2.30M
      return 0;
1384
1.85M
    e1->k = VKFLT;
1385
1.85M
    e1->u.nval = n;
1386
1.85M
  }
1387
4.39M
  return 1;
1388
6.69M
}
1389
1390
1391
/*
1392
** Convert a BinOpr to an OpCode  (ORDER OPR - ORDER OP)
1393
*/
1394
121M
l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1395
121M
  lua_assert(baser <= opr &&
1396
121M
            ((baser == OPR_ADD && opr <= OPR_SHR) ||
1397
121M
             (baser == OPR_LT && opr <= OPR_LE)));
1398
121M
  return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1399
121M
}
1400
1401
1402
/*
1403
** Convert a UnOpr to an OpCode  (ORDER OPR - ORDER OP)
1404
*/
1405
7.10M
l_sinline OpCode unopr2op (UnOpr opr) {
1406
7.10M
  return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1407
7.10M
                                       cast_int(OP_UNM));
1408
7.10M
}
1409
1410
1411
/*
1412
** Convert a BinOpr to a tag method  (ORDER OPR - ORDER TM)
1413
*/
1414
17.8M
l_sinline TMS binopr2TM (BinOpr opr) {
1415
17.8M
  lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1416
17.8M
  return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1417
17.8M
}
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
7.10M
static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
1426
7.10M
  int r = luaK_exp2anyreg(fs, e);  /* opcodes operate only on registers */
1427
7.10M
  freeexp(fs, e);
1428
7.10M
  e->u.info = luaK_codeABC(fs, op, 0, r, 0);  /* generate opcode */
1429
7.10M
  e->k = VRELOC;  /* all those operations are relocatable */
1430
7.10M
  luaK_fixline(fs, line);
1431
7.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
18.7M
                             OpCode mmop, TMS event) {
1443
18.7M
  int v1 = luaK_exp2anyreg(fs, e1);
1444
18.7M
  int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
1445
18.7M
  freeexps(fs, e1, e2);
1446
18.7M
  e1->u.info = pc;
1447
18.7M
  e1->k = VRELOC;  /* all those operations are relocatable */
1448
18.7M
  luaK_fixline(fs, line);
1449
18.7M
  luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip);  /* metamethod */
1450
18.7M
  luaK_fixline(fs, line);
1451
18.7M
}
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
13.0M
                           expdesc *e1, expdesc *e2, int line) {
1460
13.0M
  OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1461
13.0M
  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
13.0M
  lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1464
13.0M
             e1->k == VNONRELOC || e1->k == VRELOC);
1465
13.0M
  lua_assert(OP_ADD <= op && op <= OP_SHR);
1466
13.0M
  finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
1467
13.0M
}
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
573k
                       TMS event) {
1476
573k
  int v2 = int2sC(cast_int(e2->u.ival));  /* immediate operand */
1477
573k
  lua_assert(e2->k == VKINT);
1478
573k
  finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
1479
573k
}
1480
1481
1482
/*
1483
** Code binary operators with K operand.
1484
*/
1485
static void codebinK (FuncState *fs, BinOpr opr,
1486
4.79M
                      expdesc *e1, expdesc *e2, int flip, int line) {
1487
4.79M
  TMS event = binopr2TM(opr);
1488
4.79M
  int v2 = e2->u.info;  /* K index */
1489
4.79M
  OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1490
4.79M
  finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1491
4.79M
}
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.11M
                             OpCode op, int line, TMS event) {
1499
2.11M
  if (!isKint(e2))
1500
1.69M
    return 0;  /* not an integer constant */
1501
421k
  else {
1502
421k
    lua_Integer i2 = e2->u.ival;
1503
421k
    if (!(fitsC(i2) && fitsC(-i2)))
1504
50.6k
      return 0;  /* not in the proper range */
1505
370k
    else {  /* operating a small integer constant */
1506
370k
      int v2 = cast_int(i2);
1507
370k
      finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
1508
      /* correct metamethod argument */
1509
370k
      SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
1510
370k
      return 1;  /* successfully coded */
1511
370k
    }
1512
421k
  }
1513
2.11M
}
1514
1515
1516
93.9M
static void swapexps (expdesc *e1, expdesc *e2) {
1517
93.9M
  expdesc temp = *e1; *e1 = *e2; *e2 = temp;  /* swap 'e1' and 'e2' */
1518
93.9M
}
1519
1520
1521
/*
1522
** Code binary operators with no constant operand.
1523
*/
1524
static void codebinNoK (FuncState *fs, BinOpr opr,
1525
11.6M
                        expdesc *e1, expdesc *e2, int flip, int line) {
1526
11.6M
  if (flip)
1527
72.6k
    swapexps(e1, e2);  /* back to original order */
1528
11.6M
  codebinexpval(fs, opr, e1, e2, line);  /* use standard operators */
1529
11.6M
}
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
13.3M
                       expdesc *e1, expdesc *e2, int flip, int line) {
1538
13.3M
  if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2))  /* K operand? */
1539
4.04M
    codebinK(fs, opr, e1, e2, flip, line);
1540
9.28M
  else  /* 'e2' is neither an immediate nor a K operand */
1541
9.28M
    codebinNoK(fs, opr, e1, e2, flip, line);
1542
13.3M
}
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.70M
                             expdesc *e1, expdesc *e2, int line) {
1552
1.70M
  int flip = 0;
1553
1.70M
  if (tonumeral(e1, NULL)) {  /* is first operand a numeric constant? */
1554
353k
    swapexps(e1, e2);  /* change order */
1555
353k
    flip = 1;
1556
353k
  }
1557
1.70M
  if (op == OPR_ADD && isSCint(e2))  /* immediate operand? */
1558
282k
    codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
1559
1.42M
  else
1560
1.42M
    codearith(fs, op, e1, e2, flip, line);
1561
1.70M
}
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.14M
                         expdesc *e1, expdesc *e2, int line) {
1570
3.14M
  int flip = 0;
1571
3.14M
  if (e1->k == VKINT) {
1572
92.1k
    swapexps(e1, e2);  /* 'e2' will be the constant operand */
1573
92.1k
    flip = 1;
1574
92.1k
  }
1575
3.14M
  if (e2->k == VKINT && luaK_exp2K(fs, e2))  /* K operand? */
1576
748k
    codebinK(fs, opr, e1, e2, flip, line);
1577
2.39M
  else  /* no constants */
1578
2.39M
    codebinNoK(fs, opr, e1, e2, flip, line);
1579
3.14M
}
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
103M
static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1587
103M
  int r1, r2;
1588
103M
  int im;
1589
103M
  int isfloat = 0;
1590
103M
  OpCode op;
1591
103M
  if (isSCnumber(e2, &im, &isfloat)) {
1592
    /* use immediate operand */
1593
148k
    r1 = luaK_exp2anyreg(fs, e1);
1594
148k
    r2 = im;
1595
148k
    op = binopr2op(opr, OPR_LT, OP_LTI);
1596
148k
  }
1597
103M
  else if (isSCnumber(e1, &im, &isfloat)) {
1598
    /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
1599
1.80M
    r1 = luaK_exp2anyreg(fs, e2);
1600
1.80M
    r2 = im;
1601
1.80M
    op = binopr2op(opr, OPR_LT, OP_GTI);
1602
1.80M
  }
1603
101M
  else {  /* regular case, compare two registers */
1604
101M
    r1 = luaK_exp2anyreg(fs, e1);
1605
101M
    r2 = luaK_exp2anyreg(fs, e2);
1606
101M
    op = binopr2op(opr, OPR_LT, OP_LT);
1607
101M
  }
1608
103M
  freeexps(fs, e1, e2);
1609
103M
  e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
1610
103M
  e1->k = VJMP;
1611
103M
}
1612
1613
1614
/*
1615
** Emit code for equality comparisons ('==', '~=').
1616
** 'e1' was already put as RK by 'luaK_infix'.
1617
*/
1618
851k
static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
1619
851k
  int r1, r2;
1620
851k
  int im;
1621
851k
  int isfloat = 0;  /* not needed here, but kept for symmetry */
1622
851k
  OpCode op;
1623
851k
  if (e1->k != VNONRELOC) {
1624
69.6k
    lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
1625
69.6k
    swapexps(e1, e2);
1626
69.6k
  }
1627
851k
  r1 = luaK_exp2anyreg(fs, e1);  /* 1st expression must be in register */
1628
851k
  if (isSCnumber(e2, &im, &isfloat)) {
1629
59.1k
    op = OP_EQI;
1630
59.1k
    r2 = im;  /* immediate operand */
1631
59.1k
  }
1632
792k
  else if (exp2RK(fs, e2)) {  /* 2nd expression is constant? */
1633
569k
    op = OP_EQK;
1634
569k
    r2 = e2->u.info;  /* constant index */
1635
569k
  }
1636
222k
  else {
1637
222k
    op = OP_EQ;  /* will compare two registers */
1638
222k
    r2 = luaK_exp2anyreg(fs, e2);
1639
222k
  }
1640
851k
  freeexps(fs, e1, e2);
1641
851k
  e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
1642
851k
  e1->k = VJMP;
1643
851k
}
1644
1645
1646
/*
1647
** Apply prefix operation 'op' to expression 'e'.
1648
*/
1649
9.32M
void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
1650
9.32M
  static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
1651
9.32M
  luaK_dischargevars(fs, e);
1652
9.32M
  switch (opr) {
1653
8.87M
    case OPR_MINUS: case OPR_BNOT:  /* use 'ef' as fake 2nd operand */
1654
8.87M
      if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef))
1655
2.08M
        break;
1656
      /* else */ /* FALLTHROUGH */
1657
7.10M
    case OPR_LEN:
1658
7.10M
      codeunexpval(fs, unopr2op(opr), e, line);
1659
7.10M
      break;
1660
135k
    case OPR_NOT: codenot(fs, e); break;
1661
0
    default: lua_assert(0);
1662
9.32M
  }
1663
9.32M
}
1664
1665
1666
/*
1667
** Process 1st operand 'v' of binary operation 'op' before reading
1668
** 2nd operand.
1669
*/
1670
128M
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1671
128M
  luaK_dischargevars(fs, v);
1672
128M
  switch (op) {
1673
410k
    case OPR_AND: {
1674
410k
      luaK_goiftrue(fs, v);  /* go ahead only if 'v' is true */
1675
410k
      break;
1676
0
    }
1677
732k
    case OPR_OR: {
1678
732k
      luaK_goiffalse(fs, v);  /* go ahead only if 'v' is false */
1679
732k
      break;
1680
0
    }
1681
886k
    case OPR_CONCAT: {
1682
886k
      luaK_exp2nextreg(fs, v);  /* operand must be on the stack */
1683
886k
      break;
1684
0
    }
1685
2.81M
    case OPR_ADD: case OPR_SUB:
1686
11.0M
    case OPR_MUL: case OPR_DIV: case OPR_IDIV:
1687
16.4M
    case OPR_MOD: case OPR_POW:
1688
19.8M
    case OPR_BAND: case OPR_BOR: case OPR_BXOR:
1689
21.6M
    case OPR_SHL: case OPR_SHR: {
1690
21.6M
      if (!tonumeral(v, NULL))
1691
15.6M
        luaK_exp2anyreg(fs, v);
1692
      /* else keep numeral, which may be folded or used as an immediate
1693
         operand */
1694
21.6M
      break;
1695
20.5M
    }
1696
870k
    case OPR_EQ: case OPR_NE: {
1697
870k
      if (!tonumeral(v, NULL))
1698
811k
        exp2RK(fs, v);
1699
      /* else keep numeral, which may be an immediate operand */
1700
870k
      break;
1701
716k
    }
1702
10.7M
    case OPR_LT: case OPR_LE:
1703
104M
    case OPR_GT: case OPR_GE: {
1704
104M
      int dummy, dummy2;
1705
104M
      if (!isSCnumber(v, &dummy, &dummy2))
1706
103M
        luaK_exp2anyreg(fs, v);
1707
      /* else keep numeral, which may be an immediate operand */
1708
104M
      break;
1709
103M
    }
1710
0
    default: lua_assert(0);
1711
128M
  }
1712
128M
}
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
808k
static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
1720
808k
  Instruction *ie2 = previousinstruction(fs);
1721
808k
  if (GET_OPCODE(*ie2) == OP_CONCAT) {  /* is 'e2' a concatenation? */
1722
256k
    int n = GETARG_B(*ie2);  /* # of elements concatenated in 'e2' */
1723
256k
    lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
1724
256k
    freeexp(fs, e2);
1725
256k
    SETARG_A(*ie2, e1->u.info);  /* correct first element ('e1') */
1726
256k
    SETARG_B(*ie2, n + 1);  /* will concatenate one more element */
1727
256k
  }
1728
551k
  else {  /* 'e2' is not a concatenation */
1729
551k
    luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0);  /* new concat opcode */
1730
551k
    freeexp(fs, e2);
1731
551k
    luaK_fixline(fs, line);
1732
551k
  }
1733
808k
}
1734
1735
1736
/*
1737
** Finalize code for binary operation, after reading 2nd operand.
1738
*/
1739
void luaK_posfix (FuncState *fs, BinOpr opr,
1740
127M
                  expdesc *e1, expdesc *e2, int line) {
1741
127M
  luaK_dischargevars(fs, e2);
1742
127M
  if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2))
1743
2.30M
    return;  /* done by folding */
1744
125M
  switch (opr) {
1745
390k
    case OPR_AND: {
1746
390k
      lua_assert(e1->t == NO_JUMP);  /* list closed by 'luaK_infix' */
1747
390k
      luaK_concat(fs, &e2->f, e1->f);
1748
390k
      *e1 = *e2;
1749
390k
      break;
1750
390k
    }
1751
711k
    case OPR_OR: {
1752
711k
      lua_assert(e1->f == NO_JUMP);  /* list closed by 'luaK_infix' */
1753
711k
      luaK_concat(fs, &e2->t, e1->t);
1754
711k
      *e1 = *e2;
1755
711k
      break;
1756
711k
    }
1757
808k
    case OPR_CONCAT: {  /* e1 .. e2 */
1758
808k
      luaK_exp2nextreg(fs, e2);
1759
808k
      codeconcat(fs, e1, e2, line);
1760
808k
      break;
1761
711k
    }
1762
1.70M
    case OPR_ADD: case OPR_MUL: {
1763
1.70M
      codecommutative(fs, opr, e1, e2, line);
1764
1.70M
      break;
1765
867k
    }
1766
1.52M
    case OPR_SUB: {
1767
1.52M
      if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
1768
354k
        break; /* coded as (r1 + -I) */
1769
      /* ELSE */
1770
1.52M
    }  /* FALLTHROUGH */
1771
11.9M
    case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
1772
11.9M
      codearith(fs, opr, e1, e2, 0, line);
1773
11.9M
      break;
1774
9.27M
    }
1775
3.14M
    case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
1776
3.14M
      codebitwise(fs, opr, e1, e2, line);
1777
3.14M
      break;
1778
763k
    }
1779
707k
    case OPR_SHL: {
1780
707k
      if (isSCint(e1)) {
1781
117k
        swapexps(e1, e2);
1782
117k
        codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL);  /* I << r2 */
1783
117k
      }
1784
589k
      else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
1785
16.2k
        /* coded as (r1 >> -I) */;
1786
16.2k
      }
1787
573k
      else  /* regular case (two registers) */
1788
573k
       codebinexpval(fs, opr, e1, e2, line);
1789
707k
      break;
1790
763k
    }
1791
948k
    case OPR_SHR: {
1792
948k
      if (isSCint(e2))
1793
172k
        codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR);  /* r1 >> I */
1794
775k
      else  /* regular case (two registers) */
1795
775k
        codebinexpval(fs, opr, e1, e2, line);
1796
948k
      break;
1797
763k
    }
1798
851k
    case OPR_EQ: case OPR_NE: {
1799
851k
      codeeq(fs, opr, e1, e2);
1800
851k
      break;
1801
712k
    }
1802
93.1M
    case OPR_GT: case OPR_GE: {
1803
      /* '(a > b)' <=> '(b < a)';  '(a >= b)' <=> '(b <= a)' */
1804
93.1M
      swapexps(e1, e2);
1805
93.1M
      opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1806
93.1M
    }  /* FALLTHROUGH */
1807
103M
    case OPR_LT: case OPR_LE: {
1808
103M
      codeorder(fs, opr, e1, e2);
1809
103M
      break;
1810
103M
    }
1811
0
    default: lua_assert(0);
1812
125M
  }
1813
125M
}
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
50.0M
void luaK_fixline (FuncState *fs, int line) {
1821
50.0M
  removelastlineinfo(fs);
1822
50.0M
  savelineinfo(fs, fs->f, line);
1823
50.0M
}
1824
1825
1826
713k
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
1827
713k
  Instruction *inst = &fs->f->code[pc];
1828
713k
  int extra = asize / (MAXARG_vC + 1);  /* higher bits of array size */
1829
713k
  int rc = asize % (MAXARG_vC + 1);  /* lower bits of array size */
1830
713k
  int k = (extra > 0);  /* true iff needs extra argument */
1831
713k
  hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0;
1832
713k
  *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k);
1833
713k
  *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
1834
713k
}
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
316k
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
1845
316k
  lua_assert(tostore != 0);
1846
316k
  if (tostore == LUA_MULTRET)
1847
42.4k
    tostore = 0;
1848
316k
  if (nelems <= MAXARG_vC)
1849
205k
    luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
1850
111k
  else {
1851
111k
    int extra = nelems / (MAXARG_vC + 1);
1852
111k
    nelems %= (MAXARG_vC + 1);
1853
111k
    luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
1854
111k
    codeextraarg(fs, extra);
1855
111k
  }
1856
316k
  fs->freereg = cast_byte(base + 1);  /* free registers with list values */
1857
316k
}
1858
1859
1860
/*
1861
** return the final target of a jump (skipping jumps to jumps)
1862
*/
1863
22.6M
static int finaltarget (Instruction *code, int i) {
1864
22.6M
  int count;
1865
47.5M
  for (count = 0; count < 100; count++) {  /* avoid infinite loops */
1866
47.4M
    Instruction pc = code[i];
1867
47.4M
    if (GET_OPCODE(pc) != OP_JMP)
1868
22.6M
      break;
1869
24.8M
    else
1870
24.8M
      i += GETARG_sJ(pc) + 1;
1871
47.4M
  }
1872
22.6M
  return i;
1873
22.6M
}
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
4.19M
void luaK_finish (FuncState *fs) {
1882
4.19M
  int i;
1883
4.19M
  Proto *p = fs->f;
1884
166M
  for (i = 0; i < fs->pc; i++) {
1885
162M
    Instruction *pc = &p->code[i];
1886
    /* avoid "not used" warnings when assert is off (for 'onelua.c') */
1887
162M
    (void)luaP_isOT; (void)luaP_isIT;
1888
162M
    lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
1889
162M
    switch (GET_OPCODE(*pc)) {
1890
5.06M
      case OP_RETURN0: case OP_RETURN1: {
1891
5.06M
        if (!(fs->needclose || (p->flag & PF_ISVARARG)))
1892
2.12M
          break;  /* no extra work */
1893
        /* else use OP_RETURN to do the extra work */
1894
2.94M
        SET_OPCODE(*pc, OP_RETURN);
1895
2.94M
      }  /* FALLTHROUGH */
1896
3.16M
      case OP_RETURN: case OP_TAILCALL: {
1897
3.16M
        if (fs->needclose)
1898
3.16M
          SETARG_k(*pc, 1);  /* signal that it needs to close */
1899
3.16M
        if (p->flag & PF_ISVARARG)
1900
3.16M
          SETARG_C(*pc, p->numparams + 1);  /* signal that it is vararg */
1901
3.16M
        break;
1902
3.05M
      }
1903
22.6M
      case OP_JMP: {
1904
22.6M
        int target = finaltarget(p->code, i);
1905
22.6M
        fixjump(fs, i, target);
1906
22.6M
        break;
1907
3.05M
      }
1908
134M
      default: break;
1909
162M
    }
1910
162M
  }
1911
4.19M
}