2000-02-22 21:31:19 +08:00
|
|
|
/*
|
2000-03-21 03:15:37 +08:00
|
|
|
** $Id: lcode.c,v 1.15 2000/03/17 14:46:04 roberto Exp roberto $
|
2000-02-22 21:31:19 +08:00
|
|
|
** Code generator for Lua
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-03-09 21:57:37 +08:00
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
2000-02-22 21:31:19 +08:00
|
|
|
#include "lcode.h"
|
2000-03-03 20:33:59 +08:00
|
|
|
#include "ldo.h"
|
2000-02-22 21:31:19 +08:00
|
|
|
#include "llex.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lparser.h"
|
2000-03-03 20:33:59 +08:00
|
|
|
#include "lstring.h"
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_error (LexState *ls, const char *msg) {
|
|
|
|
luaX_error(ls, msg, ls->token);
|
|
|
|
}
|
2000-02-22 21:31:19 +08:00
|
|
|
|
|
|
|
|
2000-03-17 21:09:46 +08:00
|
|
|
int luaK_code (FuncState *fs, Instruction i, int delta) {
|
|
|
|
luaK_deltastack(fs, delta);
|
|
|
|
luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
|
|
|
|
fs->f->code[fs->pc] = i;
|
|
|
|
return fs->pc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int luaK_0(FuncState *fs, OpCode o, int d) {
|
|
|
|
return luaK_code(fs, CREATE_0(o), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
int luaK_U(FuncState *fs, OpCode o, int u, int d) {
|
|
|
|
return luaK_code(fs, CREATE_U(o,u), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
int luaK_S(FuncState *fs, OpCode o, int s, int d) {
|
|
|
|
return luaK_code(fs, CREATE_S(o,s), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
int luaK_AB(FuncState *fs, OpCode o, int a, int b, int d) {
|
|
|
|
return luaK_code(fs, CREATE_AB(o,a,b), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-05 04:18:15 +08:00
|
|
|
/*
|
2000-03-17 21:09:46 +08:00
|
|
|
** Returns the the previous instruction, for optimizations.
|
2000-03-05 04:18:15 +08:00
|
|
|
** If there is a jump target between this and the current instruction,
|
2000-03-17 21:09:46 +08:00
|
|
|
** returns a dummy instruction to avoid wrong optimizations.
|
2000-03-05 04:18:15 +08:00
|
|
|
*/
|
2000-03-17 21:09:46 +08:00
|
|
|
static Instruction previous_instruction (FuncState *fs) {
|
2000-03-05 04:18:15 +08:00
|
|
|
if (fs->pc > fs->lasttarget) /* no jumps to current position? */
|
2000-03-17 21:09:46 +08:00
|
|
|
return fs->f->code[fs->pc-1]; /* returns previous instruction */
|
|
|
|
else
|
|
|
|
return CREATE_0(OP_END); /* no optimizations after an `END' */
|
2000-02-22 21:31:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-17 21:09:46 +08:00
|
|
|
static Instruction prepare (FuncState *fs, Instruction i, int delta) {
|
|
|
|
Instruction previous = previous_instruction(fs);
|
|
|
|
luaK_code(fs, i, delta);
|
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setprevious (FuncState *fs, Instruction i) {
|
|
|
|
fs->pc--; /* remove last instruction */
|
|
|
|
fs->f->code[fs->pc-1] = i; /* change previous instruction */
|
2000-02-22 21:31:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_minus (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_0(OP_MINUS), 0);
|
|
|
|
switch(GET_OPCODE(previous)) {
|
|
|
|
case OP_PUSHINT: SETARG_S(previous, -GETARG_S(previous)); break;
|
|
|
|
case OP_PUSHNUM: SET_OPCODE(previous, OP_PUSHNEGNUM); break;
|
|
|
|
case OP_PUSHNEGNUM: SET_OPCODE(previous, OP_PUSHNUM); break;
|
|
|
|
default: return;
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-03-17 21:09:46 +08:00
|
|
|
setprevious(fs, previous);
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-02-22 21:31:19 +08:00
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_gettable (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_0(OP_GETTABLE), -1);
|
|
|
|
switch(GET_OPCODE(previous)) {
|
|
|
|
case OP_PUSHSTRING: SET_OPCODE(previous, OP_GETDOTTED); break;
|
|
|
|
default: return;
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-03-17 21:09:46 +08:00
|
|
|
setprevious(fs, previous);
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-02-22 21:31:19 +08:00
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_add (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_0(OP_ADD), -1);
|
|
|
|
switch(GET_OPCODE(previous)) {
|
|
|
|
case OP_PUSHINT: SET_OPCODE(previous, OP_ADDI); break;
|
|
|
|
default: return;
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-03-17 21:09:46 +08:00
|
|
|
setprevious(fs, previous);
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
|
|
|
|
2000-02-22 21:31:19 +08:00
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_sub (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_0(OP_SUB), -1);
|
|
|
|
switch(GET_OPCODE(previous)) {
|
2000-03-11 02:37:44 +08:00
|
|
|
case OP_PUSHINT:
|
2000-03-17 21:09:46 +08:00
|
|
|
SET_OPCODE(previous, OP_ADDI);
|
|
|
|
SETARG_S(previous, -GETARG_S(previous));
|
2000-02-22 21:31:19 +08:00
|
|
|
break;
|
2000-03-17 21:09:46 +08:00
|
|
|
default: return;
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-03-17 21:09:46 +08:00
|
|
|
setprevious(fs, previous);
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
2000-02-22 21:31:19 +08:00
|
|
|
|
2000-03-04 02:53:17 +08:00
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_conc (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_U(OP_CONC, 2), -1);
|
|
|
|
switch(GET_OPCODE(previous)) {
|
|
|
|
case OP_CONC: SETARG_U(previous, GETARG_U(previous)+1); break;
|
|
|
|
default: return;
|
2000-03-09 08:19:22 +08:00
|
|
|
}
|
2000-03-17 21:09:46 +08:00
|
|
|
setprevious(fs, previous);
|
2000-03-09 08:19:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_eq (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_S(OP_IFEQJMP, 0), -2);
|
|
|
|
if (previous == CREATE_U(OP_PUSHNIL, 1)) {
|
|
|
|
setprevious(fs, CREATE_0(OP_NOT));
|
|
|
|
luaK_deltastack(fs, 1); /* undo delta from `prepare' */
|
2000-03-10 22:38:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_neq (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_S(OP_IFNEQJMP, 0), -2);
|
|
|
|
if (previous == CREATE_U(OP_PUSHNIL, 1)) {
|
2000-03-21 03:15:37 +08:00
|
|
|
setprevious(fs, CREATE_S(OP_IFTJMP, 0));
|
2000-03-10 22:38:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_retcode (FuncState *fs, int nlocals, int nexps) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_U(OP_RETURN, nlocals), 0);
|
|
|
|
if (nexps > 0 && GET_OPCODE(previous) == OP_CALL) {
|
|
|
|
LUA_ASSERT(fs->L, GETARG_B(previous) == MULT_RET, "call should be open");
|
|
|
|
SET_OPCODE(previous, OP_TAILCALL);
|
|
|
|
SETARG_B(previous, nlocals);
|
|
|
|
setprevious(fs, previous);
|
2000-02-22 21:31:19 +08:00
|
|
|
}
|
2000-03-04 02:53:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_pushnil (FuncState *fs, int n) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_U(OP_PUSHNIL, n), n);
|
|
|
|
switch(GET_OPCODE(previous)) {
|
|
|
|
case OP_PUSHNIL: SETARG_U(previous, GETARG_U(previous)+n); break;
|
|
|
|
default: return;
|
2000-03-05 04:18:15 +08:00
|
|
|
}
|
2000-03-17 21:09:46 +08:00
|
|
|
setprevious(fs, previous);
|
2000-02-22 21:31:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_fixjump (FuncState *fs, int pc, int dest) {
|
2000-02-22 21:31:19 +08:00
|
|
|
Instruction *jmp = &fs->f->code[pc];
|
2000-03-17 02:03:09 +08:00
|
|
|
if (dest == NO_JUMP)
|
2000-03-14 04:37:16 +08:00
|
|
|
SETARG_S(*jmp, 0); /* absolute value to represent end of list */
|
2000-03-17 02:03:09 +08:00
|
|
|
else { /* jump is relative to position following jump instruction */
|
|
|
|
int offset = dest-(pc+1);
|
|
|
|
if (offset < -MAXARG_S || offset > MAXARG_S)
|
|
|
|
luaK_error(fs->ls, "control structure too long");
|
|
|
|
SETARG_S(*jmp, offset);
|
|
|
|
}
|
2000-03-14 04:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int luaK_getjump (FuncState *fs, int pc) {
|
|
|
|
int offset = GETARG_S(fs->f->code[pc]);
|
|
|
|
if (offset == 0)
|
|
|
|
return NO_JUMP; /* end of list */
|
|
|
|
else
|
2000-03-17 02:03:09 +08:00
|
|
|
return (pc+1)+offset; /* turn offset into absolute position */
|
2000-02-22 21:31:19 +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) {
|
2000-03-05 04:18:15 +08:00
|
|
|
fs->lasttarget = fs->pc;
|
|
|
|
return fs->pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_deltastack (FuncState *fs, int delta) {
|
2000-03-03 20:33:59 +08:00
|
|
|
fs->stacksize += delta;
|
|
|
|
if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
|
|
|
|
if (fs->stacksize > MAXSTACK)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_error(fs->ls, "function or expression too complex");
|
2000-03-03 20:33:59 +08:00
|
|
|
fs->f->maxstacksize = fs->stacksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-04 02:53:17 +08:00
|
|
|
void luaK_kstr (LexState *ls, int c) {
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(ls->fs, OP_PUSHSTRING, c, 1);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LOOKBACKNUMS
|
|
|
|
#define LOOKBACKNUMS 20 /* arbitrary limit */
|
|
|
|
#endif
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static int real_constant (FuncState *fs, Number r) {
|
2000-03-03 22:58:26 +08:00
|
|
|
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
|
2000-03-14 04:37:16 +08:00
|
|
|
Proto *f = fs->f;
|
2000-03-03 20:33:59 +08:00
|
|
|
int c = f->nknum;
|
|
|
|
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
|
|
|
|
while (--c >= lim)
|
|
|
|
if (f->knum[c] == r) return c;
|
|
|
|
/* not found; create a new entry */
|
2000-03-14 04:37:16 +08:00
|
|
|
luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
|
2000-03-03 20:33:59 +08:00
|
|
|
c = f->nknum++;
|
|
|
|
f->knum[c] = r;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_number (FuncState *fs, Number f) {
|
2000-03-11 02:37:44 +08:00
|
|
|
if (f <= (Number)MAXARG_S && (int)f == f)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_S(fs, OP_PUSHINT, (int)f, 1); /* f has a short integer value */
|
2000-03-03 20:33:59 +08:00
|
|
|
else
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(fs, OP_PUSHNUM, real_constant(fs, f), 1);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_adjuststack (FuncState *fs, int n) {
|
2000-03-03 20:33:59 +08:00
|
|
|
if (n > 0)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(fs, OP_POP, n, -n);
|
2000-03-03 20:33:59 +08:00
|
|
|
else if (n < 0)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_pushnil(fs, -n);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
int luaK_lastisopen (FuncState *fs) {
|
2000-03-17 21:09:46 +08:00
|
|
|
/* check whether last instruction is an open function call */
|
|
|
|
Instruction i = previous_instruction(fs);
|
|
|
|
if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
|
2000-03-05 04:18:15 +08:00
|
|
|
return 1;
|
|
|
|
else return 0;
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_setcallreturns (FuncState *fs, int nresults) {
|
2000-03-17 21:09:46 +08:00
|
|
|
if (luaK_lastisopen(fs)) { /* expression is an open function call? */
|
|
|
|
SETARG_B(fs->f->code[fs->pc-1], nresults); /* set number of results */
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_deltastack(fs, nresults); /* push results */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void assertglobal (FuncState *fs, int index) {
|
|
|
|
luaS_assertglobal(fs->L, fs->f->kstr[index]);
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static int discharge (FuncState *fs, expdesc *var) {
|
2000-03-03 20:33:59 +08:00
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL:
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(fs, OP_PUSHLOCAL, var->u.index, 1);
|
2000-03-03 20:33:59 +08:00
|
|
|
break;
|
|
|
|
case VGLOBAL:
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(fs, OP_GETGLOBAL, var->u.index, 1);
|
|
|
|
assertglobal(fs, var->u.index); /* make sure that there is a global */
|
2000-03-03 20:33:59 +08:00
|
|
|
break;
|
|
|
|
case VINDEXED:
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_gettable(fs);
|
2000-03-03 20:33:59 +08:00
|
|
|
break;
|
|
|
|
case VEXP:
|
2000-03-09 21:57:37 +08:00
|
|
|
return 0; /* nothing to do */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
var->k = VEXP;
|
2000-03-14 04:37:16 +08:00
|
|
|
var->u.l.t = var->u.l.f = NO_JUMP;
|
2000-03-09 21:57:37 +08:00
|
|
|
return 1;
|
2000-03-05 04:18:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void discharge1 (FuncState *fs, expdesc *var) {
|
|
|
|
discharge(fs, var);
|
2000-03-09 21:57:37 +08:00
|
|
|
/* if it has jumps it is already discharged */
|
2000-03-14 04:37:16 +08:00
|
|
|
if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
|
|
|
|
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
2000-03-09 21:57:37 +08:00
|
|
|
|
2000-03-03 20:33:59 +08:00
|
|
|
|
|
|
|
void luaK_storevar (LexState *ls, const expdesc *var) {
|
2000-03-14 04:37:16 +08:00
|
|
|
FuncState *fs = ls->fs;
|
2000-03-03 20:33:59 +08:00
|
|
|
switch (var->k) {
|
|
|
|
case VLOCAL:
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(fs, OP_SETLOCAL, var->u.index, -1);
|
2000-03-03 20:33:59 +08:00
|
|
|
break;
|
|
|
|
case VGLOBAL:
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_U(fs, OP_SETGLOBAL, var->u.index, -1);
|
|
|
|
assertglobal(fs, var->u.index); /* make sure that there is a global */
|
2000-03-03 20:33:59 +08:00
|
|
|
break;
|
|
|
|
case VINDEXED:
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_0(fs, OP_SETTABLEPOP, -3);
|
2000-03-03 20:33:59 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LUA_INTERNALERROR(ls->L, "invalid var kind to store");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-09 21:57:37 +08:00
|
|
|
static OpCode invertjump (OpCode op) {
|
|
|
|
switch (op) {
|
2000-03-11 02:37:44 +08:00
|
|
|
case OP_IFNEQJMP: return OP_IFEQJMP;
|
|
|
|
case OP_IFEQJMP: return OP_IFNEQJMP;
|
|
|
|
case OP_IFLTJMP: return OP_IFGEJMP;
|
|
|
|
case OP_IFLEJMP: return OP_IFGTJMP;
|
|
|
|
case OP_IFGTJMP: return OP_IFLEJMP;
|
|
|
|
case OP_IFGEJMP: return OP_IFLTJMP;
|
|
|
|
case OP_IFTJMP: case OP_ONTJMP: return OP_IFFJMP;
|
|
|
|
case OP_IFFJMP: case OP_ONFJMP: return OP_IFTJMP;
|
2000-03-09 21:57:37 +08:00
|
|
|
default:
|
|
|
|
LUA_INTERNALERROR(NULL, "invalid jump instruction");
|
2000-03-11 02:37:44 +08:00
|
|
|
return OP_END; /* to avoid warnings */
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_jump (FuncState *fs, OpCode jump) {
|
2000-03-17 21:09:46 +08:00
|
|
|
Instruction previous = prepare(fs, CREATE_S(jump, 0), -1);
|
2000-03-17 22:46:04 +08:00
|
|
|
switch (GET_OPCODE(previous)) {
|
|
|
|
case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break;
|
|
|
|
case OP_PUSHINT:
|
|
|
|
if (jump == OP_IFTJMP) {
|
|
|
|
previous = CREATE_S(OP_JMP, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else return; /* do not set previous */
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
setprevious(fs, previous);
|
2000-03-10 22:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-09 21:57:37 +08:00
|
|
|
static void insert_last (FuncState *fs, int *list) {
|
2000-03-10 22:38:10 +08:00
|
|
|
int first = *list;
|
|
|
|
*list = fs->pc-1; /* insert last instruction in the list */
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_fixjump(fs, *list, first);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void luaK_patchlistaux (FuncState *fs, int list, int target,
|
2000-03-09 21:57:37 +08:00
|
|
|
OpCode special, int special_target) {
|
2000-03-14 04:37:16 +08:00
|
|
|
Instruction *code = fs->f->code;
|
|
|
|
while (list != NO_JUMP) {
|
|
|
|
int next = luaK_getjump(fs, list);
|
|
|
|
Instruction *i = &code[list];
|
|
|
|
OpCode op = GET_OPCODE(*i);
|
|
|
|
if (op == special) /* this `op' already has a value */
|
2000-03-17 02:03:09 +08:00
|
|
|
luaK_fixjump(fs, list, special_target);
|
2000-03-14 04:37:16 +08:00
|
|
|
else {
|
2000-03-17 02:03:09 +08:00
|
|
|
luaK_fixjump(fs, list, target); /* do the patch */
|
2000-03-14 04:37:16 +08:00
|
|
|
if (op == OP_ONTJMP) /* remove eventual values */
|
|
|
|
SET_OPCODE(*i, OP_IFTJMP);
|
|
|
|
else if (op == OP_ONFJMP)
|
|
|
|
SET_OPCODE(*i, OP_IFFJMP);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2000-03-14 04:37:16 +08:00
|
|
|
list = next;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
|
|
|
luaK_patchlistaux(fs, list, target, OP_END, 0);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-10 22:38:10 +08:00
|
|
|
static int need_value (FuncState *fs, int list, OpCode hasvalue) {
|
2000-03-14 04:37:16 +08:00
|
|
|
/* check whether list has a jump without a value */
|
|
|
|
for (; list != NO_JUMP; list = luaK_getjump(fs, list))
|
|
|
|
if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
|
|
|
|
return 0; /* not found */
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
static void concatlists (FuncState *fs, int *l1, int l2) {
|
|
|
|
if (*l1 == NO_JUMP)
|
2000-03-09 21:57:37 +08:00
|
|
|
*l1 = l2;
|
2000-03-14 04:37:16 +08:00
|
|
|
else {
|
2000-03-09 21:57:37 +08:00
|
|
|
int list = *l1;
|
|
|
|
for (;;) { /* traverse `l1' */
|
2000-03-14 04:37:16 +08:00
|
|
|
int next = luaK_getjump(fs, list);
|
|
|
|
if (next == NO_JUMP) { /* end of list? */
|
|
|
|
luaK_fixjump(fs, list, l2);
|
2000-03-09 21:57:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2000-03-14 04:37:16 +08:00
|
|
|
list = next;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
|
2000-03-09 21:57:37 +08:00
|
|
|
Instruction *previous;
|
2000-03-14 04:37:16 +08:00
|
|
|
discharge1(fs, v);
|
2000-03-09 21:57:37 +08:00
|
|
|
previous = &fs->f->code[fs->pc-1];
|
2000-03-21 03:15:37 +08:00
|
|
|
LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
|
2000-03-09 21:57:37 +08:00
|
|
|
if (ISJUMP(GET_OPCODE(*previous)))
|
|
|
|
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
|
|
|
else {
|
2000-03-11 02:37:44 +08:00
|
|
|
OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP;
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_jump(fs, jump);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
insert_last(fs, &v->u.l.f);
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_patchlist(fs, v->u.l.t, luaK_getlabel(fs));
|
|
|
|
v->u.l.t = NO_JUMP;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-14 04:37:16 +08:00
|
|
|
void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
|
2000-03-10 22:38:10 +08:00
|
|
|
Instruction previous;
|
2000-03-14 04:37:16 +08:00
|
|
|
discharge1(fs, v);
|
2000-03-10 22:38:10 +08:00
|
|
|
previous = fs->f->code[fs->pc-1];
|
2000-03-21 03:15:37 +08:00
|
|
|
LUA_ASSERT(L, GET_OPCODE(previous) != OP_SETLINE, "bad place to set line");
|
2000-03-10 22:38:10 +08:00
|
|
|
if (!ISJUMP(GET_OPCODE(previous))) {
|
2000-03-11 02:37:44 +08:00
|
|
|
OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP;
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_jump(fs, jump);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
insert_last(fs, &v->u.l.t);
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_patchlist(fs, v->u.l.f, luaK_getlabel(fs));
|
|
|
|
v->u.l.f = NO_JUMP;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
2000-03-14 04:37:16 +08:00
|
|
|
FuncState *fs = ls->fs;
|
|
|
|
if (discharge(fs, v)) return;
|
2000-03-09 21:57:37 +08:00
|
|
|
else { /* is an expression */
|
2000-03-10 22:38:10 +08:00
|
|
|
OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
|
2000-03-21 03:15:37 +08:00
|
|
|
LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
|
2000-03-14 04:37:16 +08:00
|
|
|
if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
|
2000-03-09 21:57:37 +08:00
|
|
|
/* it is an expression without jumps */
|
|
|
|
if (onlyone && v->k == VEXP)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
2000-03-09 21:57:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else { /* expression has jumps... */
|
|
|
|
int p_nil = 0; /* position of an eventual PUSHNIL */
|
|
|
|
int p_1 = 0; /* position of an eventual PUSHINT */
|
|
|
|
int final; /* position after whole expression */
|
2000-03-10 22:38:10 +08:00
|
|
|
if (ISJUMP(previous)) {
|
|
|
|
insert_last(fs, &v->u.l.t); /* put `previous' in true list */
|
2000-03-14 04:37:16 +08:00
|
|
|
p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
|
|
|
|
p_1 = luaK_S(fs, OP_PUSHINT, 1, 1);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
else { /* still may need a PUSHNIL or a PUSHINT */
|
2000-03-11 02:37:44 +08:00
|
|
|
int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP);
|
|
|
|
int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP);
|
2000-03-09 21:57:37 +08:00
|
|
|
if (need_nil && need_1) {
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */
|
|
|
|
p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
|
|
|
|
p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
else if (need_nil || need_1) {
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_S(fs, OP_JMP, 1, 0); /* skip one push */
|
2000-03-09 21:57:37 +08:00
|
|
|
if (need_nil)
|
2000-03-14 04:37:16 +08:00
|
|
|
p_nil = luaK_U(fs, OP_PUSHNIL, 1, 0);
|
2000-03-09 21:57:37 +08:00
|
|
|
else /* need_1 */
|
2000-03-14 04:37:16 +08:00
|
|
|
p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
2000-03-14 04:37:16 +08:00
|
|
|
final = luaK_getlabel(fs);
|
|
|
|
luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final);
|
|
|
|
luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final);
|
|
|
|
v->u.l.f = v->u.l.t = NO_JUMP;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-03 20:33:59 +08:00
|
|
|
void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
2000-03-14 04:37:16 +08:00
|
|
|
FuncState *fs = ls->fs;
|
2000-03-09 21:57:37 +08:00
|
|
|
if (op == '-') {
|
|
|
|
luaK_tostack(ls, v, 1);
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_minus(fs);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
else { /* op == NOT */
|
|
|
|
Instruction *previous;
|
2000-03-14 04:37:16 +08:00
|
|
|
discharge1(fs, v);
|
2000-03-09 21:57:37 +08:00
|
|
|
previous = &fs->f->code[fs->pc-1];
|
|
|
|
if (ISJUMP(GET_OPCODE(*previous)))
|
|
|
|
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
|
|
|
else
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_0(fs, OP_NOT, 0);
|
2000-03-09 21:57:37 +08:00
|
|
|
/* interchange true and false lists */
|
|
|
|
{ int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
|
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-05 04:18:15 +08:00
|
|
|
void luaK_infix (LexState *ls, int op, expdesc *v) {
|
2000-03-14 04:37:16 +08:00
|
|
|
FuncState *fs = ls->fs;
|
2000-03-11 02:37:44 +08:00
|
|
|
if (op == TK_AND)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_goiftrue(fs, v, 1);
|
2000-03-11 02:37:44 +08:00
|
|
|
else if (op == TK_OR)
|
2000-03-14 04:37:16 +08:00
|
|
|
luaK_goiffalse(fs, v, 1);
|
2000-03-09 21:57:37 +08:00
|
|
|
else
|
2000-03-10 22:38:10 +08:00
|
|
|
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
|
2000-03-14 04:37:16 +08:00
|
|
|
FuncState *fs = ls->fs;
|
2000-03-11 02:37:44 +08:00
|
|
|
if (op == TK_AND) {
|
2000-03-14 04:37:16 +08:00
|
|
|
LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
|
|
|
|
discharge1(fs, v2);
|
2000-03-09 21:57:37 +08:00
|
|
|
v1->u.l.t = v2->u.l.t;
|
2000-03-14 04:37:16 +08:00
|
|
|
concatlists(fs, &v1->u.l.f, v2->u.l.f);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2000-03-11 02:37:44 +08:00
|
|
|
else if (op == TK_OR) {
|
2000-03-14 04:37:16 +08:00
|
|
|
LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
|
|
|
|
discharge1(fs, v2);
|
2000-03-09 21:57:37 +08:00
|
|
|
v1->u.l.f = v2->u.l.f;
|
2000-03-14 04:37:16 +08:00
|
|
|
concatlists(fs, &v1->u.l.t, v2->u.l.t);
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
|
|
|
else {
|
2000-03-10 22:38:10 +08:00
|
|
|
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
|
2000-03-09 21:57:37 +08:00
|
|
|
switch (op) {
|
2000-03-14 04:37:16 +08:00
|
|
|
case '+': luaK_add(fs); break;
|
|
|
|
case '-': luaK_sub(fs); break;
|
|
|
|
case '*': luaK_0(fs, OP_MULT, -1); break;
|
|
|
|
case '/': luaK_0(fs, OP_DIV, -1); break;
|
|
|
|
case '^': luaK_0(fs, OP_POW, -1); break;
|
|
|
|
case TK_CONC: luaK_conc(fs); break;
|
|
|
|
case TK_EQ: luaK_eq(fs); break;
|
|
|
|
case TK_NE: luaK_neq(fs); break;
|
|
|
|
case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break;
|
|
|
|
case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break;
|
|
|
|
case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break;
|
|
|
|
case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break;
|
2000-03-09 21:57:37 +08:00
|
|
|
}
|
2000-03-03 20:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|