2000-02-22 16:31:19 +03:00
|
|
|
/*
|
2014-01-27 17:34:32 +04:00
|
|
|
** $Id: lcode.c,v 2.77 2013/12/30 20:47:58 roberto Exp roberto $
|
2000-02-22 16:31:19 +03:00
|
|
|
** Code generator for Lua
|
|
|
|
** See Copyright Notice in lua.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
|
|
|
|
2002-12-04 20:38:31 +03:00
|
|
|
#define lcode_c
|
2004-05-01 00:13:38 +04:00
|
|
|
#define LUA_CORE
|
2002-12-04 20:38:31 +03: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
|
|
|
|
|
|
|
|
2013-06-25 22:57:18 +04:00
|
|
|
/* test for x == -0 */
|
|
|
|
#if defined(signbit)
|
|
|
|
#define isminuszero(x) ((x) == 0.0 && signbit(x))
|
|
|
|
#else
|
|
|
|
#define isminuszero(x) ((x) == 0.0 && 1.0/(x) < 0.0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
#define hasjumps(e) ((e)->t != (e)->f)
|
|
|
|
|
|
|
|
|
2013-05-02 16:37:24 +04:00
|
|
|
static int tonumeral(expdesc *e, TValue *v) {
|
|
|
|
if (e->t != NO_JUMP || e->f != NO_JUMP)
|
|
|
|
return 0; /* not a numeral */
|
|
|
|
switch (e->k) {
|
|
|
|
case VKINT:
|
|
|
|
if (v) setivalue(v, e->u.ival);
|
|
|
|
return 1;
|
|
|
|
case VKFLT:
|
|
|
|
if (v) setnvalue(v, e->u.nval);
|
|
|
|
return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
if (GET_OPCODE(*previous) == OP_LOADNIL) {
|
2005-09-30 18:23:33 +04:00
|
|
|
int pfrom = GETARG_A(*previous);
|
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
|
|
|
|
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;
|
|
|
|
fs->jpc = NO_JUMP;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-03 23:50:25 +03:00
|
|
|
static void fixjump (FuncState *fs, int pc, int dest) {
|
2000-02-22 16:31:19 +03:00
|
|
|
Instruction *jmp = &fs->f->code[pc];
|
2002-05-10 23:22:11 +04:00
|
|
|
int offset = dest-(pc+1);
|
|
|
|
lua_assert(dest != NO_JUMP);
|
|
|
|
if (abs(offset) > MAXARG_sBx)
|
|
|
|
luaX_syntaxerror(fs->ls, "control structure too long");
|
|
|
|
SETARG_sBx(*jmp, offset);
|
2000-03-13 23:37:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-04 23:18:15 +03:00
|
|
|
/*
|
|
|
|
** returns current `pc' and marks it as a jump target (to avoid wrong
|
|
|
|
** 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-03 23:50:25 +03:00
|
|
|
static int getjump (FuncState *fs, int pc) {
|
2002-04-25 00:07:46 +04:00
|
|
|
int offset = GETARG_sBx(fs->f->code[pc]);
|
2001-06-05 22:17:01 +04: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 */
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-06 19:51:41 +04:00
|
|
|
/*
|
|
|
|
** check whether list has any jump that do not produce a value
|
|
|
|
** (or produce an inverted value)
|
|
|
|
*/
|
2005-08-30 00:49:21 +04:00
|
|
|
static int need_value (FuncState *fs, int list) {
|
2004-12-03 23:50:25 +03:00
|
|
|
for (; list != NO_JUMP; list = getjump(fs, list)) {
|
2002-05-06 19:51:41 +04:00
|
|
|
Instruction i = *getjumpcontrol(fs, list);
|
2005-08-30 00:49:21 +04:00
|
|
|
if (GET_OPCODE(i) != OP_TESTSET) return 1;
|
2002-05-06 19:51:41 +04:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
return 0; /* not found */
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2005-10-13 16:21:51 +04:00
|
|
|
else /* no register to put value or register already has the value */
|
2005-08-30 00:49:21 +04:00
|
|
|
*i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
|
2005-10-13 16:21:51 +04:00
|
|
|
|
|
|
|
return 1;
|
2001-06-08 16:29:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
2002-05-13 17:07:48 +04:00
|
|
|
if (target == fs->pc)
|
|
|
|
luaK_patchtohere(fs, list);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-01 21:03:10 +03:00
|
|
|
LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
|
|
|
|
level++; /* argument is +1 to reserve 0 as non-op */
|
|
|
|
while (list != NO_JUMP) {
|
|
|
|
int next = getjump(fs, list);
|
|
|
|
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);
|
|
|
|
list = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-08 22:10:32 +03:00
|
|
|
void luaK_patchtohere (FuncState *fs, int list) {
|
2002-05-10 23:22:11 +04:00
|
|
|
luaK_getlabel(fs);
|
|
|
|
luaK_concat(fs, &fs->jpc, list);
|
2002-03-08 22:10:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
2002-05-10 23:22:11 +04:00
|
|
|
if (l2 == NO_JUMP) return;
|
|
|
|
else if (*l1 == NO_JUMP)
|
2001-06-05 22:17:01 +04:00
|
|
|
*l1 = l2;
|
|
|
|
else {
|
|
|
|
int list = *l1;
|
|
|
|
int next;
|
2004-12-03 23:50:25 +03:00
|
|
|
while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
|
2001-06-05 22:17:01 +04:00
|
|
|
list = next;
|
2004-12-03 23:50:25 +03:00
|
|
|
fixjump(fs, list, l2);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-24 00:33:05 +04:00
|
|
|
static int luaK_code (FuncState *fs, Instruction i) {
|
|
|
|
Proto *f = fs->f;
|
|
|
|
dischargejpc(fs); /* `pc' will change */
|
|
|
|
/* 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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int codeextraarg (FuncState *fs, int a) {
|
|
|
|
lua_assert(a <= MAXARG_Ax);
|
|
|
|
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (newstack >= MAXSTACK)
|
2002-05-07 21:36:56 +04:00
|
|
|
luaX_syntaxerror(fs->ls, "function or expression too complex");
|
2005-12-22 19:19:56 +03:00
|
|
|
fs->f->maxstacksize = cast_byte(newstack);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-08 22:10:32 +03:00
|
|
|
void luaK_reserveregs (FuncState *fs, int n) {
|
|
|
|
luaK_checkstack(fs, n);
|
|
|
|
fs->freereg += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-08-30 20:01:37 +04:00
|
|
|
/*
|
|
|
|
** Use scanner's table to cache position of constants in contant list
|
|
|
|
** and try to reuse constants
|
|
|
|
*/
|
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? */
|
2013-04-15 19:43:34 +04:00
|
|
|
k = 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2001-12-12 01:48:44 +03:00
|
|
|
return addk(fs, &o, &o);
|
2000-03-03 15:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-25 22:57:18 +04:00
|
|
|
/*
|
|
|
|
** use userdata as key to avoid collision with float with same value;
|
|
|
|
** conversion to 'void*' used only for hash, no "precision" problems
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-25 22:57:18 +04:00
|
|
|
/*
|
|
|
|
** Both NaN and -0.0 should not go to the constant table, as they have
|
|
|
|
** problems with the hashing. (NaN is not ** a valid key,
|
|
|
|
** -0.0 collides with +0.0.)
|
|
|
|
*/
|
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-01-27 17:34:32 +04:00
|
|
|
lua_assert(!luai_numisnan(r) && !isminuszero(r));
|
2001-06-05 22:17:01 +04:00
|
|
|
setnvalue(&o, r);
|
2013-06-25 22:57:18 +04:00
|
|
|
return addk(fs, &o, &o);
|
2001-12-12 01:48:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-04 23:01:53 +04:00
|
|
|
static int boolK (FuncState *fs, int b) {
|
|
|
|
TValue o;
|
|
|
|
setbvalue(&o, b);
|
|
|
|
return addk(fs, &o, &o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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? */
|
2002-01-10 01:02:47 +03:00
|
|
|
SETARG_C(getcode(fs, e), nresults+1);
|
2004-05-31 22:51:50 +04:00
|
|
|
}
|
|
|
|
else if (e->k == VVARARG) {
|
|
|
|
SETARG_B(getcode(fs, e), nresults+1);
|
|
|
|
SETARG_A(getcode(fs, e), fs->freereg);
|
|
|
|
luaK_reserveregs(fs, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_setoneret (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k == VCALL) { /* expression is an open function call? */
|
|
|
|
e->k = VNONRELOC;
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = GETARG_A(getcode(fs, e));
|
2004-05-31 22:51:50 +04:00
|
|
|
}
|
|
|
|
else if (e->k == VVARARG) {
|
|
|
|
SETARG_B(getcode(fs, e), 2);
|
|
|
|
e->k = VRELOCABLE; /* can relocate its simple result */
|
2000-03-09 16:57:37 +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) {
|
|
|
|
case VLOCAL: {
|
|
|
|
e->k = VNONRELOC;
|
|
|
|
break;
|
|
|
|
}
|
2001-09-07 21:39:10 +04:00
|
|
|
case VUPVAL: {
|
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: {
|
2010-07-07 20:27:29 +04:00
|
|
|
OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
|
2010-07-03 00:42:40 +04:00
|
|
|
freereg(fs, e->u.ind.idx);
|
2010-07-07 20:27:29 +04:00
|
|
|
if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
|
|
|
|
freereg(fs, e->u.ind.t);
|
|
|
|
op = OP_GETTABLE;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2004-05-31 22:51:50 +04:00
|
|
|
case VVARARG:
|
2001-06-05 22:17:01 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-12 01:48:44 +03:00
|
|
|
static int code_label (FuncState *fs, int A, int b, int jump) {
|
2001-06-05 22:17:01 +04:00
|
|
|
luaK_getlabel(fs); /* those instructions may be jump targets */
|
2001-12-12 01:48:44 +03:00
|
|
|
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
2000-03-09 16:57:37 +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;
|
|
|
|
}
|
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: {
|
|
|
|
Instruction *pc = &getcode(fs, e);
|
|
|
|
SETARG_A(*pc, reg);
|
|
|
|
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: {
|
|
|
|
lua_assert(e->k == VVOID || e->k == VJMP);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void discharge2anyreg (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k != VNONRELOC) {
|
|
|
|
luaK_reserveregs(fs, 1);
|
|
|
|
discharge2reg(fs, e, fs->freereg-1);
|
2000-06-16 21:51:40 +04:00
|
|
|
}
|
2000-03-24 15:18:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2002-05-09 18:14:34 +04:00
|
|
|
if (e->k == VJMP)
|
2010-07-03 00:42:40 +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);
|
2002-05-09 18:14:34 +04:00
|
|
|
p_f = code_label(fs, reg, 0, 1);
|
|
|
|
p_t = code_label(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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2001-06-05 22:17:01 +04:00
|
|
|
if (e->k == VNONRELOC) {
|
2010-07-03 00:42:40 +04:00
|
|
|
if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
|
|
|
|
if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
|
|
|
|
exp2reg(fs, e, e->u.info); /* put value on it */
|
|
|
|
return e->u.info;
|
2000-04-17 18:05:34 +04:00
|
|
|
}
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
luaK_exp2nextreg(fs, e); /* default */
|
2010-07-03 00:42:40 +04:00
|
|
|
return e->u.info;
|
2000-03-09 16:57:37 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
|
|
luaK_exp2val(fs, e);
|
2001-12-12 01:48:44 +03:00
|
|
|
switch (e->k) {
|
2004-10-04 23:01:53 +04:00
|
|
|
case VTRUE:
|
|
|
|
case VFALSE:
|
2002-01-26 00:50:39 +03:00
|
|
|
case VNIL: {
|
2010-06-30 18:11:17 +04:00
|
|
|
if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
|
2001-12-12 01:48:44 +03:00
|
|
|
e->k = VK;
|
2010-07-03 00:42:40 +04:00
|
|
|
return RKASK(e->u.info);
|
2001-12-12 01:48:44 +03:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
2013-04-16 22:46:28 +04:00
|
|
|
case VKINT: {
|
|
|
|
e->u.info = luaK_intK(fs, e->u.ival);
|
|
|
|
e->k = VK;
|
|
|
|
goto vk;
|
|
|
|
}
|
|
|
|
case VKFLT: {
|
2010-07-03 00:42:40 +04:00
|
|
|
e->u.info = luaK_numberK(fs, e->u.nval);
|
2007-03-09 21:50:56 +03:00
|
|
|
e->k = VK;
|
|
|
|
/* go through */
|
|
|
|
}
|
2001-12-12 01:48:44 +03:00
|
|
|
case VK: {
|
2013-04-16 22:46:28 +04:00
|
|
|
vk:
|
2010-07-03 00:42:40 +04:00
|
|
|
if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
|
|
|
|
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
|
|
|
|
|
|
|
|
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);
|
2010-07-03 00:42:40 +04:00
|
|
|
exp2reg(fs, ex, var->u.info);
|
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;
|
|
|
|
}
|
2000-08-09 18:49:13 +04:00
|
|
|
default: {
|
2001-06-05 22:17:01 +04:00
|
|
|
lua_assert(0); /* invalid var kind to store */
|
|
|
|
break;
|
2000-03-09 16:57:37 +03:00
|
|
|
}
|
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
|
|
|
|
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 */
|
2001-06-05 22:17:01 +04:00
|
|
|
e->k = VNONRELOC;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void invertjump (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
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Instruction ie = getcode(fs, e);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-05 22:17:01 +04:00
|
|
|
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
|
|
|
int pc; /* pc of last jump */
|
2001-06-12 18:36:48 +04:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-05 22:17:01 +04:00
|
|
|
switch (e->k) {
|
|
|
|
case VJMP: {
|
|
|
|
invertjump(fs, e);
|
2010-07-03 00:42:40 +04:00
|
|
|
pc = e->u.info;
|
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: {
|
2002-05-06 19:51:41 +04:00
|
|
|
pc = jumponcond(fs, e, 0);
|
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
|
|
|
}
|
|
|
|
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
2005-08-17 22:32:09 +04:00
|
|
|
luaK_patchtohere(fs, e->t);
|
|
|
|
e->t = NO_JUMP;
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-30 20:26:41 +04:00
|
|
|
void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
2001-06-05 22:17:01 +04:00
|
|
|
int pc; /* pc of last 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: {
|
2010-07-03 00:42:40 +04:00
|
|
|
pc = e->u.info;
|
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: {
|
2002-05-06 19:51:41 +04:00
|
|
|
pc = jumponcond(fs, e, 1);
|
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
|
|
|
}
|
|
|
|
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
2005-08-17 22:32:09 +04:00
|
|
|
luaK_patchtohere(fs, e->f);
|
|
|
|
e->f = NO_JUMP;
|
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: {
|
|
|
|
e->k = VTRUE;
|
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: {
|
2001-12-12 01:48:44 +03:00
|
|
|
e->k = VFALSE;
|
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: {
|
|
|
|
invertjump(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
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
default: {
|
|
|
|
lua_assert(0); /* cannot happen */
|
2000-06-16 21:51:40 +04:00
|
|
|
break;
|
|
|
|
}
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
/* interchange true and false lists */
|
|
|
|
{ int temp = e->f; e->f = e->t; e->t = temp; }
|
2005-08-30 00:49:21 +04:00
|
|
|
removevalues(fs, e->f);
|
|
|
|
removevalues(fs, e->t);
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
2010-02-26 23:40:29 +03:00
|
|
|
lua_assert(!hasjumps(t));
|
2010-07-03 00:42:40 +04:00
|
|
|
t->u.ind.t = t->u.info;
|
|
|
|
t->u.ind.idx = luaK_exp2RK(fs, k);
|
2010-07-07 20:27:29 +04:00
|
|
|
t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
|
|
|
|
: check_exp(vkisinreg(t->k), VLOCAL);
|
|
|
|
t->k = VINDEXED;
|
2001-06-05 22:17:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-18 18:12:03 +04:00
|
|
|
/* return false if folding can raise an error */
|
|
|
|
static int validop (OpCode op, TValue *v1, TValue *v2) {
|
|
|
|
lua_Integer i;
|
|
|
|
switch (op) {
|
|
|
|
case OP_IDIV: /* division by 0 and conversion errors */
|
|
|
|
return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0);
|
2013-12-31 00:47:58 +04:00
|
|
|
case OP_BAND: case OP_BOR: case OP_BXOR:
|
|
|
|
case OP_SHL: case OP_SHR: case OP_BNOT: /* conversion errors */
|
2013-12-18 18:12:03 +04:00
|
|
|
return (tointeger(v1, &i) && tointeger(v2, &i));
|
|
|
|
case OP_MOD: /* integer module by 0 */
|
|
|
|
return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
|
|
|
|
case OP_POW: /* negative integer exponentiation */
|
|
|
|
return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0);
|
|
|
|
default: return 1; /* everything else is valid */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-24 21:37:52 +04:00
|
|
|
static int constfolding (OpCode 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 */
|
2013-05-02 16:37:24 +04:00
|
|
|
luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res);
|
|
|
|
if (ttisinteger(&res)) {
|
|
|
|
e1->k = VKINT;
|
|
|
|
e1->u.ival = ivalue(&res);
|
|
|
|
}
|
|
|
|
else {
|
2013-06-25 22:57:18 +04:00
|
|
|
lua_Number n = fltvalue(&res);
|
2014-01-27 17:34:32 +04:00
|
|
|
if (luai_numisnan(n) || isminuszero(n))
|
2013-06-25 22:57:18 +04:00
|
|
|
return 0; /* folds neither NaN nor -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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-17 16:46:32 +04:00
|
|
|
static void codearith (FuncState *fs, OpCode op,
|
|
|
|
expdesc *e1, expdesc *e2, int line) {
|
2013-05-02 16:37:24 +04:00
|
|
|
if (!constfolding(op, e1, e2)) { /* could not fold operation? */
|
2013-12-18 22:44:42 +04:00
|
|
|
int o1, o2;
|
2013-12-31 00:47:58 +04:00
|
|
|
if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) {
|
2013-12-18 22:44:42 +04:00
|
|
|
o2 = 0;
|
|
|
|
o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */
|
|
|
|
}
|
|
|
|
else { /* regular case (binary operators) */
|
|
|
|
o2 = luaK_exp2RK(fs, e2);
|
|
|
|
o1 = luaK_exp2RK(fs, e1);
|
|
|
|
}
|
2007-03-09 21:50:56 +03:00
|
|
|
if (o1 > o2) {
|
|
|
|
freeexp(fs, e1);
|
|
|
|
freeexp(fs, e2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
freeexp(fs, e2);
|
|
|
|
freeexp(fs, e1);
|
|
|
|
}
|
2010-07-03 00:42:40 +04:00
|
|
|
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
|
2005-10-10 00:05:48 +04:00
|
|
|
e1->k = VRELOCABLE;
|
2010-04-17 16:46:32 +04:00
|
|
|
luaK_fixline(fs, line);
|
2005-10-10 00:05:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
|
|
|
|
expdesc *e2) {
|
|
|
|
int o1 = luaK_exp2RK(fs, e1);
|
|
|
|
int o2 = luaK_exp2RK(fs, e2);
|
|
|
|
freeexp(fs, e2);
|
|
|
|
freeexp(fs, e1);
|
|
|
|
if (cond == 0 && op != OP_EQ) {
|
|
|
|
int temp; /* exchange args to replace by `<' or `<=' */
|
|
|
|
temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
|
|
|
|
cond = 1;
|
|
|
|
}
|
2010-07-03 00:42:40 +04:00
|
|
|
e1->u.info = condjump(fs, op, cond, o1, o2);
|
2005-10-10 00:05:48 +04:00
|
|
|
e1->k = VJMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-17 16:46:32 +04:00
|
|
|
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
|
2005-10-10 00:05:48 +04:00
|
|
|
expdesc e2;
|
2013-12-18 22:44:42 +04:00
|
|
|
e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
|
2005-03-16 19:59:21 +03:00
|
|
|
switch (op) {
|
2013-12-31 00:47:58 +04:00
|
|
|
case OPR_MINUS: case OPR_BNOT: case OPR_LEN: {
|
2013-12-18 22:44:42 +04:00
|
|
|
codearith(fs, op - OPR_MINUS + OP_UNM, e, &e2, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
|
|
|
switch (op) {
|
|
|
|
case OPR_AND: {
|
|
|
|
luaK_goiftrue(fs, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_OR: {
|
|
|
|
luaK_goiffalse(fs, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_CONCAT: {
|
|
|
|
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: {
|
2013-05-02 16:37:24 +04:00
|
|
|
if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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: {
|
|
|
|
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
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: {
|
|
|
|
lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
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);
|
|
|
|
if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
|
2010-07-03 00:42:40 +04:00
|
|
|
lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
|
2001-06-05 22:17:01 +04:00
|
|
|
freeexp(fs, e1);
|
2010-07-03 00:42:40 +04:00
|
|
|
SETARG_B(getcode(fs, e2), e1->u.info);
|
|
|
|
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' */
|
2010-04-17 16:46:32 +04:00
|
|
|
codearith(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: {
|
2010-04-17 16:46:32 +04:00
|
|
|
codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
|
2009-06-18 20:35:05 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_EQ: case OPR_LT: case OPR_LE: {
|
2010-01-11 20:38:30 +03:00
|
|
|
codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
|
2009-06-18 20:35:05 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_NE: case OPR_GT: case OPR_GE: {
|
2010-01-11 20:38:30 +03:00
|
|
|
codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-13 17:07:48 +04:00
|
|
|
void luaK_fixline (FuncState *fs, int line) {
|
|
|
|
fs->f->lineinfo[fs->pc - 1] = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
lua_assert(tostore != 0);
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|