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