This commit is contained in:
Roberto Ierusalimschy
2000-08-14 14:45:59 -03:00
parent 9e84bf18db
commit d1ffd1a1c5
2 changed files with 85 additions and 94 deletions

4
lua.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.43 2000/08/04 19:38:35 roberto Exp roberto $ ** $Id: lua.c,v 1.44 2000/08/09 19:16:57 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -53,7 +53,7 @@ static lua_Hook old_callhook = NULL;
extern void USERINIT (void); extern void USERINIT (void);
#else #else
#define USERINIT userinit #define USERINIT userinit
void userinit (void) { static void userinit (void) {
lua_iolibopen(); lua_iolibopen();
lua_strlibopen(); lua_strlibopen();
lua_mathlibopen(); lua_mathlibopen();

175
lvm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.125 2000/08/10 19:50:47 roberto Exp roberto $ ** $Id: lvm.c,v 1.126 2000/08/11 16:17:28 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -371,25 +371,24 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
if (linehook) if (linehook)
traceexec(L, base, top, linehook); traceexec(L, base, top, linehook);
switch (GET_OPCODE(i)) { switch (GET_OPCODE(i)) {
case OP_END: {
case OP_END:
return L->top; /* no results */ return L->top; /* no results */
}
case OP_RETURN: case OP_RETURN: {
L->top = top; L->top = top;
return base+GETARG_U(i); return base+GETARG_U(i);
}
case OP_CALL: case OP_CALL: {
L->top = top; L->top = top;
luaD_call(L, base+GETARG_A(i), GETARG_B(i)); luaD_call(L, base+GETARG_A(i), GETARG_B(i));
top = L->top; top = L->top;
break; break;
}
case OP_TAILCALL: case OP_TAILCALL: {
L->top = top; L->top = top;
luaD_call(L, base+GETARG_A(i), MULT_RET); luaD_call(L, base+GETARG_A(i), MULT_RET);
return base+GETARG_B(i); return base+GETARG_B(i);
}
case OP_PUSHNIL: { case OP_PUSHNIL: {
int n = GETARG_U(i); int n = GETARG_U(i);
LUA_ASSERT(n>0, "invalid argument"); LUA_ASSERT(n>0, "invalid argument");
@@ -398,66 +397,65 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} while (--n > 0); } while (--n > 0);
break; break;
} }
case OP_POP: {
case OP_POP:
top -= GETARG_U(i); top -= GETARG_U(i);
break; break;
}
case OP_PUSHINT: case OP_PUSHINT: {
ttype(top) = TAG_NUMBER; ttype(top) = TAG_NUMBER;
nvalue(top) = (Number)GETARG_S(i); nvalue(top) = (Number)GETARG_S(i);
top++; top++;
break; break;
}
case OP_PUSHSTRING: case OP_PUSHSTRING: {
ttype(top) = TAG_STRING; ttype(top) = TAG_STRING;
tsvalue(top) = kstr[GETARG_U(i)]; tsvalue(top) = kstr[GETARG_U(i)];
top++; top++;
break; break;
}
case OP_PUSHNUM: case OP_PUSHNUM: {
ttype(top) = TAG_NUMBER; ttype(top) = TAG_NUMBER;
nvalue(top) = tf->knum[GETARG_U(i)]; nvalue(top) = tf->knum[GETARG_U(i)];
top++; top++;
break; break;
}
case OP_PUSHNEGNUM: case OP_PUSHNEGNUM: {
ttype(top) = TAG_NUMBER; ttype(top) = TAG_NUMBER;
nvalue(top) = -tf->knum[GETARG_U(i)]; nvalue(top) = -tf->knum[GETARG_U(i)];
top++; top++;
break; break;
}
case OP_PUSHUPVALUE: case OP_PUSHUPVALUE: {
*top++ = cl->upvalue[GETARG_U(i)]; *top++ = cl->upvalue[GETARG_U(i)];
break; break;
}
case OP_GETLOCAL: case OP_GETLOCAL: {
*top++ = *(base+GETARG_U(i)); *top++ = *(base+GETARG_U(i));
break; break;
}
case OP_GETGLOBAL: case OP_GETGLOBAL: {
luaV_getglobal(L, kstr[GETARG_U(i)], top); luaV_getglobal(L, kstr[GETARG_U(i)], top);
top++; top++;
break; break;
}
case OP_GETTABLE: case OP_GETTABLE: {
luaV_gettable(L, top); luaV_gettable(L, top);
top--; top--;
break; break;
}
case OP_GETDOTTED: case OP_GETDOTTED: {
ttype(top) = TAG_STRING; ttype(top) = TAG_STRING;
tsvalue(top++) = kstr[GETARG_U(i)]; tsvalue(top++) = kstr[GETARG_U(i)];
luaV_gettable(L, top); luaV_gettable(L, top);
top--; top--;
break; break;
}
case OP_GETINDEXED: case OP_GETINDEXED: {
*top++ = *(base+GETARG_U(i)); *top++ = *(base+GETARG_U(i));
luaV_gettable(L, top); luaV_gettable(L, top);
top--; top--;
break; break;
}
case OP_PUSHSELF: { case OP_PUSHSELF: {
TObject receiver; TObject receiver;
receiver = *(top-1); receiver = *(top-1);
@@ -467,29 +465,28 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
*(top-1) = receiver; *(top-1) = receiver;
break; break;
} }
case OP_CREATETABLE: {
case OP_CREATETABLE:
L->top = top; L->top = top;
luaC_checkGC(L); luaC_checkGC(L);
hvalue(top) = luaH_new(L, GETARG_U(i)); hvalue(top) = luaH_new(L, GETARG_U(i));
ttype(top) = TAG_TABLE; ttype(top) = TAG_TABLE;
top++; top++;
break; break;
}
case OP_SETLOCAL: case OP_SETLOCAL: {
*(base+GETARG_U(i)) = *(--top); *(base+GETARG_U(i)) = *(--top);
break; break;
}
case OP_SETGLOBAL: case OP_SETGLOBAL: {
luaV_setglobal(L, kstr[GETARG_U(i)], top); luaV_setglobal(L, kstr[GETARG_U(i)], top);
top--; top--;
break; break;
}
case OP_SETTABLE: case OP_SETTABLE: {
luaV_settable(L, top-GETARG_A(i), top); luaV_settable(L, top-GETARG_A(i), top);
top -= GETARG_B(i); /* pop values */ top -= GETARG_B(i); /* pop values */
break; break;
}
case OP_SETLIST: { case OP_SETLIST: {
int aux = GETARG_A(i) * LFIELDS_PER_FLUSH; int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
int n = GETARG_B(i); int n = GETARG_B(i);
@@ -499,7 +496,6 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
*luaH_setint(L, arr, n+aux) = *(--top); *luaH_setint(L, arr, n+aux) = *(--top);
break; break;
} }
case OP_SETMAP: { case OP_SETMAP: {
int n = GETARG_U(i); int n = GETARG_U(i);
StkId finaltop = top-2*n; StkId finaltop = top-2*n;
@@ -511,16 +507,15 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
break; break;
} }
case OP_ADD: {
case OP_ADD:
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_ADD); call_arith(L, top, IM_ADD);
else else
nvalue(top-2) += nvalue(top-1); nvalue(top-2) += nvalue(top-1);
top--; top--;
break; break;
}
case OP_ADDI: case OP_ADDI: {
if (tonumber(top-1)) { if (tonumber(top-1)) {
ttype(top) = TAG_NUMBER; ttype(top) = TAG_NUMBER;
nvalue(top) = (Number)GETARG_S(i); nvalue(top) = (Number)GETARG_S(i);
@@ -529,37 +524,37 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
else else
nvalue(top-1) += (Number)GETARG_S(i); nvalue(top-1) += (Number)GETARG_S(i);
break; break;
}
case OP_SUB: case OP_SUB: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_SUB); call_arith(L, top, IM_SUB);
else else
nvalue(top-2) -= nvalue(top-1); nvalue(top-2) -= nvalue(top-1);
top--; top--;
break; break;
}
case OP_MULT: case OP_MULT: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_MUL); call_arith(L, top, IM_MUL);
else else
nvalue(top-2) *= nvalue(top-1); nvalue(top-2) *= nvalue(top-1);
top--; top--;
break; break;
}
case OP_DIV: case OP_DIV: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_DIV); call_arith(L, top, IM_DIV);
else else
nvalue(top-2) /= nvalue(top-1); nvalue(top-2) /= nvalue(top-1);
top--; top--;
break; break;
}
case OP_POW: case OP_POW: {
if (!call_binTM(L, top, IM_POW)) if (!call_binTM(L, top, IM_POW))
lua_error(L, "undefined operation"); lua_error(L, "undefined operation");
top--; top--;
break; break;
}
case OP_CONCAT: { case OP_CONCAT: {
int n = GETARG_U(i); int n = GETARG_U(i);
strconc(L, n, top); strconc(L, n, top);
@@ -568,8 +563,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
luaC_checkGC(L); luaC_checkGC(L);
break; break;
} }
case OP_MINUS: {
case OP_MINUS:
if (tonumber(top-1)) { if (tonumber(top-1)) {
ttype(top) = TAG_NIL; ttype(top) = TAG_NIL;
call_arith(L, top+1, IM_UNM); call_arith(L, top+1, IM_UNM);
@@ -577,71 +571,71 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
else else
nvalue(top-1) = -nvalue(top-1); nvalue(top-1) = -nvalue(top-1);
break; break;
}
case OP_NOT: case OP_NOT: {
ttype(top-1) = ttype(top-1) =
(ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL; (ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
nvalue(top-1) = 1; nvalue(top-1) = 1;
break; break;
}
case OP_JMPNE: case OP_JMPNE: {
top -= 2; top -= 2;
if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i); if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i);
break; break;
}
case OP_JMPEQ: case OP_JMPEQ: {
top -= 2; top -= 2;
if (luaO_equalObj(top, top+1)) pc += GETARG_S(i); if (luaO_equalObj(top, top+1)) pc += GETARG_S(i);
break; break;
}
case OP_JMPLT: case OP_JMPLT: {
top -= 2; top -= 2;
if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
break; break;
}
case OP_JMPLE: /* a <= b === !(b<a) */ case OP_JMPLE: { /* a <= b === !(b<a) */
top -= 2; top -= 2;
if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
break; break;
}
case OP_JMPGT: /* a > b === (b<a) */ case OP_JMPGT: { /* a > b === (b<a) */
top -= 2; top -= 2;
if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
break; break;
}
case OP_JMPGE: /* a >= b === !(a<b) */ case OP_JMPGE: { /* a >= b === !(a<b) */
top -= 2; top -= 2;
if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
break; break;
}
case OP_JMPT: case OP_JMPT: {
if (ttype(--top) != TAG_NIL) pc += GETARG_S(i); if (ttype(--top) != TAG_NIL) pc += GETARG_S(i);
break; break;
}
case OP_JMPF: case OP_JMPF: {
if (ttype(--top) == TAG_NIL) pc += GETARG_S(i); if (ttype(--top) == TAG_NIL) pc += GETARG_S(i);
break; break;
}
case OP_JMPONT: case OP_JMPONT: {
if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i); if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i);
else top--; else top--;
break; break;
}
case OP_JMPONF: case OP_JMPONF: {
if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i); if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i);
else top--; else top--;
break; break;
}
case OP_JMP: case OP_JMP: {
pc += GETARG_S(i); pc += GETARG_S(i);
break; break;
}
case OP_PUSHNILJMP: case OP_PUSHNILJMP: {
ttype(top++) = TAG_NIL; ttype(top++) = TAG_NIL;
pc++; pc++;
break; break;
}
case OP_FORPREP: case OP_FORPREP: {
if (tonumber(top-1)) if (tonumber(top-1))
lua_error(L, "`for' step must be a number"); lua_error(L, "`for' step must be a number");
if (tonumber(top-2)) if (tonumber(top-2))
@@ -655,7 +649,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
pc += GETARG_S(i)+1; /* jump to loop end */ pc += GETARG_S(i)+1; /* jump to loop end */
} }
break; break;
}
case OP_FORLOOP: { case OP_FORLOOP: {
LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step"); LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step");
LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit"); LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit");
@@ -670,7 +664,6 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
pc += GETARG_S(i); /* repeat loop */ pc += GETARG_S(i); /* repeat loop */
break; break;
} }
case OP_LFORPREP: { case OP_LFORPREP: {
if (ttype(top-1) != TAG_TABLE) if (ttype(top-1) != TAG_TABLE)
lua_error(L, "`for' table must be a table"); lua_error(L, "`for' table must be a table");
@@ -688,7 +681,6 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
break; break;
} }
case OP_LFORLOOP: { case OP_LFORLOOP: {
int n; int n;
top -= 2; /* remove old index,value */ top -= 2; /* remove old index,value */
@@ -705,14 +697,13 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
break; break;
} }
case OP_CLOSURE: {
case OP_CLOSURE:
L->top = top; L->top = top;
luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i)); luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
top = L->top; top = L->top;
luaC_checkGC(L); luaC_checkGC(L);
break; break;
}
} }
} }
} }