Coverage Report

Created: 2025-11-11 06:55

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