2000-02-22 16:31:19 +03:00
|
|
|
/*
|
2016-05-13 22:09:46 +03:00
|
|
|
** $Id: lcode.c,v 2.108 2016/01/05 16:22:37 roberto Exp roberto $
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
2015-04-29 21:24:11 +03:00
|
|
|
/* Maximum number of registers in a Lua function (must fit in 8 bits) */
|
|
|
|
#define MAXREGS 255
|
2014-10-27 19:29:58 +03:00
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
#define hasjumps(e) ((e)->t != (e)->f)
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-05-02 16:37:24 +04:00
|
|
|
static int tonumeral(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
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2002-01-18 20:39:40 +03:00
|
|
|
Instruction *previous;
|
2011-04-19 20:22:13 +04:00
|
|
|
int l = from + n - 1; /* last register to set nil */
|
2005-09-30 18:23:33 +04:00
|
|
|
if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
|
2006-09-23 00:24:38 +04:00
|
|
|
previous = &fs->f->code[fs->pc-1];
|
2015-12-18 16:53:36 +03:00
|
|
|
if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
|
|
|
|
int pfrom = GETARG_A(*previous); /* get previous range */
|
2011-04-19 20:22:13 +04:00
|
|
|
int pl = pfrom + GETARG_B(*previous);
|
|
|
|
if ((pfrom <= from && from <= pl + 1) ||
|
|
|
|
(from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
|
2011-07-15 16:50:29 +04:00
|
|
|
if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
|
|
|
|
if (pl > l) l = pl; /* l = max(l, pl) */
|
2011-04-19 20:22:13 +04:00
|
|
|
SETARG_A(*previous, from);
|
|
|
|
SETARG_B(*previous, l - from);
|
2005-09-30 18:23:33 +04:00
|
|
|
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.
|
|
|
|
*/
|
2015-12-17 18:02:44 +03:00
|
|
|
static int getjump (FuncState *fs, int pc) {
|
|
|
|
int offset = GETARG_sBx(fs->f->code[pc]);
|
|
|
|
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);
|
|
|
|
if (abs(offset) > MAXARG_sBx)
|
|
|
|
luaX_syntaxerror(fs->ls, "control structure too long");
|
|
|
|
SETARG_sBx(*jmp, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
** can be fixed later (with 'fixjump'). If there are jumps to
|
|
|
|
** this position (kept in 'jpc'), link them all together so that
|
|
|
|
** 'patchlistaux' will fix all them directly to the final destination.
|
|
|
|
*/
|
2000-04-05 21:51:58 +04:00
|
|
|
int luaK_jump (FuncState *fs) {
|
2002-05-10 23:22:11 +04:00
|
|
|
int jpc = fs->jpc; /* save list of jumps to here */
|
|
|
|
int j;
|
2015-12-18 16:53:36 +03:00
|
|
|
fs->jpc = NO_JUMP; /* no more jumps to here */
|
2002-05-10 23:22:11 +04:00
|
|
|
j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
|
|
|
|
luaK_concat(fs, &j, jpc); /* keep them on hold */
|
2000-04-06 21:36:52 +04:00
|
|
|
return j;
|
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) {
|
|
|
|
luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2004-12-03 23:50:25 +03:00
|
|
|
static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
|
2001-06-11 18:56:42 +04:00
|
|
|
luaK_codeABC(fs, op, A, B, C);
|
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 */
|
2005-08-30 00:49:21 +04:00
|
|
|
*i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*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
|
|
|
/*
|
|
|
|
** Ensure all pending jumps to current position are fixed (jumping
|
|
|
|
** to current position with no values) and reset list of pending
|
|
|
|
** jumps
|
|
|
|
*/
|
2004-12-03 23:50:25 +03:00
|
|
|
static void dischargejpc (FuncState *fs) {
|
2005-08-30 00:49:21 +04:00
|
|
|
patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
|
2002-05-13 17:07:48 +04:00
|
|
|
fs->jpc = NO_JUMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Add elements in 'list' to list of pending jumps to "here"
|
|
|
|
** (current position)
|
|
|
|
*/
|
2015-12-17 18:02:44 +03:00
|
|
|
void luaK_patchtohere (FuncState *fs, int list) {
|
2015-12-18 16:53:36 +03:00
|
|
|
luaK_getlabel(fs); /* mark "here" as a jump target */
|
2015-12-17 18:02:44 +03:00
|
|
|
luaK_concat(fs, &fs->jpc, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2015-12-18 16:53:36 +03:00
|
|
|
if (target == fs->pc) /* 'target' is current position? */
|
|
|
|
luaK_patchtohere(fs, list); /* add list to pending jumps */
|
2002-05-13 17:07:48 +04:00
|
|
|
else {
|
|
|
|
lua_assert(target < fs->pc);
|
2005-08-30 00:49:21 +04:00
|
|
|
patchlistaux(fs, list, target, NO_REG, target);
|
2002-05-13 17:07:48 +04:00
|
|
|
}
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Path all jumps in 'list' to close upvalues up to given 'level'
|
|
|
|
** (The assertion checks that jumps either were closing nothing
|
|
|
|
** or were closing higher levels, from inner blocks.)
|
|
|
|
*/
|
2014-04-02 20:44:42 +04:00
|
|
|
void luaK_patchclose (FuncState *fs, int list, int level) {
|
2011-02-01 21:03:10 +03:00
|
|
|
level++; /* argument is +1 to reserve 0 as non-op */
|
2015-12-17 17:52:53 +03:00
|
|
|
for (; list != NO_JUMP; list = getjump(fs, list)) {
|
2011-02-01 21:03:10 +03:00
|
|
|
lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
|
|
|
|
(GETARG_A(fs->f->code[list]) == 0 ||
|
|
|
|
GETARG_A(fs->f->code[list]) >= level));
|
|
|
|
SETARG_A(fs->f->code[list], level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit instruction 'i', checking for array sizes and saving also its
|
|
|
|
** line information. Return 'i' position.
|
|
|
|
*/
|
2009-09-24 00:33:05 +04:00
|
|
|
static int luaK_code (FuncState *fs, Instruction i) {
|
|
|
|
Proto *f = fs->f;
|
2014-10-25 15:50:46 +04:00
|
|
|
dischargejpc(fs); /* 'pc' will change */
|
2009-09-24 00:33:05 +04:00
|
|
|
/* 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,
|
2009-09-24 00:33:05 +04:00
|
|
|
MAX_INT, "opcodes");
|
|
|
|
f->code[fs->pc] = i;
|
|
|
|
/* save corresponding line information */
|
2011-07-27 22:07:37 +04:00
|
|
|
luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
|
2009-09-24 00:33:05 +04:00
|
|
|
MAX_INT, "opcodes");
|
|
|
|
f->lineinfo[fs->pc] = fs->ls->lastline;
|
|
|
|
return fs->pc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Format and emit an 'iABC' instruction. (Assertions check consistency
|
|
|
|
** of parameters versus opcode.)
|
|
|
|
*/
|
2009-09-24 00:33:05 +04:00
|
|
|
int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
|
|
|
|
lua_assert(getOpMode(o) == iABC);
|
|
|
|
lua_assert(getBMode(o) != OpArgN || b == 0);
|
|
|
|
lua_assert(getCMode(o) != OpArgN || c == 0);
|
|
|
|
lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
|
|
|
|
return luaK_code(fs, CREATE_ABC(o, a, b, c));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Format and emit an 'iABx' instruction.
|
|
|
|
*/
|
2009-09-24 00:33:05 +04:00
|
|
|
int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
|
|
|
lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
|
|
|
|
lua_assert(getCMode(o) == OpArgN);
|
|
|
|
lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
|
|
|
|
return luaK_code(fs, CREATE_ABx(o, a, bc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit an "extra argument" instruction (format 'iAx')
|
|
|
|
*/
|
2009-09-24 00:33:05 +04:00
|
|
|
static int codeextraarg (FuncState *fs, int a) {
|
|
|
|
lua_assert(a <= MAXARG_Ax);
|
|
|
|
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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".
|
|
|
|
*/
|
2011-04-07 22:14:12 +04:00
|
|
|
int luaK_codek (FuncState *fs, int reg, int k) {
|
|
|
|
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);
|
2009-09-24 00:33:05 +04:00
|
|
|
codeextraarg(fs, k);
|
|
|
|
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) {
|
2014-10-27 19:29:58 +03:00
|
|
|
if (newstack >= MAXREGS)
|
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) {
|
2004-07-16 17:30:53 +04:00
|
|
|
if (!ISK(reg) && reg >= fs->nactvar) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (r1 > r2) {
|
|
|
|
freereg(fs, r1);
|
|
|
|
freereg(fs, r2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
freereg(fs, r2);
|
|
|
|
freereg(fs, r1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
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) {
|
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;
|
2013-08-30 20:01:37 +04:00
|
|
|
TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
|
2009-08-10 19:31:44 +04:00
|
|
|
int k, oldsize;
|
2013-08-30 20:01:37 +04:00
|
|
|
if (ttisinteger(idx)) { /* is there an index there? */
|
2014-04-01 18:39:55 +04:00
|
|
|
k = cast_int(ivalue(idx));
|
2013-08-30 20:01:37 +04:00
|
|
|
/* correct value? (warning: must distinguish floats from integers!) */
|
|
|
|
if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
|
|
|
|
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 */
|
2013-04-15 19:43:34 +04:00
|
|
|
setivalue(idx, k);
|
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.
|
|
|
|
*/
|
2002-01-26 00:50:39 +03:00
|
|
|
int luaK_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.
|
|
|
|
** Integers use userdata as keys to avoid collision with floats with
|
|
|
|
** same value; conversion to 'void*' is used only for hashing, so there
|
|
|
|
** are no "precision" problems.
|
2013-06-25 22:57:18 +04:00
|
|
|
*/
|
2013-04-25 23:35:19 +04:00
|
|
|
int luaK_intK (FuncState *fs, lua_Integer n) {
|
2013-06-20 21:37:31 +04:00
|
|
|
TValue k, o;
|
|
|
|
setpvalue(&k, cast(void*, cast(size_t, n)));
|
2013-04-16 22:46:28 +04:00
|
|
|
setivalue(&o, n);
|
2013-06-20 21:37:31 +04:00
|
|
|
return addk(fs, &k, &o);
|
2013-04-16 22:46:28 +04:00
|
|
|
}
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Add a float to list of constants and return its index.
|
|
|
|
*/
|
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;
|
2014-04-29 22:14:16 +04:00
|
|
|
setfltvalue(&o, r);
|
2015-12-18 16:53:36 +03:00
|
|
|
return addk(fs, &o, &o); /* use number itself as key */
|
2001-12-12 01:48:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Add a boolean to list of constants and return its index.
|
|
|
|
*/
|
2004-10-04 23:01:53 +04:00
|
|
|
static int boolK (FuncState *fs, int b) {
|
|
|
|
TValue o;
|
|
|
|
setbvalue(&o, b);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Fix an expression to return the number of results 'nresults'.
|
|
|
|
** Either 'e' is a multi-ret expression (function call or vararg)
|
|
|
|
** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
|
|
|
|
*/
|
2004-05-31 22:51:50 +04:00
|
|
|
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
2001-06-05 22:17:01 +04:00
|
|
|
if (e->k == VCALL) { /* expression is an open function call? */
|
2016-01-05 19:22:37 +03:00
|
|
|
SETARG_C(getinstruction(fs, e), nresults + 1);
|
2004-05-31 22:51:50 +04:00
|
|
|
}
|
|
|
|
else if (e->k == VVARARG) {
|
2016-01-05 19:22:37 +03:00
|
|
|
Instruction *pc = &getinstruction(fs, e);
|
|
|
|
SETARG_B(*pc, nresults + 1);
|
|
|
|
SETARG_A(*pc, fs->freereg);
|
2004-05-31 22:51:50 +04:00
|
|
|
luaK_reserveregs(fs, 1);
|
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
else lua_assert(nresults == LUA_MULTRET);
|
2004-05-31 22:51:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
** become VRELOCABLE (as OP_VARARG puts its results where it wants).
|
|
|
|
** (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) {
|
2016-01-05 19:22:37 +03:00
|
|
|
SETARG_B(getinstruction(fs, e), 2);
|
2004-05-31 22:51:50 +04:00
|
|
|
e->k = VRELOCABLE; /* can relocate its simple result */
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
2016-01-04 16:40:57 +03:00
|
|
|
** Ensure that expression 'e' is not a variable.
|
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) {
|
2016-01-04 16:40:57 +03:00
|
|
|
case VLOCAL: { /* already in a register */
|
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 */
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
|
2001-09-07 21:39:10 +04:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VINDEXED: {
|
2016-01-04 16:40:57 +03:00
|
|
|
OpCode op;
|
2010-07-03 00:42:40 +04:00
|
|
|
freereg(fs, e->u.ind.idx);
|
2016-01-04 16:40:57 +03:00
|
|
|
if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */
|
2010-07-07 20:27:29 +04:00
|
|
|
freereg(fs, e->u.ind.t);
|
|
|
|
op = OP_GETTABLE;
|
|
|
|
}
|
2016-01-04 16:40:57 +03:00
|
|
|
else {
|
|
|
|
lua_assert(e->u.ind.vt == VUPVAL);
|
|
|
|
op = OP_GETTABUP; /* 't' is in an upvalue */
|
|
|
|
}
|
2010-07-07 20:27:29 +04:00
|
|
|
e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
|
2010-02-26 23:40:29 +03:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
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
|
|
|
/*
|
|
|
|
** Ensures expression value is in register 'reg' (and therefore
|
|
|
|
** 'e' will become a non-relocatable expression).
|
|
|
|
*/
|
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;
|
|
|
|
}
|
2012-08-14 22:12:34 +04:00
|
|
|
case VFALSE: case VTRUE: {
|
2001-12-12 01:48:44 +03:00
|
|
|
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
|
|
|
break;
|
|
|
|
}
|
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: {
|
2009-06-10 20:52:03 +04:00
|
|
|
luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
|
2001-06-05 22:17:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-04-16 22:46:28 +04:00
|
|
|
case VKINT: {
|
|
|
|
luaK_codek(fs, reg, luaK_intK(fs, e->u.ival));
|
|
|
|
break;
|
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VRELOCABLE: {
|
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
|
|
|
/*
|
|
|
|
** Ensures expression value is in any register.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-17 18:02:44 +03:00
|
|
|
static int code_loadbool (FuncState *fs, int A, int b, int jump) {
|
|
|
|
luaK_getlabel(fs); /* those instructions may be jump targets */
|
|
|
|
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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
|
|
|
/*
|
|
|
|
** Ensures final expression result (including results from its jump
|
|
|
|
** lists) is in register 'reg'.
|
|
|
|
** 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);
|
2015-12-17 18:02:44 +03:00
|
|
|
p_f = code_loadbool(fs, reg, 0, 1);
|
|
|
|
p_t = code_loadbool(fs, reg, 1, 0);
|
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
|
|
|
/*
|
|
|
|
** Ensures final expression result (including results from its jump
|
|
|
|
** lists) is in next available register.
|
|
|
|
*/
|
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
|
|
|
/*
|
|
|
|
** Ensures final expression result (including results from its jump
|
|
|
|
** lists) is in some (any) register and return that register.
|
|
|
|
*/
|
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 */
|
2010-07-03 00:42:40 +04:00
|
|
|
if (e->u.info >= fs->nactvar) { /* 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
|
|
|
}
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
2015-12-18 16:53:36 +03:00
|
|
|
luaK_exp2nextreg(fs, e); /* otherwise, 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
|
|
|
/*
|
|
|
|
** Ensures final expression result is either in a register or in an
|
|
|
|
** upvalue.
|
|
|
|
*/
|
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
|
|
|
/*
|
|
|
|
** Ensures final expression result is either in a register or it is
|
|
|
|
** a constant.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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).
|
2016-01-04 16:40:57 +03:00
|
|
|
** Returns R/K index.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
|
|
luaK_exp2val(fs, e);
|
2016-01-04 16:40:57 +03:00
|
|
|
switch (e->k) { /* move constants to 'k' */
|
|
|
|
case VTRUE: e->u.info = boolK(fs, 1); goto vk;
|
|
|
|
case VFALSE: e->u.info = boolK(fs, 0); goto vk;
|
|
|
|
case VNIL: e->u.info = nilK(fs); goto vk;
|
|
|
|
case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk;
|
|
|
|
case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk;
|
|
|
|
case VK:
|
2013-04-16 22:46:28 +04:00
|
|
|
vk:
|
2016-01-04 16:40:57 +03:00
|
|
|
e->k = VK;
|
2014-10-30 21:53:28 +03:00
|
|
|
if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
|
2010-07-03 00:42:40 +04:00
|
|
|
return RKASK(e->u.info);
|
2001-12-12 01:48:44 +03:00
|
|
|
else break;
|
|
|
|
default: break;
|
2000-08-09 18:49:13 +04:00
|
|
|
}
|
2002-08-21 00:03:05 +04:00
|
|
|
/* not a constant in the right range: put it in a register */
|
2001-12-12 01:48:44 +03:00
|
|
|
return luaK_exp2anyreg(fs, e);
|
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);
|
2015-12-18 16:53:36 +03:00
|
|
|
exp2reg(fs, ex, var->u.info); /* 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);
|
2010-07-03 00:42:40 +04:00
|
|
|
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
|
2001-09-07 21:39:10 +04:00
|
|
|
break;
|
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VINDEXED: {
|
2010-07-07 20:27:29 +04:00
|
|
|
OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
|
2003-11-19 22:59:18 +03:00
|
|
|
int e = luaK_exp2RK(fs, ex);
|
2010-07-07 20:27:29 +04:00
|
|
|
luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
|
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 */
|
|
|
|
luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
|
|
|
|
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);
|
2002-08-21 00:03:05 +04:00
|
|
|
SETARG_A(*pc, !(GETARG_A(*pc)));
|
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) {
|
2001-06-05 22:17:01 +04:00
|
|
|
if (e->k == VRELOCABLE) {
|
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) {
|
|
|
|
fs->pc--; /* remove previous OP_NOT */
|
2005-08-30 00:49:21 +04:00
|
|
|
return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
/* else go through */
|
|
|
|
}
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2010-07-03 00:42:40 +04:00
|
|
|
return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 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
|
|
|
}
|
2013-04-16 22:46:28 +04:00
|
|
|
case VK: case VKFLT: case VKINT: 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) {
|
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 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
|
|
|
}
|
2013-04-16 22:46:28 +04:00
|
|
|
case VK: case VKFLT: case VKINT: 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
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
case VRELOCABLE:
|
|
|
|
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);
|
2001-06-05 22:17:01 +04:00
|
|
|
e->k = VRELOCABLE;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Create expression 't[k]'. 't' must have its final result already in a
|
|
|
|
** register or upvalue.
|
|
|
|
*/
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
2015-12-18 16:53:36 +03:00
|
|
|
lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL));
|
|
|
|
t->u.ind.t = t->u.info; /* register or upvalue index */
|
|
|
|
t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */
|
|
|
|
t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL;
|
2010-07-07 20:27:29 +04:00
|
|
|
t->k = VINDEXED;
|
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;
|
2013-12-18 18:12:03 +04:00
|
|
|
return (tointeger(v1, &i) && tointeger(v2, &i));
|
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
|
|
|
*/
|
2014-03-06 17:58:28 +04:00
|
|
|
static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) {
|
2013-05-02 16:37:24 +04:00
|
|
|
TValue v1, v2, res;
|
2013-12-18 18:12:03 +04:00
|
|
|
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
|
|
|
return 0; /* non-numeric operands or not safe to fold */
|
2014-12-29 19:49:25 +03:00
|
|
|
luaO_arith(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
e->k = VRELOCABLE; /* all those operations are relocatable */
|
|
|
|
luaK_fixline(fs, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Emit code for binary expressions that "produce values"
|
|
|
|
** (everything but logical operators 'and'/'or' and comparison
|
2015-12-18 16:53:36 +03:00
|
|
|
** operators).
|
2016-01-04 16:40:57 +03:00
|
|
|
** Expression to produce final result will be encoded in 'e1'.
|
2014-11-24 17:59:22 +03:00
|
|
|
*/
|
2016-01-04 16:40:57 +03:00
|
|
|
static void codebinexpval (FuncState *fs, OpCode op,
|
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
|
|
|
int rk1 = luaK_exp2RK(fs, e1); /* both operands are "RK" */
|
|
|
|
int rk2 = luaK_exp2RK(fs, e2);
|
|
|
|
freeexps(fs, e1, e2);
|
|
|
|
e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */
|
|
|
|
e1->k = VRELOCABLE; /* all those operations are relocatable */
|
|
|
|
luaK_fixline(fs, line);
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Emit code for comparisons.
|
2016-01-04 16:40:57 +03:00
|
|
|
** 'e1' was already put in R/K form by 'luaK_infix'.
|
2015-12-18 16:53:36 +03:00
|
|
|
*/
|
2016-01-04 16:40:57 +03:00
|
|
|
static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
|
|
|
|
int rk1 = (e1->k == VK) ? RKASK(e1->u.info)
|
|
|
|
: check_exp(e1->k == VNONRELOC, e1->u.info);
|
|
|
|
int rk2 = luaK_exp2RK(fs, e2);
|
|
|
|
freeexps(fs, e1, e2);
|
|
|
|
switch (opr) {
|
|
|
|
case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */
|
|
|
|
e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_GT: case OPR_GE: {
|
|
|
|
/* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */
|
|
|
|
OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
|
|
|
|
e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: { /* '==', '<', '<=' use their own opcodes */
|
|
|
|
OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
|
|
|
|
e1->u.info = condjump(fs, op, 1, rk1, rk2);
|
|
|
|
break;
|
|
|
|
}
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
|
|
|
e1->k = VJMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 16:53:36 +03:00
|
|
|
/*
|
|
|
|
** Aplly prefix operation 'op' to expression 'e'.
|
|
|
|
*/
|
2010-04-17 16:46:32 +04:00
|
|
|
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
2016-01-04 16:40:57 +03:00
|
|
|
static expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; /* fake 2nd operand */
|
2005-03-16 19:59:21 +03:00
|
|
|
switch (op) {
|
2016-01-04 16:40:57 +03:00
|
|
|
case OPR_MINUS: case OPR_BNOT:
|
|
|
|
if (constfolding(fs, op + LUA_OPUNM, e, &ef))
|
|
|
|
break;
|
2016-05-13 22:09:46 +03:00
|
|
|
/* FALLTHROUGH */
|
2016-01-04 16:40:57 +03:00
|
|
|
case OPR_LEN:
|
|
|
|
codeunexpval(fs, cast(OpCode, op + OP_UNM), 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) {
|
|
|
|
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: {
|
2014-10-25 15:50:46 +04: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: {
|
2015-12-18 16:53:36 +03:00
|
|
|
if (!tonumeral(v, NULL))
|
|
|
|
luaK_exp2RK(fs, v);
|
|
|
|
/* else keep numeral, which may be folded with 2nd operand */
|
2000-05-22 22:44:46 +04:00
|
|
|
break;
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2007-03-09 21:50:56 +03:00
|
|
|
default: {
|
|
|
|
luaK_exp2RK(fs, v);
|
|
|
|
break;
|
|
|
|
}
|
2000-04-07 23:35:20 +04:00
|
|
|
}
|
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.
|
|
|
|
** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
|
|
|
|
** concatenation is right associative), merge second CONCAT into first
|
|
|
|
** one.
|
|
|
|
*/
|
2010-04-17 16:46:32 +04:00
|
|
|
void luaK_posfix (FuncState *fs, BinOpr op,
|
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (op) {
|
|
|
|
case OPR_AND: {
|
2015-12-18 16:53:36 +03:00
|
|
|
lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e2);
|
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: {
|
2015-12-18 16:53:36 +03:00
|
|
|
lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e2);
|
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;
|
|
|
|
}
|
|
|
|
case OPR_CONCAT: {
|
|
|
|
luaK_exp2val(fs, e2);
|
2016-01-05 19:22:37 +03:00
|
|
|
if (e2->k == VRELOCABLE &&
|
|
|
|
GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) {
|
|
|
|
lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1);
|
2001-06-05 22:17:01 +04:00
|
|
|
freeexp(fs, e1);
|
2016-01-05 19:22:37 +03:00
|
|
|
SETARG_B(getinstruction(fs, e2), e1->u.info);
|
2010-07-03 00:42:40 +04:00
|
|
|
e1->k = VRELOCABLE; e1->u.info = e2->u.info;
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2005-10-10 00:05:48 +04:00
|
|
|
luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
|
2016-01-04 16:40:57 +03:00
|
|
|
codebinexpval(fs, OP_CONCAT, e1, e2, line);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-06-18 20:35:05 +04:00
|
|
|
case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
|
2013-12-18 18:12:03 +04:00
|
|
|
case OPR_IDIV: 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: {
|
2016-01-04 16:40:57 +03:00
|
|
|
if (!constfolding(fs, op + LUA_OPADD, e1, e2))
|
|
|
|
codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line);
|
2009-06-18 20:35:05 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-01-04 16:40:57 +03:00
|
|
|
case OPR_EQ: case OPR_LT: case OPR_LE:
|
2009-06-18 20:35:05 +04:00
|
|
|
case OPR_NE: case OPR_GT: case OPR_GE: {
|
2016-01-04 16:40:57 +03:00
|
|
|
codecomp(fs, op, 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
|
|
|
/*
|
|
|
|
** Change line information associated with current position.
|
|
|
|
*/
|
2002-05-13 17:07:48 +04:00
|
|
|
void luaK_fixline (FuncState *fs, int line) {
|
|
|
|
fs->f->lineinfo[fs->pc - 1] = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
|
|
|
|
int b = (tostore == LUA_MULTRET) ? 0 : tostore;
|
2015-12-18 16:53:36 +03:00
|
|
|
lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
|
2004-10-04 23:01:53 +04:00
|
|
|
if (c <= MAXARG_C)
|
|
|
|
luaK_codeABC(fs, OP_SETLIST, base, b, c);
|
2008-04-02 21:38:54 +04:00
|
|
|
else if (c <= MAXARG_Ax) {
|
2004-10-04 23:01:53 +04:00
|
|
|
luaK_codeABC(fs, OP_SETLIST, base, b, 0);
|
2009-09-24 00:33:05 +04:00
|
|
|
codeextraarg(fs, c);
|
2004-10-04 23:01:53 +04:00
|
|
|
}
|
2008-04-02 21:38:54 +04:00
|
|
|
else
|
|
|
|
luaX_syntaxerror(fs->ls, "constructor too long");
|
2004-10-04 23:01:53 +04:00
|
|
|
fs->freereg = base + 1; /* free registers with list values */
|
|
|
|
}
|
|
|
|
|