2000-02-22 21:31:19 +08:00
|
|
|
/*
|
2005-11-25 21:29:32 +08:00
|
|
|
** $Id: lcode.c,v 2.22 2005/11/16 11:55:27 roberto Exp roberto $
|
2000-02-22 21:31:19 +08:00
|
|
|
** Code generator for Lua
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-02-24 04:31:37 +08:00
|
|
|
#include <stdlib.h>
|
2000-04-05 04:48:44 +08:00
|
|
|
|
2002-12-05 01:38:31 +08:00
|
|
|
#define lcode_c
|
2004-05-01 04:13:38 +08:00
|
|
|
#define LUA_CORE
|
2002-12-05 01:38:31 +08:00
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
2000-02-22 21:31:19 +08:00
|
|
|
#include "lcode.h"
|
2001-06-06 02:17:01 +08:00
|
|
|
#include "ldebug.h"
|
2000-03-03 20:33:59 +08:00
|
|
|
#include "ldo.h"
|
2003-12-10 00:56:11 +08:00
|
|
|
#include "lgc.h"
|
2000-02-22 21:31:19 +08:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
2001-08-27 23:16:28 +08:00
|
|
|
#include "ltable.h"
|
2000-03-03 20:33:59 +08:00
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
#define hasjumps(e) ((e)->t != (e)->f)
|
|
|
|
|
|
|
|
|
2005-10-25 01:37:52 +08:00
|
|
|
static int isnumeral(expdesc *e) {
|
2005-10-10 04:05:48 +08:00
|
|
|
return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_nil (FuncState *fs, int from, int n) {
|
2002-01-19 01:39:40 +08:00
|
|
|
Instruction *previous;
|
2005-09-30 22:23:33 +08:00
|
|
|
if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
|
|
|
|
if (fs->pc == 0) /* function start? */
|
|
|
|
return; /* positions are already clean */
|
|
|
|
if (GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) {
|
|
|
|
int pfrom = GETARG_A(*previous);
|
|
|
|
int pto = GETARG_B(*previous);
|
|
|
|
if (pfrom <= from && from <= pto+1) { /* can connect both? */
|
|
|
|
if (from+n-1 > pto)
|
|
|
|
SETARG_B(*previous, from+n-1);
|
|
|
|
return;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
|
2000-04-06 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
2000-02-22 21:31:19 +08:00
|
|
|
|
2000-04-06 01:51:58 +08:00
|
|
|
int luaK_jump (FuncState *fs) {
|
2002-05-11 03:22:11 +08: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-07 01:36:52 +08:00
|
|
|
return j;
|
2000-04-06 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-30 04:49:21 +08:00
|
|
|
void luaK_ret (FuncState *fs, int first, int nret) {
|
|
|
|
luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-04 04:50:25 +08:00
|
|
|
static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
|
2001-06-11 22:56:42 +08:00
|
|
|
luaK_codeABC(fs, op, A, B, C);
|
2002-05-11 03:22:11 +08:00
|
|
|
return luaK_jump(fs);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-04 04:50:25 +08:00
|
|
|
static void fixjump (FuncState *fs, int pc, int dest) {
|
2000-02-22 21:31:19 +08:00
|
|
|
Instruction *jmp = &fs->f->code[pc];
|
2002-05-11 03:22:11 +08: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-14 04:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-05 04:18:15 +08: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-14 04:37:16 +08:00
|
|
|
int luaK_getlabel (FuncState *fs) {
|
2002-05-11 03:22:11 +08:00
|
|
|
fs->lasttarget = fs->pc;
|
2000-03-05 04:18:15 +08:00
|
|
|
return fs->pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-04 04:50:25 +08:00
|
|
|
static int getjump (FuncState *fs, int pc) {
|
2002-04-25 04:07:46 +08:00
|
|
|
int offset = GETARG_sBx(fs->f->code[pc]);
|
2001-06-06 02:17:01 +08: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 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
|
|
|
|
Instruction *pi = &fs->f->code[pc];
|
2003-05-14 20:09:12 +08:00
|
|
|
if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
|
2001-06-06 02:17:01 +08:00
|
|
|
return pi-1;
|
2002-01-17 06:03:57 +08:00
|
|
|
else
|
2001-06-06 02:17:01 +08:00
|
|
|
return pi;
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-06 23:51:41 +08:00
|
|
|
/*
|
|
|
|
** check whether list has any jump that do not produce a value
|
|
|
|
** (or produce an inverted value)
|
|
|
|
*/
|
2005-08-30 04:49:21 +08:00
|
|
|
static int need_value (FuncState *fs, int list) {
|
2004-12-04 04:50:25 +08:00
|
|
|
for (; list != NO_JUMP; list = getjump(fs, list)) {
|
2002-05-06 23:51:41 +08:00
|
|
|
Instruction i = *getjumpcontrol(fs, list);
|
2005-08-30 04:49:21 +08:00
|
|
|
if (GET_OPCODE(i) != OP_TESTSET) return 1;
|
2002-05-06 23:51:41 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
return 0; /* not found */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 20:21:51 +08: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 04:49:21 +08:00
|
|
|
SETARG_A(*i, reg);
|
2005-10-13 20:21:51 +08:00
|
|
|
else /* no register to put value or register already has the value */
|
2005-08-30 04:49:21 +08:00
|
|
|
*i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
|
2005-10-13 20:21:51 +08:00
|
|
|
|
|
|
|
return 1;
|
2001-06-08 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-30 04:49:21 +08:00
|
|
|
static void removevalues (FuncState *fs, int list) {
|
2005-10-13 20:21:51 +08:00
|
|
|
for (; list != NO_JUMP; list = getjump(fs, list))
|
|
|
|
patchtestreg(fs, list, NO_REG);
|
2005-08-30 04:49:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
|
|
|
|
int dtarget) {
|
2001-06-06 02:17:01 +08:00
|
|
|
while (list != NO_JUMP) {
|
2004-12-04 04:50:25 +08:00
|
|
|
int next = getjump(fs, list);
|
2005-10-13 20:21:51 +08:00
|
|
|
if (patchtestreg(fs, list, reg))
|
2005-08-30 04:49:21 +08:00
|
|
|
fixjump(fs, list, vtarget);
|
|
|
|
else
|
|
|
|
fixjump(fs, list, dtarget); /* jump to default target */
|
2001-06-06 02:17:01 +08:00
|
|
|
list = next;
|
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-04 04:50:25 +08:00
|
|
|
static void dischargejpc (FuncState *fs) {
|
2005-08-30 04:49:21 +08:00
|
|
|
patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
|
2002-05-13 21:07:48 +08:00
|
|
|
fs->jpc = NO_JUMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
2002-05-13 21:07:48 +08:00
|
|
|
if (target == fs->pc)
|
|
|
|
luaK_patchtohere(fs, list);
|
|
|
|
else {
|
|
|
|
lua_assert(target < fs->pc);
|
2005-08-30 04:49:21 +08:00
|
|
|
patchlistaux(fs, list, target, NO_REG, target);
|
2002-05-13 21:07:48 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-09 03:10:32 +08:00
|
|
|
void luaK_patchtohere (FuncState *fs, int list) {
|
2002-05-11 03:22:11 +08:00
|
|
|
luaK_getlabel(fs);
|
|
|
|
luaK_concat(fs, &fs->jpc, list);
|
2002-03-09 03:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
2002-05-11 03:22:11 +08:00
|
|
|
if (l2 == NO_JUMP) return;
|
|
|
|
else if (*l1 == NO_JUMP)
|
2001-06-06 02:17:01 +08:00
|
|
|
*l1 = l2;
|
|
|
|
else {
|
|
|
|
int list = *l1;
|
|
|
|
int next;
|
2004-12-04 04:50:25 +08:00
|
|
|
while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
|
2001-06-06 02:17:01 +08:00
|
|
|
list = next;
|
2004-12-04 04:50:25 +08:00
|
|
|
fixjump(fs, list, l2);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-11 20:34:22 +08:00
|
|
|
void luaK_checkstack (FuncState *fs, int n) {
|
2002-03-09 03:10:32 +08:00
|
|
|
int newstack = fs->freereg + n;
|
|
|
|
if (newstack > fs->f->maxstacksize) {
|
|
|
|
if (newstack >= MAXSTACK)
|
2002-05-08 01:36:56 +08:00
|
|
|
luaX_syntaxerror(fs->ls, "function or expression too complex");
|
2002-03-09 03:10:32 +08:00
|
|
|
fs->f->maxstacksize = cast(lu_byte, newstack);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-09 03:10:32 +08:00
|
|
|
void luaK_reserveregs (FuncState *fs, int n) {
|
|
|
|
luaK_checkstack(fs, n);
|
|
|
|
fs->freereg += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void freereg (FuncState *fs, int reg) {
|
2004-07-16 21:30:53 +08:00
|
|
|
if (!ISK(reg) && reg >= fs->nactvar) {
|
2001-06-06 02:17:01 +08:00
|
|
|
fs->freereg--;
|
|
|
|
lua_assert(reg == fs->freereg);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2000-03-05 04:18:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void freeexp (FuncState *fs, expdesc *e) {
|
|
|
|
if (e->k == VNONRELOC)
|
2005-10-10 04:05:48 +08:00
|
|
|
freereg(fs, e->u.s.info);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2000-04-08 03:35:20 +08:00
|
|
|
|
2000-03-03 20:33:59 +08:00
|
|
|
|
2003-12-10 20:13:36 +08:00
|
|
|
static int addk (FuncState *fs, TValue *k, TValue *v) {
|
|
|
|
lua_State *L = fs->L;
|
|
|
|
TValue *idx = luaH_set(L, fs->h, k);
|
2003-08-28 05:01:44 +08:00
|
|
|
Proto *f = fs->f;
|
|
|
|
int oldsize = f->sizek;
|
2003-02-27 20:33:07 +08:00
|
|
|
if (ttisnumber(idx)) {
|
|
|
|
lua_assert(luaO_rawequalObj(&fs->f->k[cast(int, nvalue(idx))], v));
|
|
|
|
return cast(int, nvalue(idx));
|
2001-08-27 23:16:28 +08:00
|
|
|
}
|
|
|
|
else { /* constant not found; create a new entry */
|
2003-08-28 05:01:44 +08:00
|
|
|
setnvalue(idx, cast(lua_Number, fs->nk));
|
2003-12-10 20:13:36 +08:00
|
|
|
luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
|
2002-04-25 04:07:46 +08:00
|
|
|
MAXARG_Bx, "constant table overflow");
|
2003-08-28 05:01:44 +08:00
|
|
|
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
|
2003-12-10 20:13:36 +08:00
|
|
|
setobj(L, &f->k[fs->nk], v);
|
2005-06-08 02:53:45 +08:00
|
|
|
luaC_barrier(L, f, v);
|
2001-08-27 23:16:28 +08:00
|
|
|
return fs->nk++;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-26 05:50:39 +08:00
|
|
|
int luaK_stringK (FuncState *fs, TString *s) {
|
2003-12-10 20:13:36 +08:00
|
|
|
TValue o;
|
|
|
|
setsvalue(fs->L, &o, s);
|
2001-12-12 06:48:44 +08:00
|
|
|
return addk(fs, &o, &o);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-26 05:50:39 +08:00
|
|
|
int luaK_numberK (FuncState *fs, lua_Number r) {
|
2003-12-10 20:13:36 +08:00
|
|
|
TValue o;
|
2001-06-06 02:17:01 +08:00
|
|
|
setnvalue(&o, r);
|
2001-12-12 06:48:44 +08:00
|
|
|
return addk(fs, &o, &o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 03:01:53 +08: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 20:13:36 +08:00
|
|
|
TValue k, v;
|
2001-12-12 06:48:44 +08:00
|
|
|
setnilvalue(&v);
|
2003-12-10 20:13:36 +08:00
|
|
|
/* cannot use nil as key; instead use table itself to represent nil */
|
|
|
|
sethvalue(fs->L, &k, fs->h);
|
2001-12-12 06:48:44 +08:00
|
|
|
return addk(fs, &k, &v);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-01 02:51:50 +08:00
|
|
|
void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
|
2001-06-06 02:17:01 +08:00
|
|
|
if (e->k == VCALL) { /* expression is an open function call? */
|
2002-01-10 06:02:47 +08:00
|
|
|
SETARG_C(getcode(fs, e), nresults+1);
|
2004-06-01 02:51:50 +08: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;
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = GETARG_A(getcode(fs, e));
|
2004-06-01 02:51:50 +08:00
|
|
|
}
|
|
|
|
else if (e->k == VVARARG) {
|
|
|
|
SETARG_B(getcode(fs, e), 2);
|
|
|
|
e->k = VRELOCABLE; /* can relocate its simple result */
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-12 22:36:48 +08:00
|
|
|
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
|
|
|
case VLOCAL: {
|
|
|
|
e->k = VNONRELOC;
|
|
|
|
break;
|
|
|
|
}
|
2001-09-08 01:39:10 +08:00
|
|
|
case VUPVAL: {
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
|
2001-09-08 01:39:10 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VGLOBAL: {
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXED: {
|
2005-10-10 04:05:48 +08:00
|
|
|
freereg(fs, e->u.s.aux);
|
|
|
|
freereg(fs, e->u.s.info);
|
|
|
|
e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-01 02:51:50 +08:00
|
|
|
case VVARARG:
|
2001-06-06 02:17:01 +08:00
|
|
|
case VCALL: {
|
2004-06-01 02:51:50 +08:00
|
|
|
luaK_setoneret(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break; /* there is one value available (somewhere) */
|
|
|
|
}
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-12 06:48:44 +08:00
|
|
|
static int code_label (FuncState *fs, int A, int b, int jump) {
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_getlabel(fs); /* those instructions may be jump targets */
|
2001-12-12 06:48:44 +08:00
|
|
|
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
|
|
|
case VNIL: {
|
|
|
|
luaK_nil(fs, reg, 1);
|
|
|
|
break;
|
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VFALSE: case VTRUE: {
|
|
|
|
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
|
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VK: {
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VKNUM: {
|
|
|
|
luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VRELOCABLE: {
|
|
|
|
Instruction *pc = &getcode(fs, e);
|
|
|
|
SETARG_A(*pc, reg);
|
|
|
|
break;
|
|
|
|
}
|
2002-04-22 22:37:09 +08:00
|
|
|
case VNONRELOC: {
|
2005-10-10 04:05:48 +08:00
|
|
|
if (reg != e->u.s.info)
|
|
|
|
luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
|
2002-04-22 22:37:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
lua_assert(e->k == VVOID || e->k == VJMP);
|
|
|
|
return; /* nothing to do... */
|
|
|
|
}
|
2000-03-24 20:18:30 +08:00
|
|
|
}
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = reg;
|
2001-06-06 02:17:01 +08: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-17 01:51:40 +08:00
|
|
|
}
|
2000-03-24 20:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-04 04:50:25 +08:00
|
|
|
static void exp2reg (FuncState *fs, expdesc *e, int reg) {
|
2001-06-06 02:17:01 +08:00
|
|
|
discharge2reg(fs, e, reg);
|
2002-05-09 22:14:34 +08:00
|
|
|
if (e->k == VJMP)
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */
|
2002-05-09 22:14:34 +08:00
|
|
|
if (hasjumps(e)) {
|
2002-04-22 22:37:09 +08:00
|
|
|
int final; /* position after whole expression */
|
2002-05-09 22:14:34 +08: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 04:49:21 +08:00
|
|
|
if (need_value(fs, e->t) || need_value(fs, e->f)) {
|
2005-10-13 20:21:51 +08:00
|
|
|
int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
|
2002-05-09 22:14:34 +08:00
|
|
|
p_f = code_label(fs, reg, 0, 1);
|
|
|
|
p_t = code_label(fs, reg, 1, 0);
|
2002-05-13 21:07:48 +08:00
|
|
|
luaK_patchtohere(fs, fj);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
2002-04-22 22:37:09 +08:00
|
|
|
final = luaK_getlabel(fs);
|
2005-08-30 04:49:21 +08:00
|
|
|
patchlistaux(fs, e->f, final, reg, p_f);
|
|
|
|
patchlistaux(fs, e->t, final, reg, p_t);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
2002-04-22 22:37:09 +08:00
|
|
|
e->f = e->t = NO_JUMP;
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = reg;
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VNONRELOC;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
freeexp(fs, e);
|
|
|
|
luaK_reserveregs(fs, 1);
|
2004-12-04 04:50:25 +08:00
|
|
|
exp2reg(fs, e, fs->freereg - 1);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
if (e->k == VNONRELOC) {
|
2005-10-10 04:05:48 +08:00
|
|
|
if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */
|
|
|
|
if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */
|
|
|
|
exp2reg(fs, e, e->u.s.info); /* put value on it */
|
|
|
|
return e->u.s.info;
|
2000-04-17 22:05:34 +08:00
|
|
|
}
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_exp2nextreg(fs, e); /* default */
|
2005-10-10 04:05:48 +08:00
|
|
|
return e->u.s.info;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_exp2val (FuncState *fs, expdesc *e) {
|
|
|
|
if (hasjumps(e))
|
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
else
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
int luaK_exp2RK (FuncState *fs, expdesc *e) {
|
|
|
|
luaK_exp2val(fs, e);
|
2001-12-12 06:48:44 +08:00
|
|
|
switch (e->k) {
|
2005-10-10 04:05:48 +08:00
|
|
|
case VKNUM:
|
2004-10-05 03:01:53 +08:00
|
|
|
case VTRUE:
|
|
|
|
case VFALSE:
|
2002-01-26 05:50:39 +08:00
|
|
|
case VNIL: {
|
2004-06-30 02:49:02 +08:00
|
|
|
if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = (e->k == VNIL) ? nilK(fs) :
|
|
|
|
(e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
|
|
|
|
boolK(fs, (e->k == VTRUE));
|
2001-12-12 06:48:44 +08:00
|
|
|
e->k = VK;
|
2005-10-10 04:05:48 +08:00
|
|
|
return RKASK(e->u.s.info);
|
2001-12-12 06:48:44 +08:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
case VK: {
|
2005-10-10 04:05:48 +08:00
|
|
|
if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
|
|
|
|
return RKASK(e->u.s.info);
|
2001-12-12 06:48:44 +08:00
|
|
|
else break;
|
|
|
|
}
|
|
|
|
default: break;
|
2000-08-09 22:49:13 +08:00
|
|
|
}
|
2002-08-21 04:03:05 +08:00
|
|
|
/* not a constant in the right range: put it in a register */
|
2001-12-12 06:48:44 +08:00
|
|
|
return luaK_exp2anyreg(fs, e);
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
|
|
|
|
|
2003-11-20 03:59:18 +08:00
|
|
|
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL: {
|
2003-11-20 03:59:18 +08:00
|
|
|
freeexp(fs, ex);
|
2005-10-10 04:05:48 +08:00
|
|
|
exp2reg(fs, ex, var->u.s.info);
|
2002-04-22 22:37:09 +08:00
|
|
|
return;
|
2000-08-09 22:49:13 +08:00
|
|
|
}
|
2001-09-08 01:39:10 +08:00
|
|
|
case VUPVAL: {
|
2003-11-20 03:59:18 +08:00
|
|
|
int e = luaK_exp2anyreg(fs, ex);
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
|
2001-09-08 01:39:10 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VGLOBAL: {
|
2003-11-20 03:59:18 +08:00
|
|
|
int e = luaK_exp2anyreg(fs, ex);
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VINDEXED: {
|
2003-11-20 03:59:18 +08:00
|
|
|
int e = luaK_exp2RK(fs, ex);
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
|
2000-08-09 22:49:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2001-06-06 02:17:01 +08:00
|
|
|
lua_assert(0); /* invalid var kind to store */
|
|
|
|
break;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2003-11-20 03:59:18 +08:00
|
|
|
freeexp(fs, ex);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
2000-04-08 03:35:20 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
|
2001-07-17 22:30:44 +08:00
|
|
|
int func;
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_exp2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2001-07-17 22:30:44 +08:00
|
|
|
func = fs->freereg;
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_reserveregs(fs, 2);
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
|
2001-07-17 22:30:44 +08:00
|
|
|
freeexp(fs, key);
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = func;
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VNONRELOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void invertjump (FuncState *fs, expdesc *e) {
|
2005-10-10 04:05:48 +08:00
|
|
|
Instruction *pc = getjumpcontrol(fs, e->u.s.info);
|
2005-08-30 04:49:21 +08:00
|
|
|
lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
|
|
|
|
GET_OPCODE(*pc) != OP_TEST);
|
2002-08-21 04:03:05 +08:00
|
|
|
SETARG_A(*pc, !(GETARG_A(*pc)));
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-06 23:51:41 +08:00
|
|
|
static int jumponcond (FuncState *fs, expdesc *e, int cond) {
|
2001-06-06 02:17:01 +08: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 04:49:21 +08:00
|
|
|
return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
/* else go through */
|
|
|
|
}
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2005-10-10 04:05:48 +08:00
|
|
|
return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_goiftrue (FuncState *fs, expdesc *e) {
|
|
|
|
int pc; /* pc of last jump */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
2005-10-10 04:05:48 +08:00
|
|
|
case VK: case VKNUM: case VTRUE: {
|
2001-06-06 02:17:01 +08:00
|
|
|
pc = NO_JUMP; /* always true; do nothing */
|
2000-04-15 01:47:24 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VFALSE: {
|
2002-05-11 03:22:11 +08:00
|
|
|
pc = luaK_jump(fs); /* always jump */
|
2000-06-05 22:56:18 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VJMP: {
|
|
|
|
invertjump(fs, e);
|
2005-10-10 04:05:48 +08:00
|
|
|
pc = e->u.s.info;
|
2000-04-15 01:47:24 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
default: {
|
2002-05-06 23:51:41 +08:00
|
|
|
pc = jumponcond(fs, e, 0);
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
|
2005-08-18 02:32:09 +08:00
|
|
|
luaK_patchtohere(fs, e->t);
|
|
|
|
e->t = NO_JUMP;
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-09 03:44:31 +08:00
|
|
|
static void luaK_goiffalse (FuncState *fs, expdesc *e) {
|
2001-06-06 02:17:01 +08:00
|
|
|
int pc; /* pc of last jump */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
2001-12-12 06:48:44 +08:00
|
|
|
case VNIL: case VFALSE: {
|
2001-06-06 02:17:01 +08:00
|
|
|
pc = NO_JUMP; /* always false; do nothing */
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VTRUE: {
|
2002-05-11 03:22:11 +08:00
|
|
|
pc = luaK_jump(fs); /* always jump */
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-12-12 06:48:44 +08:00
|
|
|
case VJMP: {
|
2005-10-10 04:05:48 +08:00
|
|
|
pc = e->u.s.info;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
default: {
|
2002-05-06 23:51:41 +08:00
|
|
|
pc = jumponcond(fs, e, 1);
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
|
2005-08-18 02:32:09 +08:00
|
|
|
luaK_patchtohere(fs, e->f);
|
|
|
|
e->f = NO_JUMP;
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codenot (FuncState *fs, expdesc *e) {
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e);
|
2001-06-06 02:17:01 +08:00
|
|
|
switch (e->k) {
|
2001-12-12 06:48:44 +08:00
|
|
|
case VNIL: case VFALSE: {
|
|
|
|
e->k = VTRUE;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2005-10-10 04:05:48 +08:00
|
|
|
case VK: case VKNUM: case VTRUE: {
|
2001-12-12 06:48:44 +08:00
|
|
|
e->k = VFALSE;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VJMP: {
|
|
|
|
invertjump(fs, e);
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
case VRELOCABLE:
|
|
|
|
case VNONRELOC: {
|
|
|
|
discharge2anyreg(fs, e);
|
|
|
|
freeexp(fs, e);
|
2005-10-10 04:05:48 +08:00
|
|
|
e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
|
2001-06-06 02:17:01 +08:00
|
|
|
e->k = VRELOCABLE;
|
2000-04-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
default: {
|
|
|
|
lua_assert(0); /* cannot happen */
|
2000-06-17 01:51:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
/* interchange true and false lists */
|
|
|
|
{ int temp = e->f; e->f = e->t; e->t = temp; }
|
2005-08-30 04:49:21 +08:00
|
|
|
removevalues(fs, e->f);
|
|
|
|
removevalues(fs, e->t);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
2005-10-10 04:05:48 +08:00
|
|
|
t->u.s.aux = luaK_exp2RK(fs, k);
|
2001-06-06 02:17:01 +08:00
|
|
|
t->k = VINDEXED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-25 01:37:52 +08:00
|
|
|
static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
|
2005-10-10 04:05:48 +08:00
|
|
|
lua_Number v1, v2, r;
|
2005-10-25 01:37:52 +08:00
|
|
|
if (!isnumeral(e1) || !isnumeral(e2)) return 0;
|
2005-10-10 04:05:48 +08:00
|
|
|
v1 = e1->u.nval;
|
|
|
|
v2 = e2->u.nval;
|
|
|
|
switch (op) {
|
2005-10-25 01:37:52 +08:00
|
|
|
case OP_ADD: r = luai_numadd(v1, v2); break;
|
|
|
|
case OP_SUB: r = luai_numsub(v1, v2); break;
|
|
|
|
case OP_MUL: r = luai_nummul(v1, v2); break;
|
2005-10-10 04:05:48 +08:00
|
|
|
case OP_DIV:
|
|
|
|
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
|
2005-10-25 01:37:52 +08:00
|
|
|
r = luai_numdiv(v1, v2); break;
|
2005-11-16 19:55:27 +08:00
|
|
|
case OP_MOD:
|
|
|
|
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
|
|
|
|
r = luai_nummod(v1, v2); break;
|
2005-10-25 01:37:52 +08:00
|
|
|
case OP_POW: r = luai_numpow(v1, v2); break;
|
|
|
|
case OP_UNM: r = luai_numunm(v1); break;
|
2005-10-10 04:05:48 +08:00
|
|
|
case OP_LEN: return 0; /* no constant folding for 'len' */
|
|
|
|
default: lua_assert(0); r = 0; break;
|
|
|
|
}
|
2005-11-25 21:29:32 +08:00
|
|
|
if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
|
2005-10-10 04:05:48 +08:00
|
|
|
e1->u.nval = r;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
|
2005-10-25 01:37:52 +08:00
|
|
|
if (constfolding(op, e1, e2))
|
2005-10-10 04:05:48 +08:00
|
|
|
return;
|
|
|
|
else {
|
|
|
|
int o1 = luaK_exp2RK(fs, e1);
|
|
|
|
int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
|
|
|
|
freeexp(fs, e2);
|
|
|
|
freeexp(fs, e1);
|
|
|
|
e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
|
|
|
|
e1->k = VRELOCABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
e1->u.s.info = condjump(fs, op, cond, o1, o2);
|
|
|
|
e1->k = VJMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
|
2005-10-10 04:05:48 +08:00
|
|
|
expdesc e2;
|
|
|
|
e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
|
2005-03-17 00:59:21 +08:00
|
|
|
switch (op) {
|
|
|
|
case OPR_MINUS: {
|
2005-10-10 04:05:48 +08:00
|
|
|
if (e->k == VK)
|
|
|
|
luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
|
|
|
|
codearith(fs, OP_UNM, e, &e2);
|
2005-03-17 00:59:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-10-10 04:05:48 +08:00
|
|
|
case OPR_NOT: codenot(fs, e); break;
|
2005-05-20 23:53:42 +08:00
|
|
|
case OPR_LEN: {
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_exp2anyreg(fs, e); /* cannot operate on constants */
|
|
|
|
codearith(fs, OP_LEN, e, &e2);
|
2005-03-17 00:59:21 +08:00
|
|
|
break;
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
2005-03-17 00:59:21 +08:00
|
|
|
default: lua_assert(0);
|
2001-06-06 02:17:01 +08: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-08 03:35:20 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
|
|
|
default: {
|
2005-10-25 01:37:52 +08:00
|
|
|
if (!isnumeral(v)) luaK_exp2RK(fs, v);
|
2000-05-23 02:44:46 +08:00
|
|
|
break;
|
2000-06-17 01:51:40 +08:00
|
|
|
}
|
2000-04-08 03:35:20 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
|
|
|
|
switch (op) {
|
|
|
|
case OPR_AND: {
|
|
|
|
lua_assert(e1->t == NO_JUMP); /* list must be closed */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e2);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_concat(fs, &e1->f, e2->f);
|
2005-10-10 04:05:48 +08:00
|
|
|
e1->k = e2->k; e1->u.s.info = e2->u.s.info;
|
|
|
|
e1->u.s.aux = e2->u.s.aux; e1->t = e2->t;
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_OR: {
|
|
|
|
lua_assert(e1->f == NO_JUMP); /* list must be closed */
|
2001-06-12 22:36:48 +08:00
|
|
|
luaK_dischargevars(fs, e2);
|
2001-06-06 02:17:01 +08:00
|
|
|
luaK_concat(fs, &e1->t, e2->t);
|
2005-10-10 04:05:48 +08:00
|
|
|
e1->k = e2->k; e1->u.s.info = e2->u.s.info;
|
|
|
|
e1->u.s.aux = e2->u.s.aux; e1->f = e2->f;
|
2001-06-06 02:17:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OPR_CONCAT: {
|
|
|
|
luaK_exp2val(fs, e2);
|
|
|
|
if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
|
2005-10-10 04:05:48 +08:00
|
|
|
lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
|
2001-06-06 02:17:01 +08:00
|
|
|
freeexp(fs, e1);
|
2005-10-10 04:05:48 +08:00
|
|
|
SETARG_B(getcode(fs, e2), e1->u.s.info);
|
|
|
|
e1->k = e2->k; e1->u.s.info = e2->u.s.info;
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-10-10 04:05:48 +08:00
|
|
|
luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
|
|
|
|
codearith(fs, OP_CONCAT, e1, e2);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-10-10 04:05:48 +08:00
|
|
|
case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
|
|
|
|
case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
|
|
|
|
case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
|
|
|
|
case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
|
|
|
|
case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
|
|
|
|
case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
|
|
|
|
case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
|
|
|
|
case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
|
|
|
|
case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
|
|
|
|
case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
|
|
|
|
case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
|
|
|
|
case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
|
|
|
|
default: lua_assert(0);
|
2000-05-23 02:44:46 +08:00
|
|
|
}
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-13 21:07:48 +08:00
|
|
|
void luaK_fixline (FuncState *fs, int line) {
|
|
|
|
fs->f->lineinfo[fs->pc - 1] = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-09 03:44:31 +08:00
|
|
|
static int luaK_code (FuncState *fs, Instruction i, int line) {
|
2002-03-26 01:47:14 +08:00
|
|
|
Proto *f = fs->f;
|
2004-12-04 04:50:25 +08:00
|
|
|
dischargejpc(fs); /* `pc' will change */
|
2000-06-22 02:13:56 +08:00
|
|
|
/* put new instruction in code array */
|
2000-12-28 20:55:41 +08:00
|
|
|
luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
|
2001-11-29 04:13:13 +08:00
|
|
|
MAX_INT, "code size overflow");
|
2000-12-28 20:55:41 +08:00
|
|
|
f->code[fs->pc] = i;
|
2002-10-17 04:40:58 +08:00
|
|
|
/* save corresponding line information */
|
|
|
|
luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
|
|
|
|
MAX_INT, "code size overflow");
|
2002-05-11 01:02:32 +08:00
|
|
|
f->lineinfo[fs->pc] = line;
|
2000-04-08 03:35:20 +08:00
|
|
|
return fs->pc++;
|
|
|
|
}
|
|
|
|
|
2000-05-23 02:44:46 +08:00
|
|
|
|
2001-06-06 02:17:01 +08:00
|
|
|
int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
|
|
|
|
lua_assert(getOpMode(o) == iABC);
|
2003-05-14 20:09:12 +08:00
|
|
|
lua_assert(getBMode(o) != OpArgN || b == 0);
|
|
|
|
lua_assert(getCMode(o) != OpArgN || c == 0);
|
2002-05-11 01:02:32 +08:00
|
|
|
return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-25 04:07:46 +08:00
|
|
|
int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
|
|
|
|
lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
|
2003-05-14 20:09:12 +08:00
|
|
|
lua_assert(getCMode(o) == OpArgN);
|
2002-05-11 01:02:32 +08:00
|
|
|
return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
|
2001-06-06 02:17:01 +08:00
|
|
|
}
|
2000-06-29 04:21:06 +08:00
|
|
|
|
2004-10-05 03:01:53 +08: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);
|
|
|
|
else {
|
|
|
|
luaK_codeABC(fs, OP_SETLIST, base, b, 0);
|
|
|
|
luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
|
|
|
|
}
|
|
|
|
fs->freereg = base + 1; /* free registers with list values */
|
|
|
|
}
|
|
|
|
|