2000-02-22 16:31:19 +03:00
|
|
|
/*
|
2018-07-11 18:53:23 +03:00
|
|
|
** $Id: lcode.c $
|
2000-02-22 16:31:19 +03:00
|
|
|
** Code generator for Lua
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define lcode_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
2000-02-22 16:31:19 +03:00
|
|
|
|
2021-03-30 20:49:18 +03:00
|
|
|
#include <float.h>
|
2017-06-27 14:35:31 +03:00
|
|
|
#include <limits.h>
|
2013-06-25 22:57:18 +04:00
|
|
|
#include <math.h>
|
2001-02-23 23:31:37 +03:00
|
|
|
#include <stdlib.h>
|
2000-04-05 00:48:44 +04:00
|
|
|
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
2000-02-22 16:31:19 +03:00
|
|
|
#include "lcode.h"
|
2001-06-05 22:17:01 +04:00
|
|
|
#include "ldebug.h"
|
2000-03-03 15:33:59 +03:00
|
|
|
#include "ldo.h"
|
2003-12-09 19:56:11 +03:00
|
|
|
#include "lgc.h"
|
2000-02-22 16:31:19 +03:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
2009-08-10 19:31:44 +04:00
|
|
|
#include "lstring.h"
|
2001-08-27 19:16:28 +04:00
|
|
|
#include "ltable.h"
|
2011-05-31 22:24:36 +04:00
|
|
|
#include "lvm.h"
|
2000-03-03 15:33:59 +03:00
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
#define hasjumps(e) ((e)->t != (e)->f)
|
|
|
|
|
|
|
|
|
2017-11-07 20:20:42 +03:00
|
|
|
static int codesJ (FuncState *fs, OpCode o, int sj, int k);
|
|
|
|
|
|
|
|
|
2018-03-07 18:55:38 +03:00
|
|
|
|
|
|
|
/* semantic error */
|
|
|
|
l_noret luaK_semerror (LexState *ls, const char *msg) {
|
|
|
|
ls->t.token = 0; /* remove "near <token>" from final message */
|
|
|
|
luaX_syntaxerror(ls, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** If expression is a numeric constant, fills 'v' with its value
|
|
|
|
** and returns 1. Otherwise, returns 0.
|
|
|
|
*/
|
2019-07-09 16:43:17 +03:00
|
|
|
static int tonumeral (const expdesc *e, TValue *v) {
|
2015-10-26 17:27:47 +03:00
|
|
|
if (hasjumps(e))
|
2013-05-02 16:37:24 +04:00
|
|
|
return 0; /* not a numeral */
|
|
|
|
switch (e->k) {
|
|
|
|
case VKINT:
|
|
|
|
if (v) setivalue(v, e->u.ival);
|
|
|
|
return 1;
|
|
|
|
case VKFLT:
|
2014-04-29 22:14:16 +04:00
|
|
|
if (v) setfltvalue(v, e->u.nval);
|
2013-05-02 16:37:24 +04:00
|
|
|
return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-17 20:50:42 +03:00
|
|
|
/*
|
|
|
|
** Get the constant value from a constant expression
|
|
|
|
*/
|
|
|
|
static TValue *const2val (FuncState *fs, const expdesc *e) {
|
|
|
|
lua_assert(e->k == VCONST);
|
|
|
|
return &fs->ls->dyd->actvar.arr[e->u.info].k;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-12 17:38:42 +03:00
|
|
|
/*
|
|
|
|
** If expression is a constant, fills 'v' with its value
|
|
|
|
** and returns 1. Otherwise, returns 0.
|
|
|
|
*/
|
|
|
|
int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
|
|
|
|
if (hasjumps(e))
|
|
|
|
return 0; /* not a constant */
|
|
|
|
switch (e->k) {
|
2020-01-06 17:38:31 +03:00
|
|
|
case VFALSE:
|
|
|
|
setbfvalue(v);
|
|
|
|
return 1;
|
|
|
|
case VTRUE:
|
|
|
|
setbtvalue(v);
|
2019-07-12 17:38:42 +03:00
|
|
|
return 1;
|
|
|
|
case VNIL:
|
|
|
|
setnilvalue(v);
|
|
|
|
return 1;
|
2019-07-17 20:26:56 +03:00
|
|
|
case VKSTR: {
|
|
|
|
setsvalue(fs->ls->L, v, e->u.strval);
|
2019-07-12 17:38:42 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2019-07-17 20:50:42 +03:00
|
|
|
case VCONST: {
|
|
|
|
setobj(fs->ls->L, v, const2val(fs, e));
|
|
|
|
return 1;
|
|
|
|
}
|
2019-07-12 17:38:42 +03:00
|
|
|
default: return tonumeral(e, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-27 19:56:33 +03:00
|
|
|
/*
|
|
|
|
** Return the previous instruction of the current code. If there
|
|
|
|
** may be a jump target between the current instruction and the
|
|
|
|
** previous one, return an invalid instruction (to avoid wrong
|
|
|
|
** optimizations).
|
|
|
|
*/
|
|
|
|
static Instruction *previousinstruction (FuncState *fs) {
|
2019-10-25 23:41:40 +03:00
|
|
|
static const Instruction invalidinstruction = ~(Instruction)0;
|
2018-01-27 19:56:33 +03:00
|
|
|
if (fs->pc > fs->lasttarget)
|
|
|
|
return &fs->f->code[fs->pc - 1]; /* previous instruction */
|
|
|
|
else
|
|
|
|
return cast(Instruction*, &invalidinstruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Create a OP_LOADNIL instruction, but try to optimize: if the previous
|
|
|
|
** instruction is also OP_LOADNIL and ranges are compatible, adjust
|
|
|
|
** range of previous instruction instead of emitting a new one. (For
|
|
|
|
** instance, 'local a; local b' will generate a single opcode.)
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_nil (FuncState *fs, int from, int n) {
|
2011-04-19 20:22:13 +04:00
|
|
|
int l = from + n - 1; /* last register to set nil */
|
2018-01-27 19:56:33 +03:00
|
|
|
Instruction *previous = previousinstruction(fs);
|
|
|
|
if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
|
|
|
|
int pfrom = GETARG_A(*previous); /* get previous range */
|
|
|
|
int pl = pfrom + GETARG_B(*previous);
|
|
|
|
if ((pfrom <= from && from <= pl + 1) ||
|
|
|
|
(from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
|
|
|
|
if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
|
|
|
|
if (pl > l) l = pl; /* l = max(l, pl) */
|
|
|
|
SETARG_A(*previous, from);
|
|
|
|
SETARG_B(*previous, l - from);
|
|
|
|
return;
|
2011-04-19 20:22:13 +04:00
|
|
|
} /* else go through */
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
2011-04-19 20:22:13 +04:00
|
|
|
luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
|
2000-04-05 21:51:58 +04:00
|
|
|
}
|
|
|
|
|
2000-02-22 16:31:19 +03:00
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Gets the destination address of a jump instruction. Used to traverse
|
|
|
|
** a list of jumps.
|
2016-12-22 16:08:50 +03:00
|
|
|
*/
|
2015-12-17 18:02:44 +03:00
|
|
|
static int getjump (FuncState *fs, int pc) {
|
2017-11-07 20:20:42 +03:00
|
|
|
int offset = GETARG_sJ(fs->f->code[pc]);
|
2015-12-17 18:02:44 +03:00
|
|
|
if (offset == NO_JUMP) /* point to itself represents end of list */
|
|
|
|
return NO_JUMP; /* end of list */
|
|
|
|
else
|
|
|
|
return (pc+1)+offset; /* turn offset into absolute position */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Fix jump instruction at position 'pc' to jump to 'dest'.
|
|
|
|
** (Jump addresses are relative in Lua)
|
|
|
|
*/
|
2015-12-17 18:02:44 +03:00
|
|
|
static void fixjump (FuncState *fs, int pc, int dest) {
|
|
|
|
Instruction *jmp = &fs->f->code[pc];
|
2015-12-18 16:53:36 +03:00
|
|
|
int offset = dest - (pc + 1);
|
2015-12-17 18:02:44 +03:00
|
|
|
lua_assert(dest != NO_JUMP);
|
2017-12-15 21:53:48 +03:00
|
|
|
if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
|
2015-12-17 18:02:44 +03:00
|
|
|
luaX_syntaxerror(fs->ls, "control structure too long");
|
2017-11-07 20:20:42 +03:00
|
|
|
lua_assert(GET_OPCODE(*jmp) == OP_JMP);
|
|
|
|
SETARG_sJ(*jmp, offset);
|
2015-12-17 18:02:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Concatenate jump-list 'l2' into jump-list 'l1'
|
|
|
|
*/
|
2015-12-17 18:02:44 +03:00
|
|
|
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
2015-12-18 16:53:36 +03:00
|
|
|
if (l2 == NO_JUMP) return; /* nothing to concatenate? */
|
|
|
|
else if (*l1 == NO_JUMP) /* no original list? */
|
|
|
|
*l1 = l2; /* 'l1' points to 'l2' */
|
2015-12-17 18:02:44 +03:00
|
|
|
else {
|
|
|
|
int list = *l1;
|
|
|
|
int next;
|
|
|
|
while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
|
|
|
|
list = next;
|
2015-12-18 16:53:36 +03:00
|
|
|
fixjump(fs, list, l2); /* last element links to 'l2' */
|
2015-12-17 18:02:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Create a jump instruction and return its position, so its destination
|
2017-11-30 16:29:18 +03:00
|
|
|
** can be fixed later (with 'fixjump').
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2000-04-05 21:51:58 +04:00
|
|
|
int luaK_jump (FuncState *fs) {
|
2017-11-30 16:29:18 +03:00
|
|
|
return codesJ(fs, OP_JMP, NO_JUMP, 0);
|
2000-04-05 21:51:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Code a 'return' instruction
|
|
|
|
*/
|
2005-08-30 00:49:21 +04:00
|
|
|
void luaK_ret (FuncState *fs, int first, int nret) {
|
2018-02-09 18:16:06 +03:00
|
|
|
OpCode op;
|
2018-02-15 18:34:29 +03:00
|
|
|
switch (nret) {
|
|
|
|
case 0: op = OP_RETURN0; break;
|
|
|
|
case 1: op = OP_RETURN1; break;
|
|
|
|
default: op = OP_RETURN; break;
|
2017-11-29 19:57:36 +03:00
|
|
|
}
|
2018-02-15 18:34:29 +03:00
|
|
|
luaK_codeABC(fs, op, first, nret + 1, 0);
|
2005-08-30 00:49:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Code a "conditional jump", that is, a test or comparison opcode
|
|
|
|
** followed by a jump. Return jump position.
|
|
|
|
*/
|
2019-03-22 19:37:17 +03:00
|
|
|
static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
|
|
|
|
luaK_codeABCk(fs, op, A, B, C, k);
|
2002-05-10 23:22:11 +04:00
|
|
|
return luaK_jump(fs);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-04 23:18:15 +03:00
|
|
|
/*
|
2014-10-25 15:50:46 +04:00
|
|
|
** returns current 'pc' and marks it as a jump target (to avoid wrong
|
2000-03-04 23:18:15 +03:00
|
|
|
** optimizations with consecutive instructions not in the same basic block).
|
|
|
|
*/
|
2000-03-13 23:37:16 +03:00
|
|
|
int luaK_getlabel (FuncState *fs) {
|
2002-05-10 23:22:11 +04:00
|
|
|
fs->lasttarget = fs->pc;
|
2000-03-04 23:18:15 +03:00
|
|
|
return fs->pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Returns the position of the instruction "controlling" a given
|
|
|
|
** jump (that is, its condition), or the jump itself if it is
|
|
|
|
** unconditional.
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
|
|
|
|
Instruction *pi = &fs->f->code[pc];
|
2003-05-14 16:09:12 +04:00
|
|
|
if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
|
2001-06-05 22:17:01 +04:00
|
|
|
return pi-1;
|
2002-01-17 01:03:57 +03:00
|
|
|
else
|
2001-06-05 22:17:01 +04:00
|
|
|
return pi;
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Patch destination register for a TESTSET instruction.
|
|
|
|
** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
|
|
|
|
** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
|
|
|
|
** register. Otherwise, change instruction to a simple 'TEST' (produces
|
|
|
|
** no register value)
|
|
|
|
*/
|
2005-10-13 16:21:51 +04:00
|
|
|
static int patchtestreg (FuncState *fs, int node, int reg) {
|
|
|
|
Instruction *i = getjumpcontrol(fs, node);
|
|
|
|
if (GET_OPCODE(*i) != OP_TESTSET)
|
|
|
|
return 0; /* cannot patch other instructions */
|
|
|
|
if (reg != NO_REG && reg != GETARG_B(*i))
|
2005-08-30 00:49:21 +04:00
|
|
|
SETARG_A(*i, reg);
|
2015-12-18 16:53:36 +03:00
|
|
|
else {
|
|
|
|
/* no register to put value or register already has the value;
|
|
|
|
change instruction to simple test */
|
2017-11-28 15:58:18 +03:00
|
|
|
*i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
|
2015-12-18 16:53:36 +03:00
|
|
|
}
|
2005-10-13 16:21:51 +04:00
|
|
|
return 1;
|
2001-06-08 16:29:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Traverse a list of tests ensuring no one produces a value
|
|
|
|
*/
|
2005-08-30 00:49:21 +04:00
|
|
|
static void removevalues (FuncState *fs, int list) {
|
2005-10-13 16:21:51 +04:00
|
|
|
for (; list != NO_JUMP; list = getjump(fs, list))
|
|
|
|
patchtestreg(fs, list, NO_REG);
|
2005-08-30 00:49:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Traverse a list of tests, patching their destination address and
|
|
|
|
** registers: tests producing values jump to 'vtarget' (and put their
|
|
|
|
** values in 'reg'), other tests jump to 'dtarget'.
|
|
|
|
*/
|
2005-08-30 00:49:21 +04:00
|
|
|
static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
|
|
|
|
int dtarget) {
|
2001-06-05 22:17:01 +04:00
|
|
|
while (list != NO_JUMP) {
|
2004-12-03 23:50:25 +03:00
|
|
|
int next = getjump(fs, list);
|
2005-10-13 16:21:51 +04:00
|
|
|
if (patchtestreg(fs, list, reg))
|
2005-08-30 00:49:21 +04:00
|
|
|
fixjump(fs, list, vtarget);
|
|
|
|
else
|
|
|
|
fixjump(fs, list, dtarget); /* jump to default target */
|
2001-06-05 22:17:01 +04:00
|
|
|
list = next;
|
|
|
|
}
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Path all jumps in 'list' to jump to 'target'.
|
|
|
|
** (The assert means that we cannot fix a jump to a forward address
|
|
|
|
** because we only know addresses once code is generated.)
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
2017-11-30 16:29:18 +03:00
|
|
|
lua_assert(target <= fs->pc);
|
|
|
|
patchlistaux(fs, list, target, NO_REG, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_patchtohere (FuncState *fs, int list) {
|
|
|
|
int hr = luaK_getlabel(fs); /* mark "here" as a jump target */
|
|
|
|
luaK_patchlist(fs, list, hr);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-11 18:53:23 +03:00
|
|
|
/* limit for difference between lines in relative line info. */
|
|
|
|
#define LIMLINEDIFF 0x80
|
|
|
|
|
|
|
|
|
2017-06-27 14:35:31 +03:00
|
|
|
/*
|
|
|
|
** Save line info for a new instruction. If difference from last line
|
|
|
|
** does not fit in a byte, of after that many instructions, save a new
|
|
|
|
** absolute line info; (in that case, the special value 'ABSLINEINFO'
|
|
|
|
** in 'lineinfo' signals the existence of this absolute information.)
|
|
|
|
** Otherwise, store the difference from last line in 'lineinfo'.
|
|
|
|
*/
|
2018-07-11 18:53:23 +03:00
|
|
|
static void savelineinfo (FuncState *fs, Proto *f, int line) {
|
2017-06-27 14:35:31 +03:00
|
|
|
int linedif = line - fs->previousline;
|
2018-07-11 18:53:23 +03:00
|
|
|
int pc = fs->pc - 1; /* last instruction coded */
|
2021-02-02 20:43:55 +03:00
|
|
|
if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
|
2017-06-27 14:35:31 +03:00
|
|
|
luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
|
2024-06-20 19:43:33 +03:00
|
|
|
f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines");
|
2017-06-27 14:35:31 +03:00
|
|
|
f->abslineinfo[fs->nabslineinfo].pc = pc;
|
|
|
|
f->abslineinfo[fs->nabslineinfo++].line = line;
|
2018-07-11 18:53:23 +03:00
|
|
|
linedif = ABSLINEINFO; /* signal that there is absolute information */
|
2021-02-02 20:43:55 +03:00
|
|
|
fs->iwthabs = 1; /* restart counter */
|
2017-06-27 14:35:31 +03:00
|
|
|
}
|
|
|
|
luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
|
2024-06-20 19:43:33 +03:00
|
|
|
INT_MAX, "opcodes");
|
2017-06-27 14:35:31 +03:00
|
|
|
f->lineinfo[pc] = linedif;
|
|
|
|
fs->previousline = line; /* last line saved */
|
|
|
|
}
|
|
|
|
|
2011-02-01 21:03:10 +03:00
|
|
|
|
2018-07-11 18:53:23 +03:00
|
|
|
/*
|
|
|
|
** Remove line information from the last instruction.
|
|
|
|
** If line information for that instruction is absolute, set 'iwthabs'
|
|
|
|
** above its max to force the new (replacing) instruction to have
|
|
|
|
** absolute line info, too.
|
|
|
|
*/
|
|
|
|
static void removelastlineinfo (FuncState *fs) {
|
|
|
|
Proto *f = fs->f;
|
|
|
|
int pc = fs->pc - 1; /* last instruction coded */
|
|
|
|
if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */
|
2019-10-01 23:24:37 +03:00
|
|
|
fs->previousline -= f->lineinfo[pc]; /* correct last line saved */
|
|
|
|
fs->iwthabs--; /* undo previous increment */
|
2018-07-11 18:53:23 +03:00
|
|
|
}
|
|
|
|
else { /* absolute line information */
|
2019-10-01 23:24:37 +03:00
|
|
|
lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
|
2018-07-11 18:53:23 +03:00
|
|
|
fs->nabslineinfo--; /* remove it */
|
|
|
|
fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Remove the last instruction created, correcting line information
|
|
|
|
** accordingly.
|
|
|
|
*/
|
|
|
|
static void removelastinstruction (FuncState *fs) {
|
|
|
|
removelastlineinfo(fs);
|
|
|
|
fs->pc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit instruction 'i', checking for array sizes and saving also its
|
|
|
|
** line information. Return 'i' position.
|
|
|
|
*/
|
2019-07-15 20:59:35 +03:00
|
|
|
int luaK_code (FuncState *fs, Instruction i) {
|
2009-09-24 00:33:05 +04:00
|
|
|
Proto *f = fs->f;
|
|
|
|
/* put new instruction in code array */
|
2011-07-27 22:07:37 +04:00
|
|
|
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
|
2024-06-20 19:43:33 +03:00
|
|
|
INT_MAX, "opcodes");
|
2018-07-11 18:53:23 +03:00
|
|
|
f->code[fs->pc++] = i;
|
|
|
|
savelineinfo(fs, f, fs->ls->lastline);
|
|
|
|
return fs->pc - 1; /* index of new instruction */
|
2009-09-24 00:33:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Format and emit an 'iABC' instruction. (Assertions check consistency
|
|
|
|
** of parameters versus opcode.)
|
|
|
|
*/
|
2024-06-28 17:18:14 +03:00
|
|
|
int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
|
2009-09-24 00:33:05 +04:00
|
|
|
lua_assert(getOpMode(o) == iABC);
|
2024-06-28 17:18:14 +03:00
|
|
|
lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
|
|
|
|
C <= MAXARG_C && (k & ~1) == 0);
|
|
|
|
return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
|
|
|
|
lua_assert(getOpMode(o) == ivABC);
|
|
|
|
lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
|
|
|
|
C <= MAXARG_vC && (k & ~1) == 0);
|
|
|
|
return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
|
2009-09-24 00:33:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Format and emit an 'iABx' instruction.
|
|
|
|
*/
|
2024-06-28 17:18:14 +03:00
|
|
|
int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) {
|
2017-09-28 19:53:29 +03:00
|
|
|
lua_assert(getOpMode(o) == iABx);
|
2024-06-28 17:18:14 +03:00
|
|
|
lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
|
|
|
|
return luaK_code(fs, CREATE_ABx(o, A, Bc));
|
2009-09-24 00:33:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-28 19:53:29 +03:00
|
|
|
/*
|
|
|
|
** Format and emit an 'iAsBx' instruction.
|
|
|
|
*/
|
2024-06-28 17:18:14 +03:00
|
|
|
static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
|
|
|
|
unsigned int b = cast_uint(Bc) + OFFSET_sBx;
|
2017-09-28 19:53:29 +03:00
|
|
|
lua_assert(getOpMode(o) == iAsBx);
|
2024-06-28 17:18:14 +03:00
|
|
|
lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
|
|
|
|
return luaK_code(fs, CREATE_ABx(o, A, b));
|
2017-09-28 19:53:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-07 20:20:42 +03:00
|
|
|
/*
|
|
|
|
** Format and emit an 'isJ' instruction.
|
|
|
|
*/
|
|
|
|
static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
|
2024-06-28 17:18:14 +03:00
|
|
|
unsigned int j = cast_uint(sj) + OFFSET_sJ;
|
2017-11-07 20:20:42 +03:00
|
|
|
lua_assert(getOpMode(o) == isJ);
|
|
|
|
lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
|
|
|
|
return luaK_code(fs, CREATE_sJ(o, j, k));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit an "extra argument" instruction (format 'iAx')
|
|
|
|
*/
|
2024-06-28 17:18:14 +03:00
|
|
|
static int codeextraarg (FuncState *fs, int A) {
|
|
|
|
lua_assert(A <= MAXARG_Ax);
|
|
|
|
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
|
2009-09-24 00:33:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit a "load constant" instruction, using either 'OP_LOADK'
|
|
|
|
** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
|
|
|
|
** instruction with "extra argument".
|
|
|
|
*/
|
2017-04-20 22:53:55 +03:00
|
|
|
static int luaK_codek (FuncState *fs, int reg, int k) {
|
2011-04-07 22:14:12 +04:00
|
|
|
if (k <= MAXARG_Bx)
|
|
|
|
return luaK_codeABx(fs, OP_LOADK, reg, k);
|
2009-09-24 00:33:05 +04:00
|
|
|
else {
|
2011-04-07 22:14:12 +04:00
|
|
|
int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
|
2019-07-15 20:59:35 +03:00
|
|
|
codeextraarg(fs, k);
|
2009-09-24 00:33:05 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Check register-stack level, keeping track of its maximum size
|
|
|
|
** in field 'maxstacksize'
|
|
|
|
*/
|
2002-12-11 15:34:22 +03:00
|
|
|
void luaK_checkstack (FuncState *fs, int n) {
|
2002-03-08 22:10:32 +03:00
|
|
|
int newstack = fs->freereg + n;
|
|
|
|
if (newstack > fs->f->maxstacksize) {
|
2024-06-26 20:46:44 +03:00
|
|
|
if (newstack > MAX_FSTACK)
|
2015-04-29 21:24:11 +03:00
|
|
|
luaX_syntaxerror(fs->ls,
|
|
|
|
"function or expression needs too many registers");
|
2005-12-22 19:19:56 +03:00
|
|
|
fs->f->maxstacksize = cast_byte(newstack);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Reserve 'n' registers in register stack
|
|
|
|
*/
|
2002-03-08 22:10:32 +03:00
|
|
|
void luaK_reserveregs (FuncState *fs, int n) {
|
|
|
|
luaK_checkstack(fs, n);
|
|
|
|
fs->freereg += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Free register 'reg', if it is neither a constant index nor
|
|
|
|
** a local variable.
|
|
|
|
)
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
static void freereg (FuncState *fs, int reg) {
|
2019-07-10 20:00:22 +03:00
|
|
|
if (reg >= luaY_nvarstack(fs)) {
|
2001-06-05 22:17:01 +04:00
|
|
|
fs->freereg--;
|
|
|
|
lua_assert(reg == fs->freereg);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
2000-03-04 23:18:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 21:28:25 +03:00
|
|
|
/*
|
|
|
|
** Free two registers in proper order
|
|
|
|
*/
|
|
|
|
static void freeregs (FuncState *fs, int r1, int r2) {
|
|
|
|
if (r1 > r2) {
|
|
|
|
freereg(fs, r1);
|
|
|
|
freereg(fs, r2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
freereg(fs, r2);
|
|
|
|
freereg(fs, r1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Free register used by expression 'e' (if any)
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
static void freeexp (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k == VNONRELOC)
|
2010-07-03 00:42:40 +04:00
|
|
|
freereg(fs, e->u.info);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
2000-04-07 23:35:20 +04:00
|
|
|
|
2000-03-03 15:33:59 +03:00
|
|
|
|
2016-01-04 16:40:57 +03:00
|
|
|
/*
|
|
|
|
** Free registers used by expressions 'e1' and 'e2' (if any) in proper
|
|
|
|
** order.
|
|
|
|
*/
|
|
|
|
static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
|
|
|
|
int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
|
|
|
|
int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
|
2017-04-25 21:28:25 +03:00
|
|
|
freeregs(fs, r1, r2);
|
2016-01-04 16:40:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-30 20:01:37 +04:00
|
|
|
/*
|
2015-12-18 16:53:36 +03:00
|
|
|
** Add constant 'v' to prototype's list of constants (field 'k').
|
2014-03-21 17:52:33 +04:00
|
|
|
** Use scanner's table to cache position of constants in constant list
|
2015-12-18 16:53:36 +03:00
|
|
|
** and try to reuse constants. Because some values should not be used
|
|
|
|
** as keys (nil cannot be a key, integer keys can collapse with float
|
|
|
|
** keys), the caller must provide a useful 'key' for indexing the cache.
|
2020-12-04 17:08:42 +03:00
|
|
|
** Note that all functions share the same table, so entering or exiting
|
|
|
|
** a function can make some indices wrong.
|
2013-08-30 20:01:37 +04:00
|
|
|
*/
|
2006-06-22 20:12:59 +04:00
|
|
|
static int addk (FuncState *fs, TValue *key, TValue *v) {
|
2024-03-21 17:23:21 +03:00
|
|
|
TValue val;
|
2011-07-27 22:07:37 +04:00
|
|
|
lua_State *L = fs->ls->L;
|
2003-08-28 01:01:44 +04:00
|
|
|
Proto *f = fs->f;
|
2024-03-21 17:23:21 +03:00
|
|
|
int tag = luaH_get(fs->ls->h, key, &val); /* query scanner table */
|
2009-08-10 19:31:44 +04:00
|
|
|
int k, oldsize;
|
2024-03-21 17:23:21 +03:00
|
|
|
if (tag == LUA_VNUMINT) { /* is there an index there? */
|
2023-05-16 22:53:29 +03:00
|
|
|
k = cast_int(ivalue(&val));
|
2013-08-30 20:01:37 +04:00
|
|
|
/* correct value? (warning: must distinguish floats from integers!) */
|
2018-02-26 17:16:05 +03:00
|
|
|
if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
|
2013-08-30 20:01:37 +04:00
|
|
|
luaV_rawequalobj(&f->k[k], v))
|
|
|
|
return k; /* reuse index */
|
2001-08-27 19:16:28 +04:00
|
|
|
}
|
2009-08-10 19:31:44 +04:00
|
|
|
/* constant not found; create a new entry */
|
|
|
|
oldsize = f->sizek;
|
|
|
|
k = fs->nk;
|
2011-08-15 23:41:58 +04:00
|
|
|
/* numerical value does not need GC barrier;
|
|
|
|
table has no metatable, so it does not need to invalidate cache */
|
2020-12-04 17:08:42 +03:00
|
|
|
setivalue(&val, k);
|
2023-05-16 22:53:29 +03:00
|
|
|
luaH_set(L, fs->ls->h, key, &val);
|
2009-09-24 00:33:05 +04:00
|
|
|
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
|
2009-08-10 19:31:44 +04:00
|
|
|
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
|
|
|
setobj(L, &f->k[k], v);
|
|
|
|
fs->nk++;
|
|
|
|
luaC_barrier(L, f, v);
|
2006-06-22 20:12:59 +04:00
|
|
|
return k;
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Add a string to list of constants and return its index.
|
|
|
|
*/
|
2019-07-17 20:26:56 +03:00
|
|
|
static int stringK (FuncState *fs, TString *s) {
|
2003-12-10 15:13:36 +03:00
|
|
|
TValue o;
|
2011-07-27 22:07:37 +04:00
|
|
|
setsvalue(fs->ls->L, &o, s);
|
2015-12-18 16:53:36 +03:00
|
|
|
return addk(fs, &o, &o); /* use string itself as key */
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-25 22:57:18 +04:00
|
|
|
/*
|
2015-12-18 16:53:36 +03:00
|
|
|
** Add an integer to list of constants and return its index.
|
2013-06-25 22:57:18 +04:00
|
|
|
*/
|
2017-04-20 22:53:55 +03:00
|
|
|
static int luaK_intK (FuncState *fs, lua_Integer n) {
|
2021-03-30 20:49:18 +03:00
|
|
|
TValue o;
|
2013-04-16 22:46:28 +04:00
|
|
|
setivalue(&o, n);
|
2021-03-30 20:49:18 +03:00
|
|
|
return addk(fs, &o, &o); /* use integer itself as key */
|
2013-04-16 22:46:28 +04:00
|
|
|
}
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2021-03-30 20:49:18 +03:00
|
|
|
** Add a float to list of constants and return its index. Floats
|
|
|
|
** with integral values need a different key, to avoid collision
|
|
|
|
** with actual integers. To that, we add to the number its smaller
|
|
|
|
** power-of-two fraction that is still significant in its scale.
|
|
|
|
** For doubles, that would be 1/2^52.
|
|
|
|
** (This method is not bulletproof: there may be another float
|
|
|
|
** with that value, and for floats larger than 2^53 the result is
|
|
|
|
** still an integer. At worst, this only wastes an entry with
|
|
|
|
** a duplicate.)
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2013-04-25 23:35:19 +04:00
|
|
|
static int luaK_numberK (FuncState *fs, lua_Number r) {
|
2003-12-10 15:13:36 +03:00
|
|
|
TValue o;
|
2021-03-30 20:49:18 +03:00
|
|
|
lua_Integer ik;
|
2014-04-29 22:14:16 +04:00
|
|
|
setfltvalue(&o, r);
|
2021-03-30 20:49:18 +03:00
|
|
|
if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */
|
|
|
|
return addk(fs, &o, &o); /* use number itself as key */
|
|
|
|
else { /* must build an alternative key */
|
|
|
|
const int nbm = l_floatatt(MANT_DIG);
|
2022-01-03 15:12:17 +03:00
|
|
|
const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
|
2021-03-30 20:49:18 +03:00
|
|
|
const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */
|
|
|
|
TValue kv;
|
|
|
|
setfltvalue(&kv, k);
|
|
|
|
/* result is not an integral value, unless value is too large */
|
|
|
|
lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) ||
|
|
|
|
l_mathop(fabs)(r) >= l_mathop(1e6));
|
|
|
|
return addk(fs, &kv, &o);
|
|
|
|
}
|
2001-12-12 01:48:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2020-01-06 17:38:31 +03:00
|
|
|
** Add a false to list of constants and return its index.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2020-01-06 17:38:31 +03:00
|
|
|
static int boolF (FuncState *fs) {
|
2004-10-04 23:01:53 +04:00
|
|
|
TValue o;
|
2020-01-06 17:38:31 +03:00
|
|
|
setbfvalue(&o);
|
|
|
|
return addk(fs, &o, &o); /* use boolean itself as key */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Add a true to list of constants and return its index.
|
|
|
|
*/
|
|
|
|
static int boolT (FuncState *fs) {
|
|
|
|
TValue o;
|
|
|
|
setbtvalue(&o);
|
2015-12-18 16:53:36 +03:00
|
|
|
return addk(fs, &o, &o); /* use boolean itself as key */
|
2004-10-04 23:01:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Add nil to list of constants and return its index.
|
|
|
|
*/
|
2004-10-04 23:01:53 +04:00
|
|
|
static int nilK (FuncState *fs) {
|
2003-12-10 15:13:36 +03:00
|
|
|
TValue k, v;
|
2001-12-12 01:48:44 +03:00
|
|
|
setnilvalue(&v);
|
2003-12-10 15:13:36 +03:00
|
|
|
/* cannot use nil as key; instead use table itself to represent nil */
|
2013-08-30 20:01:37 +04:00
|
|
|
sethvalue(fs->ls->L, &k, fs->ls->h);
|
2001-12-12 01:48:44 +03:00
|
|
|
return addk(fs, &k, &v);
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-15 21:53:48 +03:00
|
|
|
/*
|
2019-10-01 23:24:37 +03:00
|
|
|
** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
|
|
|
|
** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
|
|
|
|
** overflows in the hidden addition inside 'int2sC'.
|
2017-12-15 21:53:48 +03:00
|
|
|
*/
|
|
|
|
static int fitsC (lua_Integer i) {
|
2019-10-01 23:24:37 +03:00
|
|
|
return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
|
2017-12-15 21:53:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check whether 'i' can be stored in an 'sBx' operand.
|
|
|
|
*/
|
|
|
|
static int fitsBx (lua_Integer i) {
|
|
|
|
return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-20 22:53:55 +03:00
|
|
|
void luaK_int (FuncState *fs, int reg, lua_Integer i) {
|
2017-12-15 21:53:48 +03:00
|
|
|
if (fitsBx(i))
|
2023-05-22 20:47:54 +03:00
|
|
|
codeAsBx(fs, OP_LOADI, reg, cast_int(i));
|
2017-04-20 22:53:55 +03:00
|
|
|
else
|
|
|
|
luaK_codek(fs, reg, luaK_intK(fs, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-22 21:41:20 +03:00
|
|
|
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
|
|
|
|
lua_Integer fi;
|
2019-12-05 20:14:29 +03:00
|
|
|
if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
|
2023-05-22 20:47:54 +03:00
|
|
|
codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
|
2017-09-19 21:38:14 +03:00
|
|
|
else
|
|
|
|
luaK_codek(fs, reg, luaK_numberK(fs, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-12 17:38:42 +03:00
|
|
|
/*
|
|
|
|
** Convert a constant in 'v' into an expression description 'e'
|
|
|
|
*/
|
2019-07-17 20:26:56 +03:00
|
|
|
static void const2exp (TValue *v, expdesc *e) {
|
2019-07-12 17:38:42 +03:00
|
|
|
switch (ttypetag(v)) {
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VNUMINT:
|
2019-07-12 17:38:42 +03:00
|
|
|
e->k = VKINT; e->u.ival = ivalue(v);
|
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VNUMFLT:
|
2019-07-12 17:38:42 +03:00
|
|
|
e->k = VKFLT; e->u.nval = fltvalue(v);
|
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VFALSE:
|
2020-01-06 17:38:31 +03:00
|
|
|
e->k = VFALSE;
|
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VTRUE:
|
2020-01-06 17:38:31 +03:00
|
|
|
e->k = VTRUE;
|
2019-07-12 17:38:42 +03:00
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VNIL:
|
2019-07-12 17:38:42 +03:00
|
|
|
e->k = VNIL;
|
|
|
|
break;
|
2020-01-31 17:09:53 +03:00
|
|
|
case LUA_VSHRSTR: case LUA_VLNGSTR:
|
2019-07-17 20:26:56 +03:00
|
|
|
e->k = VKSTR; e->u.strval = tsvalue(v);
|
2019-07-12 17:38:42 +03:00
|
|
|
break;
|
|
|
|
default: lua_assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Fix an expression to return the number of results 'nresults'.
|
2020-03-02 19:24:06 +03:00
|
|
|
** 'e' must be a multi-ret expression (function call or vararg).
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2004-05-31 22:51:50 +04:00
|
|
|
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
2017-12-22 17:16:46 +03:00
|
|
|
Instruction *pc = &getinstruction(fs, e);
|
|
|
|
if (e->k == VCALL) /* expression is an open function call? */
|
|
|
|
SETARG_C(*pc, nresults + 1);
|
2020-03-02 19:24:06 +03:00
|
|
|
else {
|
|
|
|
lua_assert(e->k == VVARARG);
|
2017-12-22 17:16:46 +03:00
|
|
|
SETARG_C(*pc, nresults + 1);
|
2016-01-05 19:22:37 +03:00
|
|
|
SETARG_A(*pc, fs->freereg);
|
2004-05-31 22:51:50 +04:00
|
|
|
luaK_reserveregs(fs, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-17 20:26:56 +03:00
|
|
|
/*
|
|
|
|
** Convert a VKSTR to a VK
|
|
|
|
*/
|
|
|
|
static void str2K (FuncState *fs, expdesc *e) {
|
|
|
|
lua_assert(e->k == VKSTR);
|
|
|
|
e->u.info = stringK(fs, e->u.strval);
|
|
|
|
e->k = VK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Fix an expression to return one result.
|
|
|
|
** If expression is not a multi-ret expression (function call or
|
|
|
|
** vararg), it already returns one result, so nothing needs to be done.
|
|
|
|
** Function calls become VNONRELOC expressions (as its result comes
|
|
|
|
** fixed in the base register of the call), while vararg expressions
|
2017-12-14 17:24:02 +03:00
|
|
|
** become VRELOC (as OP_VARARG puts its results where it wants).
|
2015-12-18 16:53:36 +03:00
|
|
|
** (Calls are created returning one result, so that does not need
|
|
|
|
** to be fixed.)
|
|
|
|
*/
|
2004-05-31 22:51:50 +04:00
|
|
|
void luaK_setoneret (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k == VCALL) { /* expression is an open function call? */
|
2016-01-05 19:22:37 +03:00
|
|
|
/* already returns 1 value */
|
|
|
|
lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
|
2015-12-18 16:53:36 +03:00
|
|
|
e->k = VNONRELOC; /* result has fixed position */
|
2016-01-05 19:22:37 +03:00
|
|
|
e->u.info = GETARG_A(getinstruction(fs, e));
|
2004-05-31 22:51:50 +04:00
|
|
|
}
|
|
|
|
else if (e->k == VVARARG) {
|
2017-12-22 17:16:46 +03:00
|
|
|
SETARG_C(getinstruction(fs, e), 2);
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC; /* can relocate its simple result */
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2020-10-30 16:18:54 +03:00
|
|
|
** Ensure that expression 'e' is not a variable (nor a <const>).
|
2018-01-18 19:24:31 +03:00
|
|
|
** (Expression still may have jump lists.)
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-12 18:36:48 +04:00
|
|
|
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (e->k) {
|
2019-07-12 17:38:42 +03:00
|
|
|
case VCONST: {
|
2019-07-17 20:50:42 +03:00
|
|
|
const2exp(const2val(fs, e), e);
|
2019-07-12 17:38:42 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-01-04 16:40:57 +03:00
|
|
|
case VLOCAL: { /* already in a register */
|
2024-06-12 21:56:13 +03:00
|
|
|
int temp = e->u.var.ridx;
|
|
|
|
e->u.info = temp; /* (can't do a direct assignment; values overlap) */
|
2015-12-18 16:53:36 +03:00
|
|
|
e->k = VNONRELOC; /* becomes a non-relocatable value */
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-01-04 16:40:57 +03:00
|
|
|
case VUPVAL: { /* move value to some (pending) register */
|
2019-07-09 16:43:17 +03:00
|
|
|
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
|
|
|
|
e->k = VRELOC;
|
2001-09-07 21:39:10 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-04-28 23:57:45 +03:00
|
|
|
case VINDEXUP: {
|
|
|
|
e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC;
|
2017-04-28 23:57:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXI: {
|
|
|
|
freereg(fs, e->u.ind.t);
|
|
|
|
e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC;
|
2017-04-28 23:57:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXSTR: {
|
|
|
|
freereg(fs, e->u.ind.t);
|
|
|
|
e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC;
|
2017-04-28 23:57:45 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VINDEXED: {
|
2017-04-28 23:57:45 +03:00
|
|
|
freeregs(fs, e->u.ind.t, e->u.ind.idx);
|
|
|
|
e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC;
|
2010-02-26 23:40:29 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-01-04 16:40:57 +03:00
|
|
|
case VVARARG: case VCALL: {
|
2004-05-31 22:51:50 +04:00
|
|
|
luaK_setoneret(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break; /* there is one value available (somewhere) */
|
|
|
|
}
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2020-10-30 16:18:54 +03:00
|
|
|
** Ensure expression value is in register 'reg', making 'e' a
|
|
|
|
** non-relocatable expression.
|
2018-01-18 19:24:31 +03:00
|
|
|
** (Expression still may have jump lists.)
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (e->k) {
|
|
|
|
case VNIL: {
|
|
|
|
luaK_nil(fs, reg, 1);
|
|
|
|
break;
|
|
|
|
}
|
2020-01-06 17:38:31 +03:00
|
|
|
case VFALSE: {
|
|
|
|
luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VTRUE: {
|
|
|
|
luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
|
2001-12-12 01:48:44 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-07-17 20:26:56 +03:00
|
|
|
case VKSTR: {
|
|
|
|
str2K(fs, e);
|
|
|
|
} /* FALLTHROUGH */
|
2001-06-05 22:17:01 +04:00
|
|
|
case VK: {
|
2010-07-03 00:42:40 +04:00
|
|
|
luaK_codek(fs, reg, e->u.info);
|
2005-10-10 00:05:48 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-16 22:46:28 +04:00
|
|
|
case VKFLT: {
|
2017-09-19 21:38:14 +03:00
|
|
|
luaK_float(fs, reg, e->u.nval);
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-16 22:46:28 +04:00
|
|
|
case VKINT: {
|
2017-04-20 22:53:55 +03:00
|
|
|
luaK_int(fs, reg, e->u.ival);
|
2013-04-16 22:46:28 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-12-14 17:24:02 +03:00
|
|
|
case VRELOC: {
|
2016-01-05 19:22:37 +03:00
|
|
|
Instruction *pc = &getinstruction(fs, e);
|
2015-12-18 16:53:36 +03:00
|
|
|
SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2002-04-22 18:37:09 +04:00
|
|
|
case VNONRELOC: {
|
2010-07-03 00:42:40 +04:00
|
|
|
if (reg != e->u.info)
|
|
|
|
luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
|
2002-04-22 18:37:09 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2015-12-18 16:53:36 +03:00
|
|
|
lua_assert(e->k == VJMP);
|
2002-04-22 18:37:09 +04:00
|
|
|
return; /* nothing to do... */
|
|
|
|
}
|
2000-03-24 15:18:30 +03:00
|
|
|
}
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = reg;
|
2001-06-05 22:17:01 +04:00
|
|
|
e->k = VNONRELOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2020-10-30 16:18:54 +03:00
|
|
|
** Ensure expression value is in a register, making 'e' a
|
|
|
|
** non-relocatable expression.
|
2018-01-18 19:24:31 +03:00
|
|
|
** (Expression still may have jump lists.)
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
2015-12-18 16:53:36 +03:00
|
|
|
if (e->k != VNONRELOC) { /* no fixed register yet? */
|
|
|
|
luaK_reserveregs(fs, 1); /* get a register */
|
|
|
|
discharge2reg(fs, e, fs->freereg-1); /* put value there */
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2000-03-24 15:18:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 17:12:33 +03:00
|
|
|
static int code_loadbool (FuncState *fs, int A, OpCode op) {
|
2015-12-17 18:02:44 +03:00
|
|
|
luaK_getlabel(fs); /* those instructions may be jump targets */
|
2020-02-11 17:12:33 +03:00
|
|
|
return luaK_codeABC(fs, op, A, 0, 0);
|
2015-12-17 18:02:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** check whether list has any jump that do not produce a value
|
2015-12-18 16:53:36 +03:00
|
|
|
** or produce an inverted value
|
2015-12-17 18:02:44 +03:00
|
|
|
*/
|
|
|
|
static int need_value (FuncState *fs, int list) {
|
|
|
|
for (; list != NO_JUMP; list = getjump(fs, list)) {
|
|
|
|
Instruction i = *getjumpcontrol(fs, list);
|
|
|
|
if (GET_OPCODE(i) != OP_TESTSET) return 1;
|
|
|
|
}
|
|
|
|
return 0; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2018-01-18 19:24:31 +03:00
|
|
|
** Ensures final expression result (which includes results from its
|
2019-03-22 19:37:17 +03:00
|
|
|
** jump lists) is in register 'reg'.
|
2015-12-18 16:53:36 +03:00
|
|
|
** If expression has jumps, need to patch these jumps either to
|
|
|
|
** its final position or to "load" instructions (for those tests
|
|
|
|
** that do not produce values).
|
|
|
|
*/
|
2004-12-03 23:50:25 +03:00
|
|
|
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
2001-06-05 22:17:01 +04:00
|
|
|
discharge2reg(fs, e, reg);
|
2015-12-18 16:53:36 +03:00
|
|
|
if (e->k == VJMP) /* expression itself is a test? */
|
2014-10-25 15:50:46 +04:00
|
|
|
luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
|
2002-05-09 18:14:34 +04:00
|
|
|
if (hasjumps(e)) {
|
2002-04-22 18:37:09 +04:00
|
|
|
int final; /* position after whole expression */
|
2002-05-09 18:14:34 +04:00
|
|
|
int p_f = NO_JUMP; /* position of an eventual LOAD false */
|
|
|
|
int p_t = NO_JUMP; /* position of an eventual LOAD true */
|
2005-08-30 00:49:21 +04:00
|
|
|
if (need_value(fs, e->t) || need_value(fs, e->f)) {
|
2005-10-13 16:21:51 +04:00
|
|
|
int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
|
2020-02-11 17:12:33 +03:00
|
|
|
p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */
|
|
|
|
p_t = code_loadbool(fs, reg, OP_LOADTRUE);
|
2019-03-22 19:37:17 +03:00
|
|
|
/* jump around these booleans if 'e' is not a test */
|
2002-05-13 17:07:48 +04:00
|
|
|
luaK_patchtohere(fs, fj);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
2002-04-22 18:37:09 +04:00
|
|
|
final = luaK_getlabel(fs);
|
2005-08-30 00:49:21 +04:00
|
|
|
patchlistaux(fs, e->f, final, reg, p_f);
|
|
|
|
patchlistaux(fs, e->t, final, reg, p_t);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
2002-04-22 18:37:09 +04:00
|
|
|
e->f = e->t = NO_JUMP;
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = reg;
|
2001-06-05 22:17:01 +04:00
|
|
|
e->k = VNONRELOC;
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2018-01-18 19:24:31 +03:00
|
|
|
** Ensures final expression result is in next available register.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
freeexp(fs, e);
|
|
|
|
luaK_reserveregs(fs, 1);
|
2004-12-03 23:50:25 +03:00
|
|
|
exp2reg(fs, e, fs->freereg - 1);
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2018-01-18 19:24:31 +03:00
|
|
|
** Ensures final expression result is in some (any) register
|
|
|
|
** and return that register.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2015-12-18 16:53:36 +03:00
|
|
|
if (e->k == VNONRELOC) { /* expression already has a register? */
|
|
|
|
if (!hasjumps(e)) /* no jumps? */
|
|
|
|
return e->u.info; /* result is already in a register */
|
2019-07-10 20:00:22 +03:00
|
|
|
if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
|
2015-12-18 16:53:36 +03:00
|
|
|
exp2reg(fs, e, e->u.info); /* put final result in it */
|
2010-07-03 00:42:40 +04:00
|
|
|
return e->u.info;
|
2000-04-17 18:05:34 +04:00
|
|
|
}
|
2020-10-30 16:18:54 +03:00
|
|
|
/* else expression has jumps and cannot change its register
|
|
|
|
to hold the jump values, because it is a local variable.
|
|
|
|
Go through to the default case. */
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
2020-10-30 16:18:54 +03:00
|
|
|
luaK_exp2nextreg(fs, e); /* default: use next available register */
|
2010-07-03 00:42:40 +04:00
|
|
|
return e->u.info;
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2018-01-18 19:24:31 +03:00
|
|
|
** Ensures final expression result is either in a register
|
|
|
|
** or in an upvalue.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2010-02-26 23:40:29 +03:00
|
|
|
void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k != VUPVAL || hasjumps(e))
|
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2018-01-18 19:24:31 +03:00
|
|
|
** Ensures final expression result is either in a register
|
|
|
|
** or it is a constant.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_exp2val (FuncState *fs, expdesc *e) {
|
|
|
|
if (hasjumps(e))
|
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
else
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 17:23:45 +03:00
|
|
|
/*
|
|
|
|
** Try to make 'e' a K expression with an index in the range of R/K
|
|
|
|
** indices. Return true iff succeeded.
|
|
|
|
*/
|
|
|
|
static int luaK_exp2K (FuncState *fs, expdesc *e) {
|
|
|
|
if (!hasjumps(e)) {
|
|
|
|
int info;
|
|
|
|
switch (e->k) { /* move constants to 'k' */
|
2020-01-06 17:38:31 +03:00
|
|
|
case VTRUE: info = boolT(fs); break;
|
|
|
|
case VFALSE: info = boolF(fs); break;
|
2018-11-23 17:23:45 +03:00
|
|
|
case VNIL: info = nilK(fs); break;
|
|
|
|
case VKINT: info = luaK_intK(fs, e->u.ival); break;
|
|
|
|
case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
|
2019-07-17 20:26:56 +03:00
|
|
|
case VKSTR: info = stringK(fs, e->u.strval); break;
|
2018-11-23 17:23:45 +03:00
|
|
|
case VK: info = e->u.info; break;
|
|
|
|
default: return 0; /* not a constant */
|
|
|
|
}
|
|
|
|
if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */
|
|
|
|
e->k = VK; /* make expression a 'K' expression */
|
|
|
|
e->u.info = info;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* else, expression doesn't fit; leave it unchanged */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Ensures final expression result is in a valid R/K index
|
|
|
|
** (that is, it is either in a register or in 'k' with an index
|
|
|
|
** in the range of R/K indices).
|
2018-11-23 17:23:45 +03:00
|
|
|
** Returns 1 iff expression is K.
|
2016-12-22 16:08:50 +03:00
|
|
|
*/
|
2023-05-22 20:47:54 +03:00
|
|
|
static int exp2RK (FuncState *fs, expdesc *e) {
|
2018-11-23 17:23:45 +03:00
|
|
|
if (luaK_exp2K(fs, e))
|
|
|
|
return 1;
|
|
|
|
else { /* not a constant in the right range: put it in a register */
|
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
return 0;
|
2000-08-09 18:49:13 +04:00
|
|
|
}
|
2017-10-05 00:56:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-28 17:18:14 +03:00
|
|
|
static void codeABRK (FuncState *fs, OpCode o, int A, int B,
|
2017-10-05 00:56:32 +03:00
|
|
|
expdesc *ec) {
|
2023-05-22 20:47:54 +03:00
|
|
|
int k = exp2RK(fs, ec);
|
2024-06-28 17:18:14 +03:00
|
|
|
luaK_codeABCk(fs, o, A, B, ec->u.info, k);
|
2000-04-07 23:35:20 +04:00
|
|
|
}
|
2000-03-03 15:33:59 +03:00
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Generate code to store result of expression 'ex' into variable 'var'.
|
|
|
|
*/
|
2003-11-19 22:59:18 +03:00
|
|
|
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL: {
|
2003-11-19 22:59:18 +03:00
|
|
|
freeexp(fs, ex);
|
2020-12-02 21:13:13 +03:00
|
|
|
exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */
|
2002-04-22 18:37:09 +04:00
|
|
|
return;
|
2000-08-09 18:49:13 +04:00
|
|
|
}
|
2001-09-07 21:39:10 +04:00
|
|
|
case VUPVAL: {
|
2003-11-19 22:59:18 +03:00
|
|
|
int e = luaK_exp2anyreg(fs, ex);
|
2019-07-09 16:43:17 +03:00
|
|
|
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
|
2001-09-07 21:39:10 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-04-28 23:57:45 +03:00
|
|
|
case VINDEXUP: {
|
2017-10-05 00:56:32 +03:00
|
|
|
codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
|
2017-04-28 23:57:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXI: {
|
2017-10-05 00:56:32 +03:00
|
|
|
codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
|
2017-04-28 23:57:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXSTR: {
|
2017-10-05 00:56:32 +03:00
|
|
|
codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
|
2017-04-28 23:57:45 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VINDEXED: {
|
2017-10-05 00:56:32 +03:00
|
|
|
codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
|
2010-02-26 23:40:29 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
default: lua_assert(0); /* invalid var kind to store */
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
2003-11-19 22:59:18 +03:00
|
|
|
freeexp(fs, ex);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
2000-04-07 23:35:20 +04:00
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
2011-04-28 18:00:11 +04:00
|
|
|
int ereg;
|
2001-06-05 22:17:01 +04:00
|
|
|
luaK_exp2anyreg(fs, e);
|
2011-04-28 18:00:11 +04:00
|
|
|
ereg = e->u.info; /* register where 'e' was placed */
|
2001-06-05 22:17:01 +04:00
|
|
|
freeexp(fs, e);
|
2011-04-28 18:00:11 +04:00
|
|
|
e->u.info = fs->freereg; /* base register for op_self */
|
2015-12-18 16:53:36 +03:00
|
|
|
e->k = VNONRELOC; /* self expression has a fixed register */
|
2011-04-28 18:00:11 +04:00
|
|
|
luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
|
2017-10-05 00:56:32 +03:00
|
|
|
codeABRK(fs, OP_SELF, e->u.info, ereg, key);
|
2011-04-28 18:00:11 +04:00
|
|
|
freeexp(fs, key);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Negate condition 'e' (where 'e' is a comparison).
|
|
|
|
*/
|
|
|
|
static void negatecondition (FuncState *fs, expdesc *e) {
|
2010-07-03 00:42:40 +04:00
|
|
|
Instruction *pc = getjumpcontrol(fs, e->u.info);
|
2005-08-30 00:49:21 +04:00
|
|
|
lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
|
|
|
|
GET_OPCODE(*pc) != OP_TEST);
|
2017-11-28 15:58:18 +03:00
|
|
|
SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
|
2000-04-07 23:35:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
|
|
|
|
** is true, code will jump if 'e' is true.) Return jump position.
|
|
|
|
** Optimize when 'e' is 'not' something, inverting the condition
|
|
|
|
** and removing the 'not'.
|
|
|
|
*/
|
2002-05-06 19:51:41 +04:00
|
|
|
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
|
2017-12-14 17:24:02 +03:00
|
|
|
if (e->k == VRELOC) {
|
2016-01-05 19:22:37 +03:00
|
|
|
Instruction ie = getinstruction(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
if (GET_OPCODE(ie) == OP_NOT) {
|
2018-07-11 18:53:23 +03:00
|
|
|
removelastinstruction(fs); /* remove previous OP_NOT */
|
2019-03-22 19:37:17 +03:00
|
|
|
return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
/* else go through */
|
|
|
|
}
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2019-03-22 19:37:17 +03:00
|
|
|
return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
|
2000-04-07 23:35:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit code to go through if 'e' is true, jump otherwise.
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
2015-12-18 16:53:36 +03:00
|
|
|
int pc; /* pc of new jump */
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (e->k) {
|
2015-12-18 16:53:36 +03:00
|
|
|
case VJMP: { /* condition? */
|
|
|
|
negatecondition(fs, e); /* jump when it is false */
|
|
|
|
pc = e->u.info; /* save jump position */
|
2000-04-14 21:47:24 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2019-07-17 20:26:56 +03:00
|
|
|
case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
|
2011-01-31 17:28:41 +03:00
|
|
|
pc = NO_JUMP; /* always true; do nothing */
|
|
|
|
break;
|
2009-06-15 17:52:08 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
default: {
|
2015-12-18 16:53:36 +03:00
|
|
|
pc = jumponcond(fs, e, 0); /* jump when false */
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
|
|
|
|
luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
|
2005-08-17 22:32:09 +04:00
|
|
|
e->t = NO_JUMP;
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit code to go through if 'e' is false, jump otherwise.
|
|
|
|
*/
|
2011-08-30 20:26:41 +04:00
|
|
|
void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
2015-12-18 16:53:36 +03:00
|
|
|
int pc; /* pc of new jump */
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (e->k) {
|
2001-12-12 01:48:44 +03:00
|
|
|
case VJMP: {
|
2015-12-18 16:53:36 +03:00
|
|
|
pc = e->u.info; /* already jump if true */
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2011-01-31 17:28:41 +03:00
|
|
|
case VNIL: case VFALSE: {
|
|
|
|
pc = NO_JUMP; /* always false; do nothing */
|
|
|
|
break;
|
2009-06-15 17:52:08 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
default: {
|
2015-12-18 16:53:36 +03:00
|
|
|
pc = jumponcond(fs, e, 1); /* jump if true */
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
|
|
|
|
luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
|
2005-08-17 22:32:09 +04:00
|
|
|
e->f = NO_JUMP;
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Code 'not e', doing constant folding.
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
static void codenot (FuncState *fs, expdesc *e) {
|
|
|
|
switch (e->k) {
|
2001-12-12 01:48:44 +03:00
|
|
|
case VNIL: case VFALSE: {
|
2015-12-18 16:53:36 +03:00
|
|
|
e->k = VTRUE; /* true == not nil == not false */
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2019-07-17 20:26:56 +03:00
|
|
|
case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
|
2015-12-18 16:53:36 +03:00
|
|
|
e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VJMP: {
|
2015-12-18 16:53:36 +03:00
|
|
|
negatecondition(fs, e);
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2017-12-14 17:24:02 +03:00
|
|
|
case VRELOC:
|
2001-06-05 22:17:01 +04:00
|
|
|
case VNONRELOC: {
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC;
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
default: lua_assert(0); /* cannot happen */
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
/* interchange true and false lists */
|
|
|
|
{ int temp = e->f; e->f = e->t; e->t = temp; }
|
2015-12-18 16:53:36 +03:00
|
|
|
removevalues(fs, e->f); /* values are useless when negated */
|
2005-08-30 00:49:21 +04:00
|
|
|
removevalues(fs, e->t);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-24 23:26:39 +03:00
|
|
|
/*
|
2023-05-15 16:07:25 +03:00
|
|
|
** Check whether expression 'e' is a short literal string
|
2017-04-24 23:26:39 +03:00
|
|
|
*/
|
|
|
|
static int isKstr (FuncState *fs, expdesc *e) {
|
2017-10-03 01:50:57 +03:00
|
|
|
return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
|
2017-05-18 22:44:19 +03:00
|
|
|
ttisshrstring(&fs->f->k[e->u.info]));
|
2017-04-24 23:26:39 +03:00
|
|
|
}
|
|
|
|
|
2017-12-18 18:44:44 +03:00
|
|
|
/*
|
|
|
|
** Check whether expression 'e' is a literal integer.
|
|
|
|
*/
|
2023-05-22 20:47:54 +03:00
|
|
|
static int isKint (expdesc *e) {
|
2017-12-18 18:44:44 +03:00
|
|
|
return (e->k == VKINT && !hasjumps(e));
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:26:39 +03:00
|
|
|
|
2017-04-26 20:46:52 +03:00
|
|
|
/*
|
|
|
|
** Check whether expression 'e' is a literal integer in
|
2017-10-05 00:56:32 +03:00
|
|
|
** proper range to fit in register C
|
|
|
|
*/
|
|
|
|
static int isCint (expdesc *e) {
|
2023-05-22 20:47:54 +03:00
|
|
|
return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
|
2017-10-05 00:56:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check whether expression 'e' is a literal integer in
|
|
|
|
** proper range to fit in register sC
|
2017-04-26 20:46:52 +03:00
|
|
|
*/
|
2017-10-05 00:56:32 +03:00
|
|
|
static int isSCint (expdesc *e) {
|
2023-05-22 20:47:54 +03:00
|
|
|
return isKint(e) && fitsC(e->u.ival);
|
2017-04-26 20:46:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-22 21:41:20 +03:00
|
|
|
/*
|
|
|
|
** Check whether expression 'e' is a literal integer or float in
|
2019-03-22 19:37:17 +03:00
|
|
|
** proper range to fit in a register (sB or sC).
|
2017-11-22 21:41:20 +03:00
|
|
|
*/
|
2019-09-19 20:29:21 +03:00
|
|
|
static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
|
|
|
|
lua_Integer i;
|
2017-11-22 21:41:20 +03:00
|
|
|
if (e->k == VKINT)
|
2019-09-19 20:29:21 +03:00
|
|
|
i = e->u.ival;
|
2019-12-05 20:14:29 +03:00
|
|
|
else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
|
2019-03-22 19:37:17 +03:00
|
|
|
*isfloat = 1;
|
2019-09-19 20:29:21 +03:00
|
|
|
else
|
|
|
|
return 0; /* not a number */
|
|
|
|
if (!hasjumps(e) && fitsC(i)) {
|
|
|
|
*pi = int2sC(cast_int(i));
|
2017-12-15 21:53:48 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-11-22 21:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Create expression 't[k]'. 't' must have its final result already in a
|
2017-04-24 23:26:39 +03:00
|
|
|
** register or upvalue. Upvalues can only be indexed by literal strings.
|
2017-04-28 23:57:45 +03:00
|
|
|
** Keys can be literal strings in the constant table or arbitrary
|
|
|
|
** values in registers.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
2019-07-17 20:26:56 +03:00
|
|
|
if (k->k == VKSTR)
|
|
|
|
str2K(fs, k);
|
2019-05-17 17:11:44 +03:00
|
|
|
lua_assert(!hasjumps(t) &&
|
|
|
|
(t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
|
2019-07-17 20:26:56 +03:00
|
|
|
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
|
2017-04-24 23:26:39 +03:00
|
|
|
luaK_exp2anyreg(fs, t); /* put it in a register */
|
2017-04-28 23:57:45 +03:00
|
|
|
if (t->k == VUPVAL) {
|
2024-06-12 21:56:13 +03:00
|
|
|
int temp = t->u.info; /* upvalue index */
|
2023-05-15 16:07:25 +03:00
|
|
|
lua_assert(isKstr(fs, k));
|
2024-06-12 21:56:13 +03:00
|
|
|
t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */
|
2023-05-15 16:07:25 +03:00
|
|
|
t->u.ind.idx = k->u.info; /* literal short string */
|
2017-04-28 23:57:45 +03:00
|
|
|
t->k = VINDEXUP;
|
|
|
|
}
|
|
|
|
else {
|
2019-05-17 17:11:44 +03:00
|
|
|
/* register index of the table */
|
2020-12-02 21:13:13 +03:00
|
|
|
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
|
2019-05-17 17:11:44 +03:00
|
|
|
if (isKstr(fs, k)) {
|
2023-05-15 16:07:25 +03:00
|
|
|
t->u.ind.idx = k->u.info; /* literal short string */
|
2019-05-17 17:11:44 +03:00
|
|
|
t->k = VINDEXSTR;
|
|
|
|
}
|
|
|
|
else if (isCint(k)) {
|
|
|
|
t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */
|
|
|
|
t->k = VINDEXI;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
|
|
|
|
t->k = VINDEXED;
|
|
|
|
}
|
2017-04-28 23:57:45 +03:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 17:39:05 +04:00
|
|
|
/*
|
2015-12-18 16:53:36 +03:00
|
|
|
** Return false if folding can raise an error.
|
|
|
|
** Bitwise operations need operands convertible to integers; division
|
|
|
|
** operations cannot have 0 as divisor.
|
2014-03-06 17:39:05 +04:00
|
|
|
*/
|
2014-03-09 23:21:34 +04:00
|
|
|
static int validop (int op, TValue *v1, TValue *v2) {
|
2013-12-18 18:12:03 +04:00
|
|
|
switch (op) {
|
2014-03-06 17:39:05 +04:00
|
|
|
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
2014-12-29 19:49:25 +03:00
|
|
|
case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
|
|
|
|
lua_Integer i;
|
2021-02-24 18:24:42 +03:00
|
|
|
return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
|
|
|
|
luaV_tointegerns(v2, &i, LUA_FLOORN2I));
|
2014-12-29 19:49:25 +03:00
|
|
|
}
|
|
|
|
case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
|
|
|
|
return (nvalue(v2) != 0);
|
2013-12-18 18:12:03 +04:00
|
|
|
default: return 1; /* everything else is valid */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 17:39:05 +04:00
|
|
|
/*
|
2015-12-18 16:53:36 +03:00
|
|
|
** Try to "constant-fold" an operation; return 1 iff successful.
|
|
|
|
** (In this case, 'e1' has the final result.)
|
2014-03-06 17:39:05 +04:00
|
|
|
*/
|
2016-06-20 22:12:46 +03:00
|
|
|
static int constfolding (FuncState *fs, int op, expdesc *e1,
|
2019-07-01 18:42:31 +03:00
|
|
|
const expdesc *e2) {
|
2013-05-02 16:37:24 +04:00
|
|
|
TValue v1, v2, res;
|
2019-07-09 16:43:17 +03:00
|
|
|
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
2013-12-18 18:12:03 +04:00
|
|
|
return 0; /* non-numeric operands or not safe to fold */
|
2017-06-29 18:06:44 +03:00
|
|
|
luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
|
2013-05-02 16:37:24 +04:00
|
|
|
if (ttisinteger(&res)) {
|
|
|
|
e1->k = VKINT;
|
|
|
|
e1->u.ival = ivalue(&res);
|
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
|
2013-06-25 22:57:18 +04:00
|
|
|
lua_Number n = fltvalue(&res);
|
2014-12-29 19:49:25 +03:00
|
|
|
if (luai_numisnan(n) || n == 0)
|
|
|
|
return 0;
|
2013-05-02 16:37:24 +04:00
|
|
|
e1->k = VKFLT;
|
2013-06-25 22:57:18 +04:00
|
|
|
e1->u.nval = n;
|
2013-05-02 16:37:24 +04:00
|
|
|
}
|
2005-10-10 00:05:48 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-27 22:29:39 +03:00
|
|
|
/*
|
|
|
|
** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
|
|
|
|
*/
|
|
|
|
l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
|
|
|
|
lua_assert(baser <= opr &&
|
|
|
|
((baser == OPR_ADD && opr <= OPR_SHR) ||
|
|
|
|
(baser == OPR_LT && opr <= OPR_LE)));
|
|
|
|
return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
|
|
|
|
*/
|
|
|
|
l_sinline OpCode unopr2op (UnOpr opr) {
|
|
|
|
return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
|
|
|
|
cast_int(OP_UNM));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
|
|
|
|
*/
|
|
|
|
l_sinline TMS binopr2TM (BinOpr opr) {
|
|
|
|
lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
|
|
|
|
return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-24 17:59:22 +03:00
|
|
|
/*
|
2016-01-04 16:40:57 +03:00
|
|
|
** Emit code for unary expressions that "produce values"
|
|
|
|
** (everything but 'not').
|
|
|
|
** Expression to produce final result will be encoded in 'e'.
|
|
|
|
*/
|
|
|
|
static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
|
|
|
|
int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
|
|
|
|
freeexp(fs, e);
|
|
|
|
e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
|
2017-12-14 17:24:02 +03:00
|
|
|
e->k = VRELOC; /* all those operations are relocatable */
|
2016-01-04 16:40:57 +03:00
|
|
|
luaK_fixline(fs, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 17:23:45 +03:00
|
|
|
/*
|
|
|
|
** Emit code for binary expressions that "produce values"
|
|
|
|
** (everything but logical operators 'and'/'or' and comparison
|
|
|
|
** operators).
|
|
|
|
** Expression to produce final result will be encoded in 'e1'.
|
|
|
|
*/
|
2017-12-13 21:32:09 +03:00
|
|
|
static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
|
2019-08-28 15:58:03 +03:00
|
|
|
OpCode op, int v2, int flip, int line,
|
2019-08-27 19:59:39 +03:00
|
|
|
OpCode mmop, TMS event) {
|
2018-11-23 17:23:45 +03:00
|
|
|
int v1 = luaK_exp2anyreg(fs, e1);
|
2019-09-10 19:20:03 +03:00
|
|
|
int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
|
2017-12-13 21:32:09 +03:00
|
|
|
freeexps(fs, e1, e2);
|
|
|
|
e1->u.info = pc;
|
2017-12-14 17:24:02 +03:00
|
|
|
e1->k = VRELOC; /* all those operations are relocatable */
|
2017-12-13 21:32:09 +03:00
|
|
|
luaK_fixline(fs, line);
|
2019-08-28 15:58:03 +03:00
|
|
|
luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */
|
2019-08-27 19:59:39 +03:00
|
|
|
luaK_fixline(fs, line);
|
|
|
|
}
|
2017-12-13 21:32:09 +03:00
|
|
|
|
|
|
|
|
2016-01-04 16:40:57 +03:00
|
|
|
/*
|
2018-11-23 17:23:45 +03:00
|
|
|
** Emit code for binary expressions that "produce values" over
|
|
|
|
** two registers.
|
2014-11-24 17:59:22 +03:00
|
|
|
*/
|
2023-03-24 21:52:03 +03:00
|
|
|
static void codebinexpval (FuncState *fs, BinOpr opr,
|
2016-01-04 16:40:57 +03:00
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
2023-03-27 22:29:39 +03:00
|
|
|
OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
|
2022-04-25 20:42:51 +03:00
|
|
|
int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */
|
|
|
|
/* 'e1' must be already in a register or it is a constant */
|
|
|
|
lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
|
|
|
|
e1->k == VNONRELOC || e1->k == VRELOC);
|
2019-08-27 19:59:39 +03:00
|
|
|
lua_assert(OP_ADD <= op && op <= OP_SHR);
|
2023-03-27 22:29:39 +03:00
|
|
|
finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 20:41:30 +03:00
|
|
|
/*
|
2019-09-10 19:20:03 +03:00
|
|
|
** Code binary operators with immediate operands.
|
2017-12-04 20:41:30 +03:00
|
|
|
*/
|
|
|
|
static void codebini (FuncState *fs, OpCode op,
|
2019-08-28 15:58:03 +03:00
|
|
|
expdesc *e1, expdesc *e2, int flip, int line,
|
2019-08-27 19:59:39 +03:00
|
|
|
TMS event) {
|
2019-09-19 20:29:21 +03:00
|
|
|
int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */
|
|
|
|
lua_assert(e2->k == VKINT);
|
2019-08-28 15:58:03 +03:00
|
|
|
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
|
2018-11-23 17:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-06 23:52:46 +03:00
|
|
|
/*
|
|
|
|
** Code binary operators with K operand.
|
|
|
|
*/
|
|
|
|
static void codebinK (FuncState *fs, BinOpr opr,
|
|
|
|
expdesc *e1, expdesc *e2, int flip, int line) {
|
2023-03-27 22:29:39 +03:00
|
|
|
TMS event = binopr2TM(opr);
|
2022-05-06 23:52:46 +03:00
|
|
|
int v2 = e2->u.info; /* K index */
|
2023-03-27 22:29:39 +03:00
|
|
|
OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
|
2022-05-06 23:52:46 +03:00
|
|
|
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-01 23:24:37 +03:00
|
|
|
/* Try to code a binary operator negating its second operand.
|
|
|
|
** For the metamethod, 2nd operand must keep its original value.
|
2019-09-24 20:34:52 +03:00
|
|
|
*/
|
2019-10-01 23:24:37 +03:00
|
|
|
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
|
2019-09-24 20:34:52 +03:00
|
|
|
OpCode op, int line, TMS event) {
|
2023-05-22 20:47:54 +03:00
|
|
|
if (!isKint(e2))
|
2019-10-01 23:24:37 +03:00
|
|
|
return 0; /* not an integer constant */
|
|
|
|
else {
|
|
|
|
lua_Integer i2 = e2->u.ival;
|
|
|
|
if (!(fitsC(i2) && fitsC(-i2)))
|
|
|
|
return 0; /* not in the proper range */
|
|
|
|
else { /* operating a small integer constant */
|
|
|
|
int v2 = cast_int(i2);
|
|
|
|
finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
|
|
|
|
/* correct metamethod argument */
|
|
|
|
SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
|
|
|
|
return 1; /* successfully coded */
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 20:34:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 17:23:45 +03:00
|
|
|
static void swapexps (expdesc *e1, expdesc *e2) {
|
|
|
|
expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
|
2017-12-04 20:41:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-06 23:52:46 +03:00
|
|
|
/*
|
|
|
|
** Code binary operators with no constant operand.
|
|
|
|
*/
|
|
|
|
static void codebinNoK (FuncState *fs, BinOpr opr,
|
|
|
|
expdesc *e1, expdesc *e2, int flip, int line) {
|
|
|
|
if (flip)
|
|
|
|
swapexps(e1, e2); /* back to original order */
|
2023-03-24 21:52:03 +03:00
|
|
|
codebinexpval(fs, opr, e1, e2, line); /* use standard operators */
|
2022-05-06 23:52:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 18:49:24 +03:00
|
|
|
/*
|
|
|
|
** Code arithmetic operators ('+', '-', ...). If second operand is a
|
2019-09-10 19:20:03 +03:00
|
|
|
** constant in the proper range, use variant opcodes with K operands.
|
2017-10-04 18:49:24 +03:00
|
|
|
*/
|
2019-08-27 19:59:39 +03:00
|
|
|
static void codearith (FuncState *fs, BinOpr opr,
|
2017-10-04 18:49:24 +03:00
|
|
|
expdesc *e1, expdesc *e2, int flip, int line) {
|
2022-05-06 23:52:46 +03:00
|
|
|
if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */
|
|
|
|
codebinK(fs, opr, e1, e2, flip, line);
|
|
|
|
else /* 'e2' is neither an immediate nor a K operand */
|
|
|
|
codebinNoK(fs, opr, e1, e2, flip, line);
|
2017-11-22 21:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-04 18:49:24 +03:00
|
|
|
/*
|
|
|
|
** Code commutative operators ('+', '*'). If first operand is a
|
2018-11-23 17:23:45 +03:00
|
|
|
** numeric constant, change order of operands to try to use an
|
|
|
|
** immediate or K operator.
|
2017-10-04 18:49:24 +03:00
|
|
|
*/
|
2019-08-27 19:59:39 +03:00
|
|
|
static void codecommutative (FuncState *fs, BinOpr op,
|
2017-10-04 18:49:24 +03:00
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
|
|
|
int flip = 0;
|
2019-07-09 16:43:17 +03:00
|
|
|
if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
|
2018-11-23 17:23:45 +03:00
|
|
|
swapexps(e1, e2); /* change order */
|
2017-10-04 18:49:24 +03:00
|
|
|
flip = 1;
|
|
|
|
}
|
2019-09-10 19:20:03 +03:00
|
|
|
if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
|
2023-03-24 21:52:03 +03:00
|
|
|
codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
|
2019-09-10 19:20:03 +03:00
|
|
|
else
|
|
|
|
codearith(fs, op, e1, e2, flip, line);
|
2017-10-04 18:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 21:32:09 +03:00
|
|
|
/*
|
2022-04-25 20:42:51 +03:00
|
|
|
** Code bitwise operations; they are all commutative, so the function
|
2017-12-13 21:32:09 +03:00
|
|
|
** tries to put an integer constant as the 2nd operand (a K operand).
|
|
|
|
*/
|
|
|
|
static void codebitwise (FuncState *fs, BinOpr opr,
|
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
2019-08-28 15:58:03 +03:00
|
|
|
int flip = 0;
|
2022-05-06 23:52:46 +03:00
|
|
|
if (e1->k == VKINT) {
|
2017-12-13 21:32:09 +03:00
|
|
|
swapexps(e1, e2); /* 'e2' will be the constant operand */
|
2019-08-28 15:58:03 +03:00
|
|
|
flip = 1;
|
2017-12-13 21:32:09 +03:00
|
|
|
}
|
2022-05-06 23:52:46 +03:00
|
|
|
if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */
|
|
|
|
codebinK(fs, opr, e1, e2, flip, line);
|
|
|
|
else /* no constants */
|
|
|
|
codebinNoK(fs, opr, e1, e2, flip, line);
|
2017-12-13 21:32:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2019-03-22 19:37:17 +03:00
|
|
|
** Emit code for order comparisons. When using an immediate operand,
|
|
|
|
** 'isfloat' tells whether the original value was a float.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2023-03-24 21:52:03 +03:00
|
|
|
static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
2017-11-28 15:58:18 +03:00
|
|
|
int r1, r2;
|
2019-09-19 20:29:21 +03:00
|
|
|
int im;
|
2019-03-22 19:37:17 +03:00
|
|
|
int isfloat = 0;
|
2023-03-24 21:52:03 +03:00
|
|
|
OpCode op;
|
2019-03-22 19:37:17 +03:00
|
|
|
if (isSCnumber(e2, &im, &isfloat)) {
|
2017-11-28 15:58:18 +03:00
|
|
|
/* use immediate operand */
|
|
|
|
r1 = luaK_exp2anyreg(fs, e1);
|
2019-09-19 20:29:21 +03:00
|
|
|
r2 = im;
|
2023-03-27 22:29:39 +03:00
|
|
|
op = binopr2op(opr, OPR_LT, OP_LTI);
|
2017-11-28 15:58:18 +03:00
|
|
|
}
|
2019-03-22 19:37:17 +03:00
|
|
|
else if (isSCnumber(e1, &im, &isfloat)) {
|
2018-02-21 18:49:32 +03:00
|
|
|
/* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
|
2017-11-28 15:58:18 +03:00
|
|
|
r1 = luaK_exp2anyreg(fs, e2);
|
2019-09-19 20:29:21 +03:00
|
|
|
r2 = im;
|
2023-03-27 22:29:39 +03:00
|
|
|
op = binopr2op(opr, OPR_LT, OP_GTI);
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
2017-11-28 15:58:18 +03:00
|
|
|
else { /* regular case, compare two registers */
|
|
|
|
r1 = luaK_exp2anyreg(fs, e1);
|
|
|
|
r2 = luaK_exp2anyreg(fs, e2);
|
2023-03-27 22:29:39 +03:00
|
|
|
op = binopr2op(opr, OPR_LT, OP_LT);
|
2017-11-28 15:58:18 +03:00
|
|
|
}
|
|
|
|
freeexps(fs, e1, e2);
|
2019-03-22 19:37:17 +03:00
|
|
|
e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
|
2005-10-10 00:05:48 +04:00
|
|
|
e1->k = VJMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-16 15:59:14 +03:00
|
|
|
/*
|
|
|
|
** Emit code for equality comparisons ('==', '~=').
|
|
|
|
** 'e1' was already put as RK by 'luaK_infix'.
|
|
|
|
*/
|
|
|
|
static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
2017-11-22 21:41:20 +03:00
|
|
|
int r1, r2;
|
2019-09-19 20:29:21 +03:00
|
|
|
int im;
|
2019-03-22 19:37:17 +03:00
|
|
|
int isfloat = 0; /* not needed here, but kept for symmetry */
|
2017-11-22 21:41:20 +03:00
|
|
|
OpCode op;
|
|
|
|
if (e1->k != VNONRELOC) {
|
|
|
|
lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
|
|
|
|
swapexps(e1, e2);
|
|
|
|
}
|
2019-12-30 17:45:08 +03:00
|
|
|
r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */
|
2019-03-22 19:37:17 +03:00
|
|
|
if (isSCnumber(e2, &im, &isfloat)) {
|
2017-11-22 21:41:20 +03:00
|
|
|
op = OP_EQI;
|
2019-09-19 20:29:21 +03:00
|
|
|
r2 = im; /* immediate operand */
|
2017-11-22 21:41:20 +03:00
|
|
|
}
|
2023-05-22 20:47:54 +03:00
|
|
|
else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */
|
2017-11-22 21:41:20 +03:00
|
|
|
op = OP_EQK;
|
|
|
|
r2 = e2->u.info; /* constant index */
|
2017-11-16 15:59:14 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-11-22 21:41:20 +03:00
|
|
|
op = OP_EQ; /* will compare two registers */
|
|
|
|
r2 = luaK_exp2anyreg(fs, e2);
|
2017-11-16 15:59:14 +03:00
|
|
|
}
|
|
|
|
freeexps(fs, e1, e2);
|
2019-03-22 19:37:17 +03:00
|
|
|
e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
|
2017-11-16 15:59:14 +03:00
|
|
|
e1->k = VJMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2017-11-23 22:29:04 +03:00
|
|
|
** Apply prefix operation 'op' to expression 'e'.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2023-03-24 21:52:03 +03:00
|
|
|
void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
|
2016-06-20 22:12:46 +03:00
|
|
|
static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
|
2019-07-12 17:38:42 +03:00
|
|
|
luaK_dischargevars(fs, e);
|
2023-03-24 21:52:03 +03:00
|
|
|
switch (opr) {
|
2016-06-20 22:12:46 +03:00
|
|
|
case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
|
2023-03-24 21:52:03 +03:00
|
|
|
if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
|
2016-01-04 16:40:57 +03:00
|
|
|
break;
|
2019-07-01 18:42:31 +03:00
|
|
|
/* else */ /* FALLTHROUGH */
|
2016-01-04 16:40:57 +03:00
|
|
|
case OPR_LEN:
|
2023-03-27 22:29:39 +03:00
|
|
|
codeunexpval(fs, unopr2op(opr), e, line);
|
2005-03-16 19:59:21 +03:00
|
|
|
break;
|
2005-10-10 00:05:48 +04:00
|
|
|
case OPR_NOT: codenot(fs, e); break;
|
2005-03-16 19:59:21 +03:00
|
|
|
default: lua_assert(0);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Process 1st operand 'v' of binary operation 'op' before reading
|
|
|
|
** 2nd operand.
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
2019-07-01 18:42:31 +03:00
|
|
|
luaK_dischargevars(fs, v);
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (op) {
|
|
|
|
case OPR_AND: {
|
2015-12-18 16:53:36 +03:00
|
|
|
luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_OR: {
|
2015-12-18 16:53:36 +03:00
|
|
|
luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_CONCAT: {
|
2018-01-27 19:56:33 +03:00
|
|
|
luaK_exp2nextreg(fs, v); /* operand must be on the stack */
|
2000-04-07 23:35:20 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2013-04-26 17:08:29 +04:00
|
|
|
case OPR_ADD: case OPR_SUB:
|
|
|
|
case OPR_MUL: case OPR_DIV: case OPR_IDIV:
|
2013-12-18 18:12:03 +04:00
|
|
|
case OPR_MOD: case OPR_POW:
|
2013-12-31 00:47:58 +04:00
|
|
|
case OPR_BAND: case OPR_BOR: case OPR_BXOR:
|
|
|
|
case OPR_SHL: case OPR_SHR: {
|
2019-07-09 16:43:17 +03:00
|
|
|
if (!tonumeral(v, NULL))
|
2017-11-16 15:59:14 +03:00
|
|
|
luaK_exp2anyreg(fs, v);
|
2022-04-25 20:42:51 +03:00
|
|
|
/* else keep numeral, which may be folded or used as an immediate
|
|
|
|
operand */
|
2017-11-16 15:59:14 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_EQ: case OPR_NE: {
|
2019-07-09 16:43:17 +03:00
|
|
|
if (!tonumeral(v, NULL))
|
2023-05-22 20:47:54 +03:00
|
|
|
exp2RK(fs, v);
|
2017-11-22 21:41:20 +03:00
|
|
|
/* else keep numeral, which may be an immediate operand */
|
2017-11-16 15:59:14 +03:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2017-11-16 15:59:14 +03:00
|
|
|
case OPR_LT: case OPR_LE:
|
|
|
|
case OPR_GT: case OPR_GE: {
|
2019-09-19 20:29:21 +03:00
|
|
|
int dummy, dummy2;
|
2019-03-22 19:37:17 +03:00
|
|
|
if (!isSCnumber(v, &dummy, &dummy2))
|
2017-11-28 18:26:15 +03:00
|
|
|
luaK_exp2anyreg(fs, v);
|
2017-11-28 15:58:18 +03:00
|
|
|
/* else keep numeral, which may be an immediate operand */
|
2007-03-09 21:50:56 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-09-26 20:10:49 +03:00
|
|
|
default: lua_assert(0);
|
2000-04-07 23:35:20 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
2018-01-27 19:56:33 +03:00
|
|
|
/*
|
|
|
|
** Create code for '(e1 .. e2)'.
|
|
|
|
** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
|
|
|
|
** because concatenation is right associative), merge both CONCATs.
|
|
|
|
*/
|
|
|
|
static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
|
|
|
|
Instruction *ie2 = previousinstruction(fs);
|
|
|
|
if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */
|
|
|
|
int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */
|
|
|
|
lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
|
|
|
|
freeexp(fs, e2);
|
|
|
|
SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */
|
|
|
|
SETARG_B(*ie2, n + 1); /* will concatenate one more element */
|
|
|
|
}
|
|
|
|
else { /* 'e2' is not a concatenation */
|
|
|
|
luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */
|
|
|
|
freeexp(fs, e2);
|
|
|
|
luaK_fixline(fs, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Finalize code for binary operation, after reading 2nd operand.
|
|
|
|
*/
|
2017-11-28 15:58:18 +03:00
|
|
|
void luaK_posfix (FuncState *fs, BinOpr opr,
|
2010-04-17 16:46:32 +04:00
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
2019-07-01 18:42:31 +03:00
|
|
|
luaK_dischargevars(fs, e2);
|
2019-09-11 16:20:10 +03:00
|
|
|
if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
|
|
|
|
return; /* done by folding */
|
2017-11-28 15:58:18 +03:00
|
|
|
switch (opr) {
|
2001-06-05 22:17:01 +04:00
|
|
|
case OPR_AND: {
|
2019-07-01 18:42:31 +03:00
|
|
|
lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */
|
2006-03-21 22:28:49 +03:00
|
|
|
luaK_concat(fs, &e2->f, e1->f);
|
|
|
|
*e1 = *e2;
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_OR: {
|
2019-07-01 18:42:31 +03:00
|
|
|
lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */
|
2006-03-21 22:28:49 +03:00
|
|
|
luaK_concat(fs, &e2->t, e1->t);
|
|
|
|
*e1 = *e2;
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2018-01-27 19:56:33 +03:00
|
|
|
case OPR_CONCAT: { /* e1 .. e2 */
|
|
|
|
luaK_exp2nextreg(fs, e2);
|
|
|
|
codeconcat(fs, e1, e2, line);
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-10-04 18:49:24 +03:00
|
|
|
case OPR_ADD: case OPR_MUL: {
|
2019-09-11 16:20:10 +03:00
|
|
|
codecommutative(fs, opr, e1, e2, line);
|
2017-10-04 18:49:24 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-09-24 20:34:52 +03:00
|
|
|
case OPR_SUB: {
|
2019-10-01 23:24:37 +03:00
|
|
|
if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
|
|
|
|
break; /* coded as (r1 + -I) */
|
2019-10-04 22:17:04 +03:00
|
|
|
/* ELSE */
|
|
|
|
} /* FALLTHROUGH */
|
2019-09-24 20:34:52 +03:00
|
|
|
case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
|
2019-09-11 16:20:10 +03:00
|
|
|
codearith(fs, opr, e1, e2, 0, line);
|
2017-10-04 18:49:24 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-12-04 20:41:30 +03:00
|
|
|
case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
|
2019-09-11 16:20:10 +03:00
|
|
|
codebitwise(fs, opr, e1, e2, line);
|
2009-06-18 20:35:05 +04:00
|
|
|
break;
|
|
|
|
}
|
2017-12-04 20:41:30 +03:00
|
|
|
case OPR_SHL: {
|
2019-09-11 16:20:10 +03:00
|
|
|
if (isSCint(e1)) {
|
|
|
|
swapexps(e1, e2);
|
2019-09-24 20:34:52 +03:00
|
|
|
codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */
|
|
|
|
}
|
2019-10-01 23:24:37 +03:00
|
|
|
else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
|
|
|
|
/* coded as (r1 >> -I) */;
|
2017-12-04 20:41:30 +03:00
|
|
|
}
|
2019-09-24 20:34:52 +03:00
|
|
|
else /* regular case (two registers) */
|
2023-03-24 21:52:03 +03:00
|
|
|
codebinexpval(fs, opr, e1, e2, line);
|
2017-12-04 20:41:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_SHR: {
|
2019-10-01 23:24:37 +03:00
|
|
|
if (isSCint(e2))
|
2019-09-24 20:34:52 +03:00
|
|
|
codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
|
|
|
|
else /* regular case (two registers) */
|
2023-03-24 21:52:03 +03:00
|
|
|
codebinexpval(fs, opr, e1, e2, line);
|
2017-12-04 20:41:30 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-11-16 15:59:14 +03:00
|
|
|
case OPR_EQ: case OPR_NE: {
|
2018-04-04 17:23:41 +03:00
|
|
|
codeeq(fs, opr, e1, e2);
|
2017-11-28 15:58:18 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-11-16 15:59:14 +03:00
|
|
|
case OPR_GT: case OPR_GE: {
|
2017-11-28 15:58:18 +03:00
|
|
|
/* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
|
|
|
|
swapexps(e1, e2);
|
2023-03-27 22:29:39 +03:00
|
|
|
opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
|
|
|
|
} /* FALLTHROUGH */
|
|
|
|
case OPR_LT: case OPR_LE: {
|
|
|
|
codeorder(fs, opr, e1, e2);
|
2009-06-18 20:35:05 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-10-10 00:05:48 +04:00
|
|
|
default: lua_assert(0);
|
2000-05-22 22:44:46 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2018-07-11 18:53:23 +03:00
|
|
|
** Change line information associated with current position, by removing
|
|
|
|
** previous info and adding it again with new line.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2002-05-13 17:07:48 +04:00
|
|
|
void luaK_fixline (FuncState *fs, int line) {
|
2018-07-11 18:53:23 +03:00
|
|
|
removelastlineinfo(fs);
|
|
|
|
savelineinfo(fs, fs->f, line);
|
2002-05-13 17:07:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-27 18:59:22 +03:00
|
|
|
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
|
2019-07-15 20:59:35 +03:00
|
|
|
Instruction *inst = &fs->f->code[pc];
|
2020-02-27 18:59:22 +03:00
|
|
|
int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */
|
2024-06-28 17:18:14 +03:00
|
|
|
int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */
|
|
|
|
int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */
|
2020-02-27 18:59:22 +03:00
|
|
|
int k = (extra > 0); /* true iff needs extra argument */
|
2024-06-28 17:18:14 +03:00
|
|
|
*inst = CREATE_vABCk(OP_NEWTABLE, ra, rb, rc, k);
|
2019-07-15 20:59:35 +03:00
|
|
|
*(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit a SETLIST instruction.
|
|
|
|
** 'base' is register that keeps table;
|
|
|
|
** 'nelems' is #table plus those to be stored now;
|
|
|
|
** 'tostore' is number of values (in registers 'base + 1',...) to add to
|
|
|
|
** table (or LUA_MULTRET to add up to stack top).
|
|
|
|
*/
|
2004-10-04 23:01:53 +04:00
|
|
|
void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
|
2024-06-27 17:24:27 +03:00
|
|
|
lua_assert(tostore != 0);
|
2019-07-15 20:59:35 +03:00
|
|
|
if (tostore == LUA_MULTRET)
|
|
|
|
tostore = 0;
|
2024-06-28 17:18:14 +03:00
|
|
|
if (nelems <= MAXARG_vC)
|
|
|
|
luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
|
2019-07-15 20:59:35 +03:00
|
|
|
else {
|
2024-06-28 17:18:14 +03:00
|
|
|
int extra = nelems / (MAXARG_vC + 1);
|
|
|
|
nelems %= (MAXARG_vC + 1);
|
|
|
|
luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
|
2019-07-15 20:59:35 +03:00
|
|
|
codeextraarg(fs, extra);
|
2004-10-04 23:01:53 +04:00
|
|
|
}
|
|
|
|
fs->freereg = base + 1; /* free registers with list values */
|
|
|
|
}
|
|
|
|
|
2017-11-30 16:29:18 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** return the final target of a jump (skipping jumps to jumps)
|
|
|
|
*/
|
|
|
|
static int finaltarget (Instruction *code, int i) {
|
|
|
|
int count;
|
|
|
|
for (count = 0; count < 100; count++) { /* avoid infinite loops */
|
|
|
|
Instruction pc = code[i];
|
|
|
|
if (GET_OPCODE(pc) != OP_JMP)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
i += GETARG_sJ(pc) + 1;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Do a final pass over the code of a function, doing small peephole
|
|
|
|
** optimizations and adjustments.
|
|
|
|
*/
|
2024-06-28 17:18:14 +03:00
|
|
|
#include "lopnames.h"
|
2017-11-30 16:29:18 +03:00
|
|
|
void luaK_finish (FuncState *fs) {
|
|
|
|
int i;
|
|
|
|
Proto *p = fs->f;
|
|
|
|
for (i = 0; i < fs->pc; i++) {
|
|
|
|
Instruction *pc = &p->code[i];
|
2024-06-27 21:01:57 +03:00
|
|
|
/* avoid "not used" warnings when assert is off (for 'onelua.c') */
|
|
|
|
(void)luaP_isOT; (void)luaP_isIT;
|
|
|
|
lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
|
2017-11-30 16:29:18 +03:00
|
|
|
switch (GET_OPCODE(*pc)) {
|
2018-02-15 18:34:29 +03:00
|
|
|
case OP_RETURN0: case OP_RETURN1: {
|
2023-08-30 16:44:28 +03:00
|
|
|
if (!(fs->needclose || (p->flag & PF_ISVARARG)))
|
2018-02-15 18:34:29 +03:00
|
|
|
break; /* no extra work */
|
|
|
|
/* else use OP_RETURN to do the extra work */
|
|
|
|
SET_OPCODE(*pc, OP_RETURN);
|
2018-03-16 17:22:09 +03:00
|
|
|
} /* FALLTHROUGH */
|
2018-02-15 18:34:29 +03:00
|
|
|
case OP_RETURN: case OP_TAILCALL: {
|
2019-07-16 21:44:37 +03:00
|
|
|
if (fs->needclose)
|
|
|
|
SETARG_k(*pc, 1); /* signal that it needs to close */
|
2023-08-30 16:44:28 +03:00
|
|
|
if (p->flag & PF_ISVARARG)
|
2019-07-16 21:44:37 +03:00
|
|
|
SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
|
2017-11-30 16:29:18 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OP_JMP: {
|
|
|
|
int target = finaltarget(p->code, i);
|
|
|
|
fixjump(fs, i, target);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|