mirror of
https://github.com/lua/lua
synced 2024-12-24 19:36:50 +03:00
details
This commit is contained in:
parent
73aa465a8e
commit
f0b697e01c
366
lcode.c
366
lcode.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcode.c,v 1.9 2000/03/10 14:38:10 roberto Exp roberto $
|
** $Id: lcode.c,v 1.10 2000/03/10 18:37:44 roberto Exp roberto $
|
||||||
** Code generator for Lua
|
** Code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -27,8 +27,7 @@ void luaK_error (LexState *ls, const char *msg) {
|
|||||||
** If there is a jump target between this and the current instruction,
|
** If there is a jump target between this and the current instruction,
|
||||||
** returns the address of a dummy instruction to avoid wrong optimizations.
|
** returns the address of a dummy instruction to avoid wrong optimizations.
|
||||||
*/
|
*/
|
||||||
static Instruction *previous_instruction (LexState *ls) {
|
static Instruction *previous_instruction (FuncState *fs) {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
if (fs->pc > fs->lasttarget) /* no jumps to current position? */
|
if (fs->pc > fs->lasttarget) /* no jumps to current position? */
|
||||||
return &fs->f->code[fs->pc-1]; /* returns previous instruction */
|
return &fs->f->code[fs->pc-1]; /* returns previous instruction */
|
||||||
else {
|
else {
|
||||||
@ -38,123 +37,134 @@ static Instruction *previous_instruction (LexState *ls) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaK_primitivecode (LexState *ls, Instruction i) {
|
int luaK_primitivecode (FuncState *fs, Instruction i) {
|
||||||
FuncState *fs = ls->fs;
|
luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S);
|
||||||
luaM_growvector(ls->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S);
|
|
||||||
fs->f->code[fs->pc] = i;
|
fs->f->code[fs->pc] = i;
|
||||||
return fs->pc++;
|
return fs->pc++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_minus (LexState *ls) {
|
static void luaK_minus (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
switch(GET_OPCODE(*previous)) {
|
switch(GET_OPCODE(*previous)) {
|
||||||
case OP_PUSHINT: SETARG_S(*previous, -GETARG_S(*previous)); return;
|
case OP_PUSHINT: SETARG_S(*previous, -GETARG_S(*previous)); return;
|
||||||
case OP_PUSHNUM: SET_OPCODE(*previous, OP_PUSHNEGNUM); return;
|
case OP_PUSHNUM: SET_OPCODE(*previous, OP_PUSHNEGNUM); return;
|
||||||
case OP_PUSHNEGNUM: SET_OPCODE(*previous, OP_PUSHNUM); return;
|
case OP_PUSHNEGNUM: SET_OPCODE(*previous, OP_PUSHNUM); return;
|
||||||
default: luaK_primitivecode(ls, CREATE_0(OP_MINUS));
|
default: luaK_primitivecode(fs, CREATE_0(OP_MINUS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_gettable (LexState *ls) {
|
static void luaK_gettable (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
luaK_deltastack(ls, -1);
|
luaK_deltastack(fs, -1);
|
||||||
switch(GET_OPCODE(*previous)) {
|
switch(GET_OPCODE(*previous)) {
|
||||||
case OP_PUSHSTRING: SET_OPCODE(*previous, OP_GETDOTTED); break;
|
case OP_PUSHSTRING: SET_OPCODE(*previous, OP_GETDOTTED); break;
|
||||||
default: luaK_primitivecode(ls, CREATE_0(OP_GETTABLE));
|
default: luaK_primitivecode(fs, CREATE_0(OP_GETTABLE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_add (LexState *ls) {
|
static void luaK_add (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
luaK_deltastack(ls, -1);
|
luaK_deltastack(fs, -1);
|
||||||
switch(GET_OPCODE(*previous)) {
|
switch(GET_OPCODE(*previous)) {
|
||||||
case OP_PUSHINT: SET_OPCODE(*previous, OP_ADDI); break;
|
case OP_PUSHINT: SET_OPCODE(*previous, OP_ADDI); break;
|
||||||
default: luaK_primitivecode(ls, CREATE_0(OP_ADD));
|
default: luaK_primitivecode(fs, CREATE_0(OP_ADD));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_sub (LexState *ls) {
|
static void luaK_sub (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
luaK_deltastack(ls, -1);
|
luaK_deltastack(fs, -1);
|
||||||
switch(GET_OPCODE(*previous)) {
|
switch(GET_OPCODE(*previous)) {
|
||||||
case OP_PUSHINT:
|
case OP_PUSHINT:
|
||||||
SET_OPCODE(*previous, OP_ADDI);
|
SET_OPCODE(*previous, OP_ADDI);
|
||||||
SETARG_S(*previous, -GETARG_S(*previous));
|
SETARG_S(*previous, -GETARG_S(*previous));
|
||||||
break;
|
break;
|
||||||
default: luaK_primitivecode(ls, CREATE_0(OP_SUB));
|
default: luaK_primitivecode(fs, CREATE_0(OP_SUB));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_conc (LexState *ls) {
|
static void luaK_conc (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
luaK_deltastack(ls, -1);
|
luaK_deltastack(fs, -1);
|
||||||
switch(GET_OPCODE(*previous)) {
|
switch(GET_OPCODE(*previous)) {
|
||||||
case OP_CONC: SETARG_U(*previous, GETARG_U(*previous)+1); break;
|
case OP_CONC: SETARG_U(*previous, GETARG_U(*previous)+1); break;
|
||||||
default: luaK_primitivecode(ls, CREATE_U(OP_CONC, 2));
|
default: luaK_primitivecode(fs, CREATE_U(OP_CONC, 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_eq (LexState *ls) {
|
static void luaK_eq (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
if (*previous == CREATE_U(OP_PUSHNIL, 1)) {
|
if (*previous == CREATE_U(OP_PUSHNIL, 1)) {
|
||||||
*previous = CREATE_0(OP_NOT);
|
*previous = CREATE_0(OP_NOT);
|
||||||
luaK_deltastack(ls, -1); /* undo effect of PUSHNIL */
|
luaK_deltastack(fs, -1); /* undo effect of PUSHNIL */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
luaK_S(ls, OP_IFEQJMP, 0, -2);
|
luaK_S(fs, OP_IFEQJMP, 0, -2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_neq (LexState *ls) {
|
static void luaK_neq (FuncState *fs) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
if (*previous == CREATE_U(OP_PUSHNIL, 1)) {
|
if (*previous == CREATE_U(OP_PUSHNIL, 1)) {
|
||||||
ls->fs->pc--; /* remove PUSHNIL */
|
fs->pc--; /* remove PUSHNIL */
|
||||||
luaK_deltastack(ls, -1); /* undo effect of PUSHNIL */
|
luaK_deltastack(fs, -1); /* undo effect of PUSHNIL */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
luaK_S(ls, OP_IFNEQJMP, 0, -2);
|
luaK_S(fs, OP_IFNEQJMP, 0, -2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_retcode (LexState *ls, int nlocals, int nexps) {
|
void luaK_retcode (FuncState *fs, int nlocals, int nexps) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
if (nexps > 0 && GET_OPCODE(*previous) == OP_CALL) {
|
if (nexps > 0 && GET_OPCODE(*previous) == OP_CALL) {
|
||||||
LUA_ASSERT(ls->L, GETARG_B(*previous) == MULT_RET, "call should be open");
|
LUA_ASSERT(fs->L, GETARG_B(*previous) == MULT_RET, "call should be open");
|
||||||
SET_OPCODE(*previous, OP_TAILCALL);
|
SET_OPCODE(*previous, OP_TAILCALL);
|
||||||
SETARG_B(*previous, nlocals);
|
SETARG_B(*previous, nlocals);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
luaK_primitivecode(ls, CREATE_U(OP_RETURN, nlocals));
|
luaK_primitivecode(fs, CREATE_U(OP_RETURN, nlocals));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_pushnil (LexState *ls, int n) {
|
static void luaK_pushnil (FuncState *fs, int n) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
luaK_deltastack(ls, n);
|
luaK_deltastack(fs, n);
|
||||||
switch(GET_OPCODE(*previous)) {
|
switch(GET_OPCODE(*previous)) {
|
||||||
case OP_PUSHNIL: SETARG_U(*previous, GETARG_U(*previous)+n); break;
|
case OP_PUSHNIL: SETARG_U(*previous, GETARG_U(*previous)+n); break;
|
||||||
default: luaK_primitivecode(ls, CREATE_U(OP_PUSHNIL, n));
|
default: luaK_primitivecode(fs, CREATE_U(OP_PUSHNIL, n));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaK_code (LexState *ls, Instruction i, int delta) {
|
int luaK_code (FuncState *fs, Instruction i, int delta) {
|
||||||
luaK_deltastack(ls, delta);
|
luaK_deltastack(fs, delta);
|
||||||
return luaK_primitivecode(ls, i);
|
return luaK_primitivecode(fs, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_fixjump (LexState *ls, int pc, int dest) {
|
void luaK_fixjump (FuncState *fs, int pc, int dest) {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
Instruction *jmp = &fs->f->code[pc];
|
Instruction *jmp = &fs->f->code[pc];
|
||||||
/* jump is relative to position following jump instruction */
|
if (dest != NO_JUMP) {
|
||||||
SETARG_S(*jmp, dest-(pc+1));
|
/* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -162,26 +172,24 @@ void luaK_fixjump (LexState *ls, int pc, int dest) {
|
|||||||
** returns current `pc' and marks it as a jump target (to avoid wrong
|
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||||
** optimizations with consecutive instructions not in the same basic block).
|
** optimizations with consecutive instructions not in the same basic block).
|
||||||
*/
|
*/
|
||||||
int luaK_getlabel (LexState *ls) {
|
int luaK_getlabel (FuncState *fs) {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
fs->lasttarget = fs->pc;
|
fs->lasttarget = fs->pc;
|
||||||
return fs->pc;
|
return fs->pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_deltastack (LexState *ls, int delta) {
|
void luaK_deltastack (FuncState *fs, int delta) {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
fs->stacksize += delta;
|
fs->stacksize += delta;
|
||||||
if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
|
if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
|
||||||
if (fs->stacksize > MAXSTACK)
|
if (fs->stacksize > MAXSTACK)
|
||||||
luaK_error(ls, "function or expression too complex");
|
luaK_error(fs->ls, "function or expression too complex");
|
||||||
fs->f->maxstacksize = fs->stacksize;
|
fs->f->maxstacksize = fs->stacksize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_kstr (LexState *ls, int c) {
|
void luaK_kstr (LexState *ls, int c) {
|
||||||
luaK_U(ls, OP_PUSHSTRING, c, 1);
|
luaK_U(ls->fs, OP_PUSHSTRING, c, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -189,103 +197,104 @@ void luaK_kstr (LexState *ls, int c) {
|
|||||||
#define LOOKBACKNUMS 20 /* arbitrary limit */
|
#define LOOKBACKNUMS 20 /* arbitrary limit */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int real_constant (LexState *ls, Number r) {
|
static int real_constant (FuncState *fs, Number r) {
|
||||||
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
|
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
|
||||||
Proto *f = ls->fs->f;
|
Proto *f = fs->f;
|
||||||
int c = f->nknum;
|
int c = f->nknum;
|
||||||
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
|
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
|
||||||
while (--c >= lim)
|
while (--c >= lim)
|
||||||
if (f->knum[c] == r) return c;
|
if (f->knum[c] == r) return c;
|
||||||
/* not found; create a new entry */
|
/* not found; create a new entry */
|
||||||
luaM_growvector(ls->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
|
luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
|
||||||
c = f->nknum++;
|
c = f->nknum++;
|
||||||
f->knum[c] = r;
|
f->knum[c] = r;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_number (LexState *ls, Number f) {
|
void luaK_number (FuncState *fs, Number f) {
|
||||||
if (f <= (Number)MAXARG_S && (int)f == f)
|
if (f <= (Number)MAXARG_S && (int)f == f)
|
||||||
luaK_S(ls, OP_PUSHINT, (int)f, 1); /* f has a short integer value */
|
luaK_S(fs, OP_PUSHINT, (int)f, 1); /* f has a short integer value */
|
||||||
else
|
else
|
||||||
luaK_U(ls, OP_PUSHNUM, real_constant(ls, f), 1);
|
luaK_U(fs, OP_PUSHNUM, real_constant(fs, f), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_adjuststack (LexState *ls, int n) {
|
void luaK_adjuststack (FuncState *fs, int n) {
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
luaK_U(ls, OP_POP, n, -n);
|
luaK_U(fs, OP_POP, n, -n);
|
||||||
else if (n < 0)
|
else if (n < 0)
|
||||||
luaK_pushnil(ls, -n);
|
luaK_pushnil(fs, -n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaK_lastisopen (LexState *ls) {
|
int luaK_lastisopen (FuncState *fs) {
|
||||||
/* check whether last instruction is an (open) function call */
|
/* check whether last instruction is an (open) function call */
|
||||||
Instruction *i = previous_instruction(ls);
|
Instruction *i = previous_instruction(fs);
|
||||||
if (GET_OPCODE(*i) == OP_CALL) {
|
if (GET_OPCODE(*i) == OP_CALL) {
|
||||||
LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open");
|
LUA_ASSERT(fs->L, GETARG_B(*i) == MULT_RET, "call should be open");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_setcallreturns (LexState *ls, int nresults) {
|
void luaK_setcallreturns (FuncState *fs, int nresults) {
|
||||||
Instruction *i = previous_instruction(ls);
|
Instruction *i = previous_instruction(fs);
|
||||||
if (GET_OPCODE(*i) == OP_CALL) { /* expression is a function call? */
|
if (GET_OPCODE(*i) == OP_CALL) { /* expression is a function call? */
|
||||||
LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open");
|
LUA_ASSERT(fs->L, GETARG_B(*i) == MULT_RET, "call should be open");
|
||||||
SETARG_B(*i, nresults); /* set nresults */
|
SETARG_B(*i, nresults); /* set nresults */
|
||||||
luaK_deltastack(ls, nresults); /* push results */
|
luaK_deltastack(fs, nresults); /* push results */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void assertglobal (LexState *ls, int index) {
|
static void assertglobal (FuncState *fs, int index) {
|
||||||
luaS_assertglobal(ls->L, ls->fs->f->kstr[index]);
|
luaS_assertglobal(fs->L, fs->f->kstr[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int discharge (LexState *ls, expdesc *var) {
|
static int discharge (FuncState *fs, expdesc *var) {
|
||||||
switch (var->k) {
|
switch (var->k) {
|
||||||
case VLOCAL:
|
case VLOCAL:
|
||||||
luaK_U(ls, OP_PUSHLOCAL, var->u.index, 1);
|
luaK_U(fs, OP_PUSHLOCAL, var->u.index, 1);
|
||||||
break;
|
break;
|
||||||
case VGLOBAL:
|
case VGLOBAL:
|
||||||
luaK_U(ls, OP_GETGLOBAL, var->u.index, 1);
|
luaK_U(fs, OP_GETGLOBAL, var->u.index, 1);
|
||||||
assertglobal(ls, var->u.index); /* make sure that there is a global */
|
assertglobal(fs, var->u.index); /* make sure that there is a global */
|
||||||
break;
|
break;
|
||||||
case VINDEXED:
|
case VINDEXED:
|
||||||
luaK_gettable(ls);
|
luaK_gettable(fs);
|
||||||
break;
|
break;
|
||||||
case VEXP:
|
case VEXP:
|
||||||
return 0; /* nothing to do */
|
return 0; /* nothing to do */
|
||||||
}
|
}
|
||||||
var->k = VEXP;
|
var->k = VEXP;
|
||||||
var->u.l.t = var->u.l.f = 0;
|
var->u.l.t = var->u.l.f = NO_JUMP;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void discharge1 (LexState *ls, expdesc *var) {
|
static void discharge1 (FuncState *fs, expdesc *var) {
|
||||||
discharge(ls, var);
|
discharge(fs, var);
|
||||||
/* if it has jumps it is already discharged */
|
/* if it has jumps it is already discharged */
|
||||||
if (var->u.l.t == 0 && var->u.l.f == 0)
|
if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
|
||||||
luaK_setcallreturns(ls, 1); /* call must return 1 value */
|
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_storevar (LexState *ls, const expdesc *var) {
|
void luaK_storevar (LexState *ls, const expdesc *var) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
switch (var->k) {
|
switch (var->k) {
|
||||||
case VLOCAL:
|
case VLOCAL:
|
||||||
luaK_U(ls, OP_SETLOCAL, var->u.index, -1);
|
luaK_U(fs, OP_SETLOCAL, var->u.index, -1);
|
||||||
break;
|
break;
|
||||||
case VGLOBAL:
|
case VGLOBAL:
|
||||||
luaK_U(ls, OP_SETGLOBAL, var->u.index, -1);
|
luaK_U(fs, OP_SETGLOBAL, var->u.index, -1);
|
||||||
assertglobal(ls, var->u.index); /* make sure that there is a global */
|
assertglobal(fs, var->u.index); /* make sure that there is a global */
|
||||||
break;
|
break;
|
||||||
case VINDEXED:
|
case VINDEXED:
|
||||||
luaK_0(ls, OP_SETTABLEPOP, -3);
|
luaK_0(fs, OP_SETTABLEPOP, -3);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LUA_INTERNALERROR(ls->L, "invalid var kind to store");
|
LUA_INTERNALERROR(ls->L, "invalid var kind to store");
|
||||||
@ -310,128 +319,113 @@ static OpCode invertjump (OpCode op) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_jump (LexState *ls, OpCode jump) {
|
static void luaK_jump (FuncState *fs, OpCode jump) {
|
||||||
Instruction *previous = previous_instruction(ls);
|
Instruction *previous = previous_instruction(fs);
|
||||||
luaK_deltastack(ls, -1);
|
luaK_deltastack(fs, -1);
|
||||||
if (*previous == CREATE_0(OP_NOT))
|
if (*previous == CREATE_0(OP_NOT))
|
||||||
*previous = CREATE_S(invertjump(jump), 0);
|
*previous = CREATE_S(invertjump(jump), 0);
|
||||||
else
|
else
|
||||||
luaK_primitivecode(ls, CREATE_S(jump, 0));
|
luaK_primitivecode(fs, CREATE_S(jump, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void insert_last (FuncState *fs, int *list) {
|
static void insert_last (FuncState *fs, int *list) {
|
||||||
int first = *list;
|
int first = *list;
|
||||||
*list = fs->pc-1; /* insert last instruction in the list */
|
*list = fs->pc-1; /* insert last instruction in the list */
|
||||||
if (first == 0)
|
luaK_fixjump(fs, *list, first);
|
||||||
SETARG_S(fs->f->code[*list], 0);
|
|
||||||
else
|
|
||||||
SETARG_S(fs->f->code[*list], first-fs->pc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void luaK_patchlistaux (LexState *ls, int list, int target,
|
static void luaK_patchlistaux (FuncState *fs, int list, int target,
|
||||||
OpCode special, int special_target) {
|
OpCode special, int special_target) {
|
||||||
if (list != 0) {
|
Instruction *code = fs->f->code;
|
||||||
Instruction *code = ls->fs->f->code;
|
while (list != NO_JUMP) {
|
||||||
for (;;) {
|
int next = luaK_getjump(fs, list);
|
||||||
Instruction *i = &code[list];
|
Instruction *i = &code[list];
|
||||||
OpCode op = GET_OPCODE(*i);
|
OpCode op = GET_OPCODE(*i);
|
||||||
int next = GETARG_S(*i);
|
if (op == special) /* this `op' already has a value */
|
||||||
if (op == special) /* this `op' already has a value */
|
SETARG_S(*i, special_target-(list+1));
|
||||||
SETARG_S(*i, special_target-(list+1));
|
else {
|
||||||
else {
|
SETARG_S(*i, target-(list+1)); /* do the patch */
|
||||||
SETARG_S(*i, target-(list+1)); /* do the patch */
|
if (op == OP_ONTJMP) /* remove eventual values */
|
||||||
if (op == OP_ONTJMP) /* remove eventual values */
|
SET_OPCODE(*i, OP_IFTJMP);
|
||||||
SET_OPCODE(*i, OP_IFTJMP);
|
else if (op == OP_ONFJMP)
|
||||||
else if (op == OP_ONFJMP)
|
SET_OPCODE(*i, OP_IFFJMP);
|
||||||
SET_OPCODE(*i, OP_IFFJMP);
|
|
||||||
}
|
|
||||||
if (next == 0) return;
|
|
||||||
list += next+1;
|
|
||||||
}
|
}
|
||||||
|
list = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_patchlist (LexState *ls, int list, int target) {
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
||||||
luaK_patchlistaux(ls, list, target, OP_END, 0);
|
luaK_patchlistaux(fs, list, target, OP_END, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int need_value (FuncState *fs, int list, OpCode hasvalue) {
|
static int need_value (FuncState *fs, int list, OpCode hasvalue) {
|
||||||
if (list == 0) return 0;
|
/* check whether list has a jump without a value */
|
||||||
else { /* check whether list has a jump without a value */
|
for (; list != NO_JUMP; list = luaK_getjump(fs, list))
|
||||||
Instruction *code = fs->f->code;
|
if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
|
||||||
for (;;) {
|
return 0; /* not found */
|
||||||
int next = GETARG_S(code[list]);
|
|
||||||
if (GET_OPCODE(code[list]) != hasvalue) return 1;
|
|
||||||
else if (next == 0) return 0;
|
|
||||||
list += next+1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void concatlists (LexState *ls, int *l1, int l2) {
|
static void concatlists (FuncState *fs, int *l1, int l2) {
|
||||||
if (*l1 == 0)
|
if (*l1 == NO_JUMP)
|
||||||
*l1 = l2;
|
*l1 = l2;
|
||||||
else if (l2 != 0) {
|
else {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
int list = *l1;
|
int list = *l1;
|
||||||
for (;;) { /* traverse `l1' */
|
for (;;) { /* traverse `l1' */
|
||||||
int next = GETARG_S(fs->f->code[list]);
|
int next = luaK_getjump(fs, list);
|
||||||
if (next == 0) { /* end of list? */
|
if (next == NO_JUMP) { /* end of list? */
|
||||||
SETARG_S(fs->f->code[list], l2-(list+1)); /* end points to `l2' */
|
luaK_fixjump(fs, list, l2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
list += next+1;
|
list = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_goiftrue (LexState *ls, expdesc *v, int keepvalue) {
|
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
Instruction *previous;
|
Instruction *previous;
|
||||||
discharge1(ls, v);
|
discharge1(fs, v);
|
||||||
previous = &fs->f->code[fs->pc-1];
|
previous = &fs->f->code[fs->pc-1];
|
||||||
if (ISJUMP(GET_OPCODE(*previous)))
|
if (ISJUMP(GET_OPCODE(*previous)))
|
||||||
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
||||||
else {
|
else {
|
||||||
OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP;
|
OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP;
|
||||||
luaK_jump(ls, jump);
|
luaK_jump(fs, jump);
|
||||||
}
|
}
|
||||||
insert_last(fs, &v->u.l.f);
|
insert_last(fs, &v->u.l.f);
|
||||||
luaK_patchlist(ls, v->u.l.t, luaK_getlabel(ls));
|
luaK_patchlist(fs, v->u.l.t, luaK_getlabel(fs));
|
||||||
v->u.l.t = 0;
|
v->u.l.t = NO_JUMP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_goiffalse (LexState *ls, expdesc *v, int keepvalue) {
|
void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
Instruction previous;
|
Instruction previous;
|
||||||
discharge1(ls, v);
|
discharge1(fs, v);
|
||||||
previous = fs->f->code[fs->pc-1];
|
previous = fs->f->code[fs->pc-1];
|
||||||
if (!ISJUMP(GET_OPCODE(previous))) {
|
if (!ISJUMP(GET_OPCODE(previous))) {
|
||||||
OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP;
|
OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP;
|
||||||
luaK_jump(ls, jump);
|
luaK_jump(fs, jump);
|
||||||
}
|
}
|
||||||
insert_last(fs, &v->u.l.t);
|
insert_last(fs, &v->u.l.t);
|
||||||
luaK_patchlist(ls, v->u.l.f, luaK_getlabel(ls));
|
luaK_patchlist(fs, v->u.l.f, luaK_getlabel(fs));
|
||||||
v->u.l.f = 0;
|
v->u.l.f = NO_JUMP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
||||||
if (discharge(ls, v)) return;
|
FuncState *fs = ls->fs;
|
||||||
|
if (discharge(fs, v)) return;
|
||||||
else { /* is an expression */
|
else { /* is an expression */
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
|
OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
|
||||||
if (!ISJUMP(previous) && v->u.l.f == 0 && v->u.l.t == 0) {
|
if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
|
||||||
/* it is an expression without jumps */
|
/* it is an expression without jumps */
|
||||||
if (onlyone && v->k == VEXP)
|
if (onlyone && v->k == VEXP)
|
||||||
luaK_setcallreturns(ls, 1); /* call must return 1 value */
|
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else { /* expression has jumps... */
|
else { /* expression has jumps... */
|
||||||
@ -440,48 +434,48 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
|||||||
int final; /* position after whole expression */
|
int final; /* position after whole expression */
|
||||||
if (ISJUMP(previous)) {
|
if (ISJUMP(previous)) {
|
||||||
insert_last(fs, &v->u.l.t); /* put `previous' in true list */
|
insert_last(fs, &v->u.l.t); /* put `previous' in true list */
|
||||||
p_nil = luaK_0(ls, OP_PUSHNILJMP, 0);
|
p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
|
||||||
p_1 = luaK_S(ls, OP_PUSHINT, 1, 1);
|
p_1 = luaK_S(fs, OP_PUSHINT, 1, 1);
|
||||||
}
|
}
|
||||||
else { /* still may need a PUSHNIL or a PUSHINT */
|
else { /* still may need a PUSHNIL or a PUSHINT */
|
||||||
int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP);
|
int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP);
|
||||||
int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP);
|
int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP);
|
||||||
if (need_nil && need_1) {
|
if (need_nil && need_1) {
|
||||||
luaK_S(ls, OP_JMP, 2, 0); /* skip both pushes */
|
luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */
|
||||||
p_nil = luaK_0(ls, OP_PUSHNILJMP, 0);
|
p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
|
||||||
p_1 = luaK_S(ls, OP_PUSHINT, 1, 0);
|
p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
|
||||||
}
|
}
|
||||||
else if (need_nil || need_1) {
|
else if (need_nil || need_1) {
|
||||||
luaK_S(ls, OP_JMP, 1, 0); /* skip one push */
|
luaK_S(fs, OP_JMP, 1, 0); /* skip one push */
|
||||||
if (need_nil)
|
if (need_nil)
|
||||||
p_nil = luaK_U(ls, OP_PUSHNIL, 1, 0);
|
p_nil = luaK_U(fs, OP_PUSHNIL, 1, 0);
|
||||||
else /* need_1 */
|
else /* need_1 */
|
||||||
p_1 = luaK_S(ls, OP_PUSHINT, 1, 0);
|
p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final = luaK_getlabel(ls);
|
final = luaK_getlabel(fs);
|
||||||
luaK_patchlistaux(ls, v->u.l.f, p_nil, OP_ONFJMP, final);
|
luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final);
|
||||||
luaK_patchlistaux(ls, v->u.l.t, p_1, OP_ONTJMP, final);
|
luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final);
|
||||||
v->u.l.f = v->u.l.t = 0;
|
v->u.l.f = v->u.l.t = NO_JUMP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
if (op == '-') {
|
if (op == '-') {
|
||||||
luaK_tostack(ls, v, 1);
|
luaK_tostack(ls, v, 1);
|
||||||
luaK_minus(ls);
|
luaK_minus(fs);
|
||||||
}
|
}
|
||||||
else { /* op == NOT */
|
else { /* op == NOT */
|
||||||
FuncState *fs = ls->fs;
|
|
||||||
Instruction *previous;
|
Instruction *previous;
|
||||||
discharge1(ls, v);
|
discharge1(fs, v);
|
||||||
previous = &fs->f->code[fs->pc-1];
|
previous = &fs->f->code[fs->pc-1];
|
||||||
if (ISJUMP(GET_OPCODE(*previous)))
|
if (ISJUMP(GET_OPCODE(*previous)))
|
||||||
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
||||||
else
|
else
|
||||||
luaK_0(ls, OP_NOT, 0);
|
luaK_0(fs, OP_NOT, 0);
|
||||||
/* interchange true and false lists */
|
/* interchange true and false lists */
|
||||||
{ int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
|
{ int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
|
||||||
}
|
}
|
||||||
@ -489,43 +483,45 @@ void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
|||||||
|
|
||||||
|
|
||||||
void luaK_infix (LexState *ls, int op, expdesc *v) {
|
void luaK_infix (LexState *ls, int op, expdesc *v) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
if (op == TK_AND)
|
if (op == TK_AND)
|
||||||
luaK_goiftrue(ls, v, 1);
|
luaK_goiftrue(fs, v, 1);
|
||||||
else if (op == TK_OR)
|
else if (op == TK_OR)
|
||||||
luaK_goiffalse(ls, v, 1);
|
luaK_goiffalse(fs, v, 1);
|
||||||
else
|
else
|
||||||
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
|
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
|
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
if (op == TK_AND) {
|
if (op == TK_AND) {
|
||||||
LUA_ASSERT(ls->L, v1->u.l.t == 0, "list must be closed");
|
LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
|
||||||
discharge1(ls, v2);
|
discharge1(fs, v2);
|
||||||
v1->u.l.t = v2->u.l.t;
|
v1->u.l.t = v2->u.l.t;
|
||||||
concatlists(ls, &v1->u.l.f, v2->u.l.f);
|
concatlists(fs, &v1->u.l.f, v2->u.l.f);
|
||||||
}
|
}
|
||||||
else if (op == TK_OR) {
|
else if (op == TK_OR) {
|
||||||
LUA_ASSERT(ls->L, v1->u.l.f == 0, "list must be closed");
|
LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
|
||||||
discharge1(ls, v2);
|
discharge1(fs, v2);
|
||||||
v1->u.l.f = v2->u.l.f;
|
v1->u.l.f = v2->u.l.f;
|
||||||
concatlists(ls, &v1->u.l.t, v2->u.l.t);
|
concatlists(fs, &v1->u.l.t, v2->u.l.t);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
|
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case '+': luaK_add(ls); break;
|
case '+': luaK_add(fs); break;
|
||||||
case '-': luaK_sub(ls); break;
|
case '-': luaK_sub(fs); break;
|
||||||
case '*': luaK_0(ls, OP_MULT, -1); break;
|
case '*': luaK_0(fs, OP_MULT, -1); break;
|
||||||
case '/': luaK_0(ls, OP_DIV, -1); break;
|
case '/': luaK_0(fs, OP_DIV, -1); break;
|
||||||
case '^': luaK_0(ls, OP_POW, -1); break;
|
case '^': luaK_0(fs, OP_POW, -1); break;
|
||||||
case TK_CONC: luaK_conc(ls); break;
|
case TK_CONC: luaK_conc(fs); break;
|
||||||
case TK_EQ: luaK_eq(ls); break;
|
case TK_EQ: luaK_eq(fs); break;
|
||||||
case TK_NE: luaK_neq(ls); break;
|
case TK_NE: luaK_neq(fs); break;
|
||||||
case '>': luaK_S(ls, OP_IFGTJMP, 0, -2); break;
|
case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break;
|
||||||
case '<': luaK_S(ls, OP_IFLTJMP, 0, -2); break;
|
case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break;
|
||||||
case TK_GE: luaK_S(ls, OP_IFGEJMP, 0, -2); break;
|
case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break;
|
||||||
case TK_LE: luaK_S(ls, OP_IFLEJMP, 0, -2); break;
|
case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
lcode.h
28
lcode.h
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcode.h,v 1.5 2000/03/09 13:57:37 roberto Exp roberto $
|
** $Id: lcode.h,v 1.6 2000/03/10 18:37:44 roberto Exp roberto $
|
||||||
** Code generator for Lua
|
** Code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -20,20 +20,20 @@
|
|||||||
|
|
||||||
|
|
||||||
void luaK_error (LexState *ls, const char *msg);
|
void luaK_error (LexState *ls, const char *msg);
|
||||||
int luaK_primitivecode (LexState *ls, Instruction i);
|
int luaK_primitivecode (FuncState *fs, Instruction i);
|
||||||
int luaK_code (LexState *ls, Instruction i, int delta);
|
int luaK_code (FuncState *fs, Instruction i, int delta);
|
||||||
void luaK_retcode (LexState *ls, int nlocals, int nexps);
|
void luaK_retcode (FuncState *fs, int nlocals, int nexps);
|
||||||
void luaK_fixjump (LexState *ls, int pc, int dest);
|
void luaK_fixjump (FuncState *fs, int pc, int dest);
|
||||||
void luaK_patchlist (LexState *ls, int list, int target);
|
void luaK_patchlist (FuncState *fs, int list, int target);
|
||||||
void luaK_goiftrue (LexState *ls, expdesc *v, int keepvalue);
|
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue);
|
||||||
void luaK_goiffalse (LexState *ls, expdesc *v, int keepvalue);
|
void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue);
|
||||||
int luaK_getlabel (LexState *ls);
|
int luaK_getlabel (FuncState *fs);
|
||||||
void luaK_deltastack (LexState *ls, int delta);
|
void luaK_deltastack (FuncState *fs, int delta);
|
||||||
void luaK_kstr (LexState *ls, int c);
|
void luaK_kstr (LexState *ls, int c);
|
||||||
void luaK_number (LexState *ls, Number f);
|
void luaK_number (FuncState *fs, Number f);
|
||||||
void luaK_adjuststack (LexState *ls, int n);
|
void luaK_adjuststack (FuncState *fs, int n);
|
||||||
int luaK_lastisopen (LexState *ls);
|
int luaK_lastisopen (FuncState *fs);
|
||||||
void luaK_setcallreturns (LexState *ls, int nresults);
|
void luaK_setcallreturns (FuncState *fs, int nresults);
|
||||||
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
|
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
|
||||||
void luaK_storevar (LexState *ls, const expdesc *var);
|
void luaK_storevar (LexState *ls, const expdesc *var);
|
||||||
void luaK_prefix (LexState *ls, int op, expdesc *v);
|
void luaK_prefix (LexState *ls, int op, expdesc *v);
|
||||||
|
10
lopcodes.h
10
lopcodes.h
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.h,v 1.47 2000/03/09 13:57:37 roberto Exp roberto $
|
** $Id: lopcodes.h,v 1.48 2000/03/10 18:37:44 roberto Exp roberto $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -20,8 +20,8 @@
|
|||||||
type 3: 1st unsigned argument in the higher 16 bits (`A')
|
type 3: 1st unsigned argument in the higher 16 bits (`A')
|
||||||
2nd unsigned argument in the middle 8 bits (`B')
|
2nd unsigned argument in the middle 8 bits (`B')
|
||||||
|
|
||||||
The signed argument is represented in excess 2^23; that is, the Number value
|
The signed argument is represented in excess 2^23; that is, the number
|
||||||
is the usigned value minus 2^23.
|
value is the usigned value minus 2^23.
|
||||||
===========================================================================*/
|
===========================================================================*/
|
||||||
|
|
||||||
#define SIZE_INSTRUCTION 32
|
#define SIZE_INSTRUCTION 32
|
||||||
@ -92,7 +92,7 @@ OP_END,/* - - (return) */
|
|||||||
OP_RETURN,/* U - (return) */
|
OP_RETURN,/* U - (return) */
|
||||||
|
|
||||||
OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
|
OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
|
||||||
OP_TAILCALL,/* A B v_a-v_1 f (return) f(v1,...,v_a) */
|
OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
|
||||||
|
|
||||||
OP_PUSHNIL,/* U - nil_1-nil_u */
|
OP_PUSHNIL,/* U - nil_1-nil_u */
|
||||||
OP_POP,/* U a_u-a_1 - */
|
OP_POP,/* U a_u-a_1 - */
|
||||||
@ -158,7 +158,7 @@ OP_SETLINE/* U - - LINE=u */
|
|||||||
|
|
||||||
|
|
||||||
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
||||||
#define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */
|
#define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
154
lparser.c
154
lparser.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.c,v 1.67 2000/03/09 13:57:37 roberto Exp roberto $
|
** $Id: lparser.c,v 1.68 2000/03/10 18:37:44 roberto Exp roberto $
|
||||||
** LL(1) Parser and code generator for Lua
|
** LL(1) Parser and code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -115,9 +115,10 @@ static void checklimit (LexState *ls, int val, int limit, const char *msg) {
|
|||||||
|
|
||||||
|
|
||||||
static void check_debugline (LexState *ls) {
|
static void check_debugline (LexState *ls) {
|
||||||
if (ls->L->debug && ls->linenumber != ls->fs->lastsetline) {
|
FuncState *fs = ls->fs;
|
||||||
luaK_U(ls, OP_SETLINE, ls->linenumber, 0);
|
if (ls->L->debug && ls->linenumber != fs->lastsetline) {
|
||||||
ls->fs->lastsetline = ls->linenumber;
|
luaK_U(fs, OP_SETLINE, ls->linenumber, 0);
|
||||||
|
fs->lastsetline = ls->linenumber;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,11 +131,11 @@ static void check_match (LexState *ls, int what, int who, int where) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int string_constant (LexState *ls, FuncState *fs, TString *s) {
|
static int string_constant (FuncState *fs, TString *s) {
|
||||||
Proto *f = fs->f;
|
Proto *f = fs->f;
|
||||||
int c = s->constindex;
|
int c = s->constindex;
|
||||||
if (c >= f->nkstr || f->kstr[c] != s) {
|
if (c >= f->nkstr || f->kstr[c] != s) {
|
||||||
luaM_growvector(ls->L, f->kstr, f->nkstr, 1, TString *,
|
luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *,
|
||||||
constantEM, MAXARG_U);
|
constantEM, MAXARG_U);
|
||||||
c = f->nkstr++;
|
c = f->nkstr++;
|
||||||
f->kstr[c] = s;
|
f->kstr[c] = s;
|
||||||
@ -145,7 +146,7 @@ static int string_constant (LexState *ls, FuncState *fs, TString *s) {
|
|||||||
|
|
||||||
|
|
||||||
static void code_string (LexState *ls, TString *s) {
|
static void code_string (LexState *ls, TString *s) {
|
||||||
luaK_kstr(ls, string_constant(ls, ls->fs, s));
|
luaK_kstr(ls, string_constant(ls->fs, s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -153,7 +154,7 @@ static int checkname (LexState *ls) {
|
|||||||
int sc;
|
int sc;
|
||||||
if (ls->token != TK_NAME)
|
if (ls->token != TK_NAME)
|
||||||
luaK_error(ls, "<name> expected");
|
luaK_error(ls, "<name> expected");
|
||||||
sc = string_constant(ls, ls->fs, ls->seminfo.ts);
|
sc = string_constant(ls->fs, ls->seminfo.ts);
|
||||||
next(ls);
|
next(ls);
|
||||||
return sc;
|
return sc;
|
||||||
}
|
}
|
||||||
@ -226,7 +227,7 @@ static void singlevar (LexState *ls, TString *n, expdesc *var, int prev) {
|
|||||||
if (aux_localname(level, n) >= 0)
|
if (aux_localname(level, n) >= 0)
|
||||||
luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
|
luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str);
|
||||||
var->k = VGLOBAL;
|
var->k = VGLOBAL;
|
||||||
var->u.index = string_constant(ls, fs, n);
|
var->u.index = string_constant(fs, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,29 +250,31 @@ static int indexupvalue (LexState *ls, TString *n) {
|
|||||||
|
|
||||||
|
|
||||||
static void pushupvalue (LexState *ls, TString *n) {
|
static void pushupvalue (LexState *ls, TString *n) {
|
||||||
if (ls->fs->prev == NULL)
|
FuncState *fs = ls->fs;
|
||||||
|
if (fs->prev == NULL)
|
||||||
luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
|
luaX_syntaxerror(ls, "cannot access upvalue in main", n->str);
|
||||||
if (aux_localname(ls->fs, n) >= 0)
|
if (aux_localname(ls->fs, n) >= 0)
|
||||||
luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
|
luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
|
||||||
luaK_U(ls, OP_PUSHUPVALUE, indexupvalue(ls, n), 1);
|
luaK_U(fs, OP_PUSHUPVALUE, indexupvalue(ls, n), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void adjust_mult_assign (LexState *ls, int nvars, int nexps) {
|
static void adjust_mult_assign (LexState *ls, int nvars, int nexps) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
int diff = nexps - nvars;
|
int diff = nexps - nvars;
|
||||||
if (nexps == 0 || !luaK_lastisopen(ls)) { /* list is empty or closed */
|
if (nexps == 0 || !luaK_lastisopen(fs)) { /* list is empty or closed */
|
||||||
/* push or pop eventual difference between list lengths */
|
/* push or pop eventual difference between list lengths */
|
||||||
luaK_adjuststack(ls, diff);
|
luaK_adjuststack(fs, diff);
|
||||||
}
|
}
|
||||||
else { /* list ends in a function call; must correct it */
|
else { /* list ends in a function call; must correct it */
|
||||||
diff--; /* do not count function call itself */
|
diff--; /* do not count function call itself */
|
||||||
if (diff <= 0) { /* more variables than values? */
|
if (diff <= 0) { /* more variables than values? */
|
||||||
/* function call must provide extra values */
|
/* function call must provide extra values */
|
||||||
luaK_setcallreturns(ls, -diff);
|
luaK_setcallreturns(fs, -diff);
|
||||||
}
|
}
|
||||||
else { /* more values than variables */
|
else { /* more values than variables */
|
||||||
luaK_setcallreturns(ls, 0); /* call should provide no value */
|
luaK_setcallreturns(fs, 0); /* call should provide no value */
|
||||||
luaK_adjuststack(ls, diff); /* pop eventual extra values */
|
luaK_adjuststack(fs, diff); /* pop eventual extra values */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -285,9 +288,9 @@ static void code_args (LexState *ls, int nparams, int dots) {
|
|||||||
fs->f->numparams = nparams;
|
fs->f->numparams = nparams;
|
||||||
fs->f->is_vararg = dots;
|
fs->f->is_vararg = dots;
|
||||||
if (!dots)
|
if (!dots)
|
||||||
luaK_deltastack(ls, nparams);
|
luaK_deltastack(fs, nparams);
|
||||||
else {
|
else {
|
||||||
luaK_deltastack(ls, nparams+1);
|
luaK_deltastack(fs, nparams+1);
|
||||||
add_localvar(ls, luaS_newfixed(ls->L, "arg"));
|
add_localvar(ls, luaS_newfixed(ls->L, "arg"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -298,7 +301,7 @@ static int getvarname (LexState *ls, expdesc *var) {
|
|||||||
case VGLOBAL:
|
case VGLOBAL:
|
||||||
return var->u.index;
|
return var->u.index;
|
||||||
case VLOCAL:
|
case VLOCAL:
|
||||||
return string_constant(ls, ls->fs, ls->fs->localvar[var->u.index]);
|
return string_constant(ls->fs, ls->fs->localvar[var->u.index]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error_unexpected(ls); /* there is no `var name' */
|
error_unexpected(ls); /* there is no `var name' */
|
||||||
@ -308,15 +311,16 @@ static int getvarname (LexState *ls, expdesc *var) {
|
|||||||
|
|
||||||
|
|
||||||
static void func_onstack (LexState *ls, FuncState *func) {
|
static void func_onstack (LexState *ls, FuncState *func) {
|
||||||
Proto *f = ls->fs->f;
|
FuncState *fs = ls->fs;
|
||||||
|
Proto *f = fs->f;
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<func->nupvalues; i++)
|
for (i=0; i<func->nupvalues; i++)
|
||||||
luaK_tostack(ls, &func->upvalues[i], 1);
|
luaK_tostack(ls, &func->upvalues[i], 1);
|
||||||
luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *,
|
luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *,
|
||||||
constantEM, MAXARG_A);
|
constantEM, MAXARG_A);
|
||||||
f->kproto[f->nkproto++] = func->f;
|
f->kproto[f->nkproto++] = func->f;
|
||||||
luaK_deltastack(ls, 1); /* CLOSURE puts one extra element before popping */
|
luaK_deltastack(fs, 1); /* CLOSURE puts one extra element before popping */
|
||||||
luaK_AB(ls, OP_CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues);
|
luaK_AB(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -324,6 +328,8 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) {
|
|||||||
lua_State *L = ls->L;
|
lua_State *L = ls->L;
|
||||||
Proto *f = luaF_newproto(ls->L);
|
Proto *f = luaF_newproto(ls->L);
|
||||||
fs->prev = ls->fs; /* linked list of funcstates */
|
fs->prev = ls->fs; /* linked list of funcstates */
|
||||||
|
fs->ls = ls;
|
||||||
|
fs->L = ls->L;
|
||||||
ls->fs = fs;
|
ls->fs = fs;
|
||||||
fs->stacksize = 0;
|
fs->stacksize = 0;
|
||||||
fs->nlocalvar = 0;
|
fs->nlocalvar = 0;
|
||||||
@ -346,19 +352,20 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) {
|
|||||||
|
|
||||||
|
|
||||||
static void close_func (LexState *ls) {
|
static void close_func (LexState *ls) {
|
||||||
|
lua_State *L = ls->L;
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
Proto *f = fs->f;
|
Proto *f = fs->f;
|
||||||
luaK_0(ls, OP_END, 0);
|
luaK_0(fs, OP_END, 0);
|
||||||
luaM_reallocvector(ls->L, f->code, fs->pc, Instruction);
|
luaM_reallocvector(L, f->code, fs->pc, Instruction);
|
||||||
luaM_reallocvector(ls->L, f->kstr, f->nkstr, TString *);
|
luaM_reallocvector(L, f->kstr, f->nkstr, TString *);
|
||||||
luaM_reallocvector(ls->L, f->knum, f->nknum, Number);
|
luaM_reallocvector(L, f->knum, f->nknum, Number);
|
||||||
luaM_reallocvector(ls->L, f->kproto, f->nkproto, Proto *);
|
luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
|
||||||
if (fs->nvars != -1) { /* debug information? */
|
if (fs->nvars != -1) { /* debug information? */
|
||||||
luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
|
luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
|
||||||
luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar);
|
luaM_reallocvector(L, f->locvars, fs->nvars, LocVar);
|
||||||
}
|
}
|
||||||
ls->fs = fs->prev;
|
ls->fs = fs->prev;
|
||||||
ls->L->top--; /* pop function */
|
L->top--; /* pop function */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -423,7 +430,7 @@ static void funcargs (LexState *ls, int slf) {
|
|||||||
check_match(ls, ')', '(', line);
|
check_match(ls, ')', '(', line);
|
||||||
#ifdef LUA_COMPAT_ARGRET
|
#ifdef LUA_COMPAT_ARGRET
|
||||||
if (nargs > 0) /* arg list is not empty? */
|
if (nargs > 0) /* arg list is not empty? */
|
||||||
luaK_setcallreturns(ls, 1); /* last call returns only 1 value */
|
luaK_setcallreturns(fs, 1); /* last call returns only 1 value */
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -442,7 +449,7 @@ static void funcargs (LexState *ls, int slf) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
fs->stacksize = slevel; /* call will remove function and arguments */
|
fs->stacksize = slevel; /* call will remove function and arguments */
|
||||||
luaK_AB(ls, OP_CALL, slevel, MULT_RET, 0);
|
luaK_AB(fs, OP_CALL, slevel, MULT_RET, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -469,10 +476,10 @@ static void var_or_func_tail (LexState *ls, expdesc *v) {
|
|||||||
next(ls);
|
next(ls);
|
||||||
name = checkname(ls);
|
name = checkname(ls);
|
||||||
luaK_tostack(ls, v, 1); /* `v' must be on stack */
|
luaK_tostack(ls, v, 1); /* `v' must be on stack */
|
||||||
luaK_U(ls, OP_PUSHSELF, name, 1);
|
luaK_U(ls->fs, OP_PUSHSELF, name, 1);
|
||||||
funcargs(ls, 1);
|
funcargs(ls, 1);
|
||||||
v->k = VEXP;
|
v->k = VEXP;
|
||||||
v->u.l.t = v->u.l.f = 0;
|
v->u.l.t = v->u.l.f = NO_JUMP;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,7 +487,7 @@ static void var_or_func_tail (LexState *ls, expdesc *v) {
|
|||||||
luaK_tostack(ls, v, 1); /* `v' must be on stack */
|
luaK_tostack(ls, v, 1); /* `v' must be on stack */
|
||||||
funcargs(ls, 0);
|
funcargs(ls, 0);
|
||||||
v->k = VEXP;
|
v->k = VEXP;
|
||||||
v->u.l.t = v->u.l.f = 0;
|
v->u.l.t = v->u.l.f = NO_JUMP;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default: return; /* should be follow... */
|
default: return; /* should be follow... */
|
||||||
@ -494,7 +501,7 @@ static void var_or_func (LexState *ls, expdesc *v) {
|
|||||||
if (optional(ls, '%')) { /* upvalue? */
|
if (optional(ls, '%')) { /* upvalue? */
|
||||||
pushupvalue(ls, str_checkname(ls));
|
pushupvalue(ls, str_checkname(ls));
|
||||||
v->k = VEXP;
|
v->k = VEXP;
|
||||||
v->u.l.t = v->u.l.f = 0;
|
v->u.l.t = v->u.l.f = NO_JUMP;
|
||||||
}
|
}
|
||||||
else /* variable name */
|
else /* variable name */
|
||||||
singlevar(ls, str_checkname(ls), v, 0);
|
singlevar(ls, str_checkname(ls), v, 0);
|
||||||
@ -532,6 +539,7 @@ static void recfield (LexState *ls) {
|
|||||||
|
|
||||||
static int recfields (LexState *ls) {
|
static int recfields (LexState *ls) {
|
||||||
/* recfields -> { ',' recfield } [','] */
|
/* recfields -> { ',' recfield } [','] */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
int n = 1; /* one has been read before */
|
int n = 1; /* one has been read before */
|
||||||
int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */
|
int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */
|
||||||
while (ls->token == ',') {
|
while (ls->token == ',') {
|
||||||
@ -541,18 +549,19 @@ static int recfields (LexState *ls) {
|
|||||||
recfield(ls);
|
recfield(ls);
|
||||||
n++;
|
n++;
|
||||||
if (++mod_n == RFIELDS_PER_FLUSH) {
|
if (++mod_n == RFIELDS_PER_FLUSH) {
|
||||||
luaK_U(ls, OP_SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH);
|
luaK_U(fs, OP_SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH);
|
||||||
mod_n = 0;
|
mod_n = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mod_n)
|
if (mod_n)
|
||||||
luaK_U(ls, OP_SETMAP, mod_n-1, -2*mod_n);
|
luaK_U(fs, OP_SETMAP, mod_n-1, -2*mod_n);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int listfields (LexState *ls) {
|
static int listfields (LexState *ls) {
|
||||||
/* listfields -> { ',' exp1 } [','] */
|
/* listfields -> { ',' exp1 } [','] */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
int n = 1; /* one has been read before */
|
int n = 1; /* one has been read before */
|
||||||
int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */
|
int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */
|
||||||
while (ls->token == ',') {
|
while (ls->token == ',') {
|
||||||
@ -564,13 +573,13 @@ static int listfields (LexState *ls) {
|
|||||||
checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH,
|
checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH,
|
||||||
"items in a list initializer");
|
"items in a list initializer");
|
||||||
if (++mod_n == LFIELDS_PER_FLUSH) {
|
if (++mod_n == LFIELDS_PER_FLUSH) {
|
||||||
luaK_AB(ls, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1,
|
luaK_AB(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1,
|
||||||
-LFIELDS_PER_FLUSH);
|
-LFIELDS_PER_FLUSH);
|
||||||
mod_n = 0;
|
mod_n = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mod_n > 0)
|
if (mod_n > 0)
|
||||||
luaK_AB(ls, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n);
|
luaK_AB(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -618,8 +627,9 @@ static void constructor_part (LexState *ls, constdesc *cd) {
|
|||||||
|
|
||||||
static void constructor (LexState *ls) {
|
static void constructor (LexState *ls) {
|
||||||
/* constructor -> '{' constructor_part [';' constructor_part] '}' */
|
/* constructor -> '{' constructor_part [';' constructor_part] '}' */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
int line = ls->linenumber;
|
int line = ls->linenumber;
|
||||||
int pc = luaK_U(ls, OP_CREATETABLE, 0, 1);
|
int pc = luaK_U(fs, OP_CREATETABLE, 0, 1);
|
||||||
int nelems;
|
int nelems;
|
||||||
constdesc cd;
|
constdesc cd;
|
||||||
check(ls, '{');
|
check(ls, '{');
|
||||||
@ -635,7 +645,7 @@ static void constructor (LexState *ls) {
|
|||||||
}
|
}
|
||||||
check_match(ls, '}', '{', line);
|
check_match(ls, '}', '{', line);
|
||||||
/* set initial table size */
|
/* set initial table size */
|
||||||
SETARG_U(ls->fs->f->code[pc], nelems);
|
SETARG_U(fs->f->code[pc], nelems);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* }====================================================================== */
|
/* }====================================================================== */
|
||||||
@ -651,12 +661,13 @@ static void constructor (LexState *ls) {
|
|||||||
|
|
||||||
|
|
||||||
static void simpleexp (LexState *ls, expdesc *v) {
|
static void simpleexp (LexState *ls, expdesc *v) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
check_debugline(ls);
|
check_debugline(ls);
|
||||||
switch (ls->token) {
|
switch (ls->token) {
|
||||||
case TK_NUMBER: { /* simpleexp -> NUMBER */
|
case TK_NUMBER: { /* simpleexp -> NUMBER */
|
||||||
Number r = ls->seminfo.r;
|
Number r = ls->seminfo.r;
|
||||||
next(ls);
|
next(ls);
|
||||||
luaK_number(ls, r);
|
luaK_number(fs, r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -666,7 +677,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TK_NIL: /* simpleexp -> NIL */
|
case TK_NIL: /* simpleexp -> NIL */
|
||||||
luaK_adjuststack(ls, -1);
|
luaK_adjuststack(fs, -1);
|
||||||
next(ls);
|
next(ls);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -694,7 +705,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
v->k = VEXP;
|
v->k = VEXP;
|
||||||
v->u.l.t = v->u.l.f = 0;
|
v->u.l.t = v->u.l.f = NO_JUMP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -776,7 +787,7 @@ static void block (LexState *ls) {
|
|||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
int nlocalvar = fs->nlocalvar;
|
int nlocalvar = fs->nlocalvar;
|
||||||
chunk(ls);
|
chunk(ls);
|
||||||
luaK_adjuststack(ls, fs->nlocalvar - nlocalvar); /* remove local variables */
|
luaK_adjuststack(fs, fs->nlocalvar - nlocalvar); /* remove local variables */
|
||||||
for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
|
for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--)
|
||||||
luaI_unregisterlocalvar(ls, fs->lastsetline);
|
luaI_unregisterlocalvar(ls, fs->lastsetline);
|
||||||
}
|
}
|
||||||
@ -806,7 +817,7 @@ static int assignment (LexState *ls, expdesc *v, int nvars) {
|
|||||||
luaK_storevar(ls, v);
|
luaK_storevar(ls, v);
|
||||||
}
|
}
|
||||||
else { /* indexed var with values in between*/
|
else { /* indexed var with values in between*/
|
||||||
luaK_U(ls, OP_SETTABLE, left+(nvars-1), -1);
|
luaK_U(ls->fs, OP_SETTABLE, left+(nvars-1), -1);
|
||||||
left += 2; /* table&index are not popped, because they aren't on top */
|
left += 2; /* table&index are not popped, because they aren't on top */
|
||||||
}
|
}
|
||||||
return left;
|
return left;
|
||||||
@ -822,7 +833,7 @@ static void whilestat (LexState *ls, int line) {
|
|||||||
/* whilestat -> WHILE exp1 DO block END */
|
/* whilestat -> WHILE exp1 DO block END */
|
||||||
Instruction buffer[MAX_WHILE_EXP];
|
Instruction buffer[MAX_WHILE_EXP];
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
int while_init = luaK_getlabel(ls);
|
int while_init = luaK_getlabel(fs);
|
||||||
int loopentry; /* point to jump to repeat the loop */
|
int loopentry; /* point to jump to repeat the loop */
|
||||||
int cond_init; /* init of condition, after the move */
|
int cond_init; /* init of condition, after the move */
|
||||||
int cond_size;
|
int cond_size;
|
||||||
@ -830,7 +841,7 @@ static void whilestat (LexState *ls, int line) {
|
|||||||
int i;
|
int i;
|
||||||
next(ls); /* skip WHILE */
|
next(ls); /* skip WHILE */
|
||||||
expr(ls, &v); /* read condition */
|
expr(ls, &v); /* read condition */
|
||||||
luaK_goiffalse(ls, &v, 0);
|
luaK_goiffalse(fs, &v, 0);
|
||||||
cond_size = fs->pc - while_init;
|
cond_size = fs->pc - while_init;
|
||||||
/* save condition (to move it to after body) */
|
/* save condition (to move it to after body) */
|
||||||
if (cond_size > MAX_WHILE_EXP)
|
if (cond_size > MAX_WHILE_EXP)
|
||||||
@ -838,31 +849,32 @@ static void whilestat (LexState *ls, int line) {
|
|||||||
for (i=0; i<cond_size; i++) buffer[i] = fs->f->code[while_init+i];
|
for (i=0; i<cond_size; i++) buffer[i] = fs->f->code[while_init+i];
|
||||||
/* go back to state prior condition */
|
/* go back to state prior condition */
|
||||||
fs->pc = while_init;
|
fs->pc = while_init;
|
||||||
luaK_S(ls, OP_JMP, 0, 0); /* initial jump to condition */
|
luaK_S(fs, OP_JMP, 0, 0); /* initial jump to condition */
|
||||||
check(ls, TK_DO);
|
check(ls, TK_DO);
|
||||||
loopentry = luaK_getlabel(ls);
|
loopentry = luaK_getlabel(fs);
|
||||||
block(ls);
|
block(ls);
|
||||||
check_match(ls, TK_END, TK_WHILE, line);
|
check_match(ls, TK_END, TK_WHILE, line);
|
||||||
cond_init = luaK_getlabel(ls);
|
cond_init = luaK_getlabel(fs);
|
||||||
luaK_fixjump(ls, while_init, cond_init);
|
luaK_fixjump(fs, while_init, cond_init);
|
||||||
/* correct `v' and copy condition to new position */
|
/* correct `v' and copy condition to new position */
|
||||||
if (v.u.l.t != 0) v.u.l.t += cond_init-while_init;
|
if (v.u.l.t != NO_JUMP) v.u.l.t += cond_init-while_init;
|
||||||
for (i=0; i<cond_size; i++) luaK_primitivecode(ls, buffer[i]);
|
for (i=0; i<cond_size; i++) luaK_primitivecode(fs, buffer[i]);
|
||||||
luaK_patchlist(ls, v.u.l.t, loopentry);
|
luaK_patchlist(fs, v.u.l.t, loopentry);
|
||||||
luaK_getlabel(ls); /* mark possible jump to this point */
|
luaK_getlabel(fs); /* mark possible jump to this point */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void repeatstat (LexState *ls, int line) {
|
static void repeatstat (LexState *ls, int line) {
|
||||||
/* repeatstat -> REPEAT block UNTIL exp1 */
|
/* repeatstat -> REPEAT block UNTIL exp1 */
|
||||||
int repeat_init = luaK_getlabel(ls);
|
FuncState *fs = ls->fs;
|
||||||
|
int repeat_init = luaK_getlabel(fs);
|
||||||
expdesc v;
|
expdesc v;
|
||||||
next(ls);
|
next(ls);
|
||||||
block(ls);
|
block(ls);
|
||||||
check_match(ls, TK_UNTIL, TK_REPEAT, line);
|
check_match(ls, TK_UNTIL, TK_REPEAT, line);
|
||||||
expr(ls, &v);
|
expr(ls, &v);
|
||||||
luaK_goiftrue(ls, &v, 0);
|
luaK_goiftrue(fs, &v, 0);
|
||||||
luaK_patchlist(ls, v.u.l.f, repeat_init);
|
luaK_patchlist(fs, v.u.l.f, repeat_init);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -935,17 +947,18 @@ static int funcstat (LexState *ls, int line) {
|
|||||||
|
|
||||||
static void namestat (LexState *ls) {
|
static void namestat (LexState *ls) {
|
||||||
/* stat -> func | ['%'] NAME assignment */
|
/* stat -> func | ['%'] NAME assignment */
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
expdesc v;
|
expdesc v;
|
||||||
check_debugline(ls);
|
check_debugline(ls);
|
||||||
var_or_func(ls, &v);
|
var_or_func(ls, &v);
|
||||||
if (v.k == VEXP) { /* stat -> func */
|
if (v.k == VEXP) { /* stat -> func */
|
||||||
if (!luaK_lastisopen(ls)) /* is just an upvalue? */
|
if (!luaK_lastisopen(fs)) /* is just an upvalue? */
|
||||||
luaK_error(ls, "syntax error");
|
luaK_error(ls, "syntax error");
|
||||||
luaK_setcallreturns(ls, 0); /* call statement uses no results */
|
luaK_setcallreturns(fs, 0); /* call statement uses no results */
|
||||||
}
|
}
|
||||||
else { /* stat -> ['%'] NAME assignment */
|
else { /* stat -> ['%'] NAME assignment */
|
||||||
int left = assignment(ls, &v, 1);
|
int left = assignment(ls, &v, 1);
|
||||||
luaK_adjuststack(ls, left); /* remove eventual garbage left on stack */
|
luaK_adjuststack(fs, left); /* remove eventual garbage left on stack */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -957,11 +970,11 @@ static void ifpart (LexState *ls, int line) {
|
|||||||
int elseinit;
|
int elseinit;
|
||||||
next(ls); /* skip IF or ELSEIF */
|
next(ls); /* skip IF or ELSEIF */
|
||||||
expr(ls, &v); /* cond */
|
expr(ls, &v); /* cond */
|
||||||
luaK_goiftrue(ls, &v, 0);
|
luaK_goiftrue(fs, &v, 0);
|
||||||
check(ls, TK_THEN);
|
check(ls, TK_THEN);
|
||||||
block(ls); /* `then' part */
|
block(ls); /* `then' part */
|
||||||
luaK_S(ls, OP_JMP, 0, 0); /* 2nd jump: over `else' part */
|
luaK_S(fs, OP_JMP, 0, 0); /* 2nd jump: over `else' part */
|
||||||
elseinit = luaK_getlabel(ls); /* address of 2nd jump == elseinit-1 */
|
elseinit = luaK_getlabel(fs); /* address of 2nd jump == elseinit-1 */
|
||||||
if (ls->token == TK_ELSEIF)
|
if (ls->token == TK_ELSEIF)
|
||||||
ifpart(ls, line);
|
ifpart(ls, line);
|
||||||
else {
|
else {
|
||||||
@ -970,13 +983,13 @@ static void ifpart (LexState *ls, int line) {
|
|||||||
check_match(ls, TK_END, TK_IF, line);
|
check_match(ls, TK_END, TK_IF, line);
|
||||||
}
|
}
|
||||||
if (fs->pc > elseinit) { /* is there an `else' part? */
|
if (fs->pc > elseinit) { /* is there an `else' part? */
|
||||||
luaK_fixjump(ls, elseinit-1, luaK_getlabel(ls)); /* fix 2nd jump */
|
luaK_fixjump(fs, elseinit-1, luaK_getlabel(fs)); /* fix 2nd jump */
|
||||||
}
|
}
|
||||||
else { /* no else part */
|
else { /* no else part */
|
||||||
fs->pc--; /* remove 2nd jump */
|
fs->pc--; /* remove 2nd jump */
|
||||||
elseinit = luaK_getlabel(ls); /* `elseinit' points to end */
|
elseinit = luaK_getlabel(fs); /* `elseinit' points to end */
|
||||||
}
|
}
|
||||||
luaK_patchlist(ls, v.u.l.f, elseinit); /* fix 1st jump to `else' part */
|
luaK_patchlist(fs, v.u.l.f, elseinit); /* fix 1st jump to `else' part */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1080,12 +1093,13 @@ static void body (LexState *ls, int needself, int line) {
|
|||||||
static void ret (LexState *ls) {
|
static void ret (LexState *ls) {
|
||||||
/* ret -> [RETURN explist sc] */
|
/* ret -> [RETURN explist sc] */
|
||||||
if (ls->token == TK_RETURN) {
|
if (ls->token == TK_RETURN) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
int nexps; /* number of expressions returned */
|
int nexps; /* number of expressions returned */
|
||||||
check_debugline(ls);
|
check_debugline(ls);
|
||||||
next(ls);
|
next(ls);
|
||||||
nexps = explist(ls);
|
nexps = explist(ls);
|
||||||
luaK_retcode(ls, ls->fs->nlocalvar, nexps);
|
luaK_retcode(fs, ls->fs->nlocalvar, nexps);
|
||||||
ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */
|
fs->stacksize = fs->nlocalvar; /* removes all temp values */
|
||||||
optional(ls, ';');
|
optional(ls, ';');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.h,v 1.11 2000/03/09 13:57:37 roberto Exp roberto $
|
** $Id: lparser.h,v 1.12 2000/03/10 18:37:44 roberto Exp roberto $
|
||||||
** LL(1) Parser and code generator for Lua
|
** LL(1) Parser and code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -65,11 +65,15 @@ typedef struct expdesc {
|
|||||||
} expdesc;
|
} expdesc;
|
||||||
|
|
||||||
|
|
||||||
|
#define NO_JUMP (-1) /* marks end of patch list */
|
||||||
|
|
||||||
|
|
||||||
/* state needed to generate code for a given function */
|
/* state needed to generate code for a given function */
|
||||||
typedef struct FuncState {
|
typedef struct FuncState {
|
||||||
Proto *f; /* current function header */
|
Proto *f; /* current function header */
|
||||||
struct FuncState *prev; /* enclosing function */
|
struct FuncState *prev; /* enclosing function */
|
||||||
|
struct LexState *ls; /* lexical state */
|
||||||
|
struct lua_State *L; /* copy of the Lua state */
|
||||||
int pc; /* next position to code */
|
int pc; /* next position to code */
|
||||||
int lasttarget; /* `pc' of last `jump target' */
|
int lasttarget; /* `pc' of last `jump target' */
|
||||||
int stacksize; /* number of values on activation register */
|
int stacksize; /* number of values on activation register */
|
||||||
|
Loading…
Reference in New Issue
Block a user