commit 7c261a13b572fb0f7449f6cdf2b495d0e5bd57d5
parent 2bb94d9e22f1f8ab2c00c9296a3920bebe4862bd
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 13 Oct 1997 20:11:43 -0200
more uniform treatment to opcode variants.
Diffstat:
M | lopcodes.h | | | 117 | +++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
M | lua.stx | | | 208 | +++++++++++++++++++++++++++++++++++-------------------------------------------- |
M | lvm.c | | | 145 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
3 files changed, 241 insertions(+), 229 deletions(-)
diff --git a/lopcodes.h b/lopcodes.h
@@ -1,5 +1,5 @@
/*
-** $Id: lopcodes.h,v 1.6 1997/10/01 20:05:34 roberto Exp roberto $
+** $Id: lopcodes.h,v 1.7 1997/10/06 14:51:11 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -9,9 +9,9 @@
/*
-** NOTICE: variants of the same opcode (like PUSH0, PUSHBYTE, PUSHWORD) must
-** be consecutive: First, those with built-in parameters, then with byte
-** parameter and last with word parameter.
+** NOTICE: variants of the same opcode must be consecutive: First, those
+** with byte parameter, then with built-in parameters, and last with
+** word parameter.
*/
@@ -20,15 +20,16 @@ typedef enum {
-----------------------------------------------------------------------------*/
ENDCODE,/* - - - */
-PUSHNIL,/* - - nil */
-PUSHNILS,/* b - nil_1...nil_b */
+PUSHNIL,/* b - nil_0...nil_b */
+PUSHNIL0,/* - - nil */
-PUSH0,/* - - 0.0 */
-PUSH1,/* - - 1.0 */
-PUSH2,/* - - 2.0 */
-PUSHBYTE,/* b - (float)b */
-PUSHWORD,/* w - (float)w */
+PUSHNUMBER,/* b - (float)b */
+PUSHNUMBER0,/* - - 0.0 */
+PUSHNUMBER1,/* - - 1.0 */
+PUSHNUMBER2,/* - - 2.0 */
+PUSHNUMBERW,/* w - (float)w */
+PUSHCONSTANT,/* b - CNST[b] */
PUSHCONSTANT0,/*- - CNST[0] */
PUSHCONSTANT1,/*- - CNST[1] */
PUSHCONSTANT2,/*- - CNST[2] */
@@ -37,15 +38,13 @@ PUSHCONSTANT4,/*- - CNST[4] */
PUSHCONSTANT5,/*- - CNST[5] */
PUSHCONSTANT6,/*- - CNST[6] */
PUSHCONSTANT7,/*- - CNST[7] */
-PUSHCONSTANT8,/*- - CNST[8] */
-PUSHCONSTANT9,/*- - CNST[9] */
-PUSHCONSTANTB,/*b - CNST[b] */
-PUSHCONSTANT,/* w - CNST[w] */
+PUSHCONSTANTW,/*w - CNST[w] */
+PUSHUPVALUE,/* b - Closure[b] */
PUSHUPVALUE0,/* - - Closure[0] */
PUSHUPVALUE1,/* - - Closure[1] */
-PUSHUPVALUE,/* b - Closure[b] */
+PUSHLOCAL,/* b - LOC[b] */
PUSHLOCAL0,/* - - LOC[0] */
PUSHLOCAL1,/* - - LOC[1] */
PUSHLOCAL2,/* - - LOC[2] */
@@ -54,10 +53,8 @@ PUSHLOCAL4,/* - - LOC[4] */
PUSHLOCAL5,/* - - LOC[5] */
PUSHLOCAL6,/* - - LOC[6] */
PUSHLOCAL7,/* - - LOC[7] */
-PUSHLOCAL8,/* - - LOC[8] */
-PUSHLOCAL9,/* - - LOC[9] */
-PUSHLOCAL,/* b - LOC[b] */
+GETGLOBAL,/* b - VAR[CNST[b]] */
GETGLOBAL0,/* - - VAR[CNST[0]] */
GETGLOBAL1,/* - - VAR[CNST[1]] */
GETGLOBAL2,/* - - VAR[CNST[2]] */
@@ -66,19 +63,19 @@ GETGLOBAL4,/* - - VAR[CNST[4]] */
GETGLOBAL5,/* - - VAR[CNST[5]] */
GETGLOBAL6,/* - - VAR[CNST[6]] */
GETGLOBAL7,/* - - VAR[CNST[7]] */
-GETGLOBAL8,/* - - VAR[CNST[8]] */
-GETGLOBAL9,/* - - VAR[CNST[9]] */
-GETGLOBALB,/* b - VAR[CNST[b]] */
-GETGLOBAL,/* w - VAR[CNST[w]] */
+GETGLOBALW,/* w - VAR[CNST[w]] */
GETTABLE,/* - i t t[i] */
-PUSHSELFB,/* b t t t[CNST[b]] */
-PUSHSELF,/* w t t t[CNST[w]] */
+PUSHSELF,/* b t t t[CNST[b]] */
+PUSHSELFW,/* w t t t[CNST[w]] */
-CREATEARRAYB,/* b - newarray(size = b) */
-CREATEARRAY,/* w - newarray(size = w) */
+CREATEARRAY,/* b - newarray(size = b) */
+CREATEARRAY0,/* - - newarray(size = 0) */
+CREATEARRAY1,/* - - newarray(size = 1) */
+CREATEARRAYW,/* w - newarray(size = w) */
+SETLOCAL,/* b x - LOC[b]=x */
SETLOCAL0,/* - x - LOC[0]=x */
SETLOCAL1,/* - x - LOC[1]=x */
SETLOCAL2,/* - x - LOC[2]=x */
@@ -87,19 +84,28 @@ SETLOCAL4,/* - x - LOC[4]=x */
SETLOCAL5,/* - x - LOC[5]=x */
SETLOCAL6,/* - x - LOC[6]=x */
SETLOCAL7,/* - x - LOC[7]=x */
-SETLOCAL8,/* - x - LOC[8]=x */
-SETLOCAL9,/* - x - LOC[9]=x */
-SETLOCAL,/* b x - LOC[b]=x */
-SETGLOBALB,/* b x - VAR[CNST[b]]=x */
-SETGLOBAL,/* w x - VAR[CNST[w]]=x */
+SETGLOBAL,/* b x - VAR[CNST[b]]=x */
+SETGLOBAL0,/* - x - VAR[CNST[0]]=x */
+SETGLOBAL1,/* - x - VAR[CNST[1]]=x */
+SETGLOBAL2,/* - x - VAR[CNST[2]]=x */
+SETGLOBAL3,/* - x - VAR[CNST[3]]=x */
+SETGLOBAL4,/* - x - VAR[CNST[4]]=x */
+SETGLOBAL5,/* - x - VAR[CNST[5]]=x */
+SETGLOBAL6,/* - x - VAR[CNST[6]]=x */
+SETGLOBAL7,/* - x - VAR[CNST[7]]=x */
+SETGLOBALW,/* w x - VAR[CNST[w]]=x */
SETTABLE0,/* - v i t - t[i]=v */
+
SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
-SETLIST0,/* b v_b...v_1 t - t[i]=v_i */
SETLIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
-SETMAP,/* b v_b k_b ...v_1 k_1 t t t[k_i]=v_i */
+SETLIST0,/* b v_b...v_1 t - t[i]=v_i */
+SETLISTW,/* w c v_b...v_1 t - t[i+c*FPF]=v_i */
+
+SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
+SETMAP0,/* - v_0 k_0 t t t[k_0]=v_0 */
EQOP,/* - y x (x==y)? 1 : nil */
NEQOP,/* - y x (x~=y)? 1 : nil */
@@ -117,25 +123,34 @@ MINUSOP,/* - x -x */
NOTOP,/* - x (x==nil)? 1 : nil */
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
+ONTJMPW,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
-JMPB,/* b - - PC+=b */
-JMP,/* w - - PC+=w */
-IFFJMPB,/* b x - (x==nil)? PC+=b */
-IFFJMP,/* w x - (x==nil)? PC+=w */
-IFTUPJMPB,/* b x - (x!=nil)? PC-=b */
-IFTUPJMP,/* w x - (x!=nil)? PC-=w */
-IFFUPJMPB,/* b x - (x==nil)? PC-=b */
-IFFUPJMP,/* w x - (x==nil)? PC-=w */
-
-CLOSUREB,/* b v_1...v_n c(CNST[b]) */
-CLOSURE,/* w v_1...v_n c(CNST[w]) */
-
-CALLFUNC,/* b c v_b...v_1 f r_c...r_1 f(v1,...,v_b) */
+ONFJMPW,/* w x (x==nil)? x : - (x==nil)? PC+=w */
+JMP,/* b - - PC+=b */
+JMPW,/* w - - PC+=w */
+IFFJMP,/* b x - (x==nil)? PC+=b */
+IFFJMPW,/* w x - (x==nil)? PC+=w */
+IFTUPJMP,/* b x - (x!=nil)? PC-=b */
+IFTUPJMPW,/* w x - (x!=nil)? PC-=w */
+IFFUPJMP,/* b x - (x==nil)? PC-=b */
+IFFUPJMPW,/* w x - (x==nil)? PC-=w */
+
+CLOSURE,/* b v_1...v_n c(CNST[b]) */
+CLOSUREW,/* w v_1...v_n c(CNST[w]) */
+
+CALLFUNC,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
+CALLFUNC0,/* b v_b...v_1 f - f(v1,...,v_b) */
+CALLFUNC1,/* b v_b...v_1 f r_1 f(v1,...,v_b) */
+
RETCODE,/* b - - */
-SETLINE,/* w - - LINE=w */
-POP1,/* - - - TOP-=1 */
-POP2,/* - - - TOP-=2 */
-POPS,/* b - - TOP-=b */
+
+SETLINE,/* b - - LINE=b */
+SETLINEW,/* w - - LINE=w */
+
+POP,/* b - - TOP-=(b+1) */
+POP0,/* - - - TOP-=1 */
+POP1,/* - - - TOP-=2 */
+
ARGS,/* b - - TOP=BASE+b */
VARARGS/* b v_x...v_1 {v_1...v_x;n=x} TOP=BASE+b+1 */
} OpCode;
diff --git a/lua.stx b/lua.stx
@@ -1,6 +1,6 @@
%{
/*
-** $Id: lua.stx,v 1.7 1997/10/01 20:05:34 roberto Exp roberto $
+** $Id: lua.stx,v 1.8 1997/10/06 14:51:11 roberto Exp roberto $
** Syntax analizer and code generator
** See Copyright Notice in lua.h
*/
@@ -23,9 +23,6 @@
/* to avoid warnings generated by yacc */
int luaY_parse (void);
-#define malloc luaM_malloc
-#define realloc luaM_realloc
-#define free luaM_free
/* size of a "normal" jump instruction: OpCode + 1 byte */
@@ -118,23 +115,6 @@ static void code_byte (Byte c)
}
-static void code_word_at (int pc, int n)
-{
- if (n > MAX_WORD)
- luaY_error("construction too big; unable to compile");
- currState->f->code[pc] = n&0xFF;
- currState->f->code[pc+1] = n>>8;
-}
-
-
-static void code_word (int n)
-{
- check_pc(2);
- currState->pc += 2;
- code_word_at(currState->pc-2, n);
-}
-
-
static void deltastack (int delta)
{
currState->stacksize += delta;
@@ -146,41 +126,55 @@ static void deltastack (int delta)
}
-static void code_opcode (OpCode op, int delta)
+static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
{
- code_byte(op);
deltastack(delta);
+ if (arg < builtin) {
+ currState->f->code[pc] = op+1+arg;
+ return 1;
+ }
+ else if (arg <= 255) {
+ currState->f->code[pc] = op;
+ currState->f->code[pc+1] = arg;
+ return 2;
+ }
+ else if (arg <= MAX_WORD) {
+ currState->f->code[pc] = op+1+builtin;
+ currState->f->code[pc+1] = arg&0xFF;
+ currState->f->code[pc+2] = arg>>8;
+ return 3;
+ }
+ else luaY_error("construction too big - unable to compile");
+ return 0; /* to avoid warnings */
}
-static void code_opb (OpCode opbyte, int arg, int delta)
+static int fix_opcode (int pc, OpCode op, int builtin, int arg)
{
- code_opcode(opbyte, delta);
- code_byte(arg);
-}
-
-static void code_opw (OpCode opbyte, int arg, int delta)
-{
- code_opcode(opbyte, delta);
- code_word(arg);
+ if (arg < builtin) { /* close space */
+ movecode_down(pc+1, pc+2, currState->pc-(pc+2));
+ currState->pc--;
+ }
+ else if (arg > 255) { /* open space */
+ check_pc(1);
+ movecode_up(pc+1, pc, currState->pc-pc);
+ currState->pc++;
+ }
+ return code_oparg_at(pc, op, builtin, arg, 0) - 2;
}
-static void code_opborw (OpCode opbyte, int arg, int delta)
+static void code_oparg (OpCode op, int builtin, int arg, int delta)
{
- if (arg <= 255)
- code_opb(opbyte, arg, delta);
- else
- code_opw(opbyte+1, arg, delta);
+ check_pc(3); /* maximum code size */
+ currState->pc += code_oparg_at(currState->pc, op, builtin, arg, delta);
}
-static void code_oparg (OpCode firstop, OpCode opbyte, int arg, int delta)
+static void code_opcode (OpCode op, int delta)
{
- if (firstop+arg < opbyte)
- code_opcode(firstop+arg, delta);
- else
- code_opborw(opbyte, arg, delta);
+ deltastack(delta);
+ code_byte(op);
}
@@ -193,7 +187,10 @@ static void code_pop (OpCode op)
#define code_binop(op) code_pop(op)
-#define code_neutralop(op) code_byte(op)
+static void code_neutralop (OpCode op)
+{
+ code_opcode(op, 0);
+}
/* unary operations get 1 argument and leave one, so they are neutral */
#define code_unop(op) code_neutralop(op)
@@ -201,7 +198,7 @@ static void code_pop (OpCode op)
static void code_constant (int c)
{
- code_oparg(PUSHCONSTANT0, PUSHCONSTANTB, c, 1);
+ code_oparg(PUSHCONSTANT, 8, c, 1);
}
@@ -237,7 +234,7 @@ static void code_string (TaggedString *s)
}
-#define LIM 13
+#define LIM 20
static int real_constant (real r)
{
/* check whether 'r' has appeared within the last LIM entries */
@@ -261,7 +258,7 @@ static void code_number (real f)
{
Word i;
if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f)
- code_oparg(PUSH0, PUSHBYTE, i, 1); /* f has an (short) integer value */
+ code_oparg(PUSHNUMBER, 3, i, 1); /* f has an (short) integer value */
else
code_constant(real_constant(f));
}
@@ -270,18 +267,13 @@ static void code_number (real f)
static void flush_record (int n)
{
if (n > 0)
- code_opb(SETMAP, n, -2*n);
+ code_oparg(SETMAP, 1, n-1, -2*n);
}
static void flush_list (int m, int n)
{
if (n == 0) return;
- if (m == 0)
- code_opcode(SETLIST0, -n);
- else if (m < 255)
- code_opb(SETLIST, m, -n);
- else
- luaY_error("list constructor too long");
+ code_oparg(SETLIST, 1, m, -n);
code_byte(n);
}
@@ -375,7 +367,7 @@ static void pushupvalue (TaggedString *n)
if (aux_localname(n, currState) >= 0)
luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
i = indexupvalue(n);
- code_oparg(PUSHUPVALUE0, PUSHUPVALUE, i, 1);
+ code_oparg(PUSHUPVALUE, 2, i, 1);
}
@@ -383,7 +375,7 @@ void luaY_codedebugline (int line)
{
static int lastline = 0;
if (lua_debug && line != lastline) {
- code_opw(SETLINE, line, 0);
+ code_oparg(SETLINE, 0, line, 0);
lastline = line;
}
}
@@ -392,22 +384,25 @@ void luaY_codedebugline (int line)
static void adjuststack (int n)
{
if (n > 0)
- code_oparg(POP1-1, POPS, n, -n); /* POP1-1 = POP0 */
+ code_oparg(POP, 2, n-1, -n);
else if (n < 0)
- code_oparg(PUSHNIL-1, PUSHNILS, -n, -n); /* PUSHNIL1-1 = PUSHNIL0 */
+ code_oparg(PUSHNIL, 1, (-n)-1, -n);
}
-static long adjust_functioncall (long exp, int i)
+static long adjust_functioncall (long exp, int nresults)
{
if (exp <= 0)
return -exp; /* exp is -list length */
else {
int temp = currState->f->code[exp];
- currState->f->code[exp] = i;
- if (i != MULT_RET)
- deltastack(i);
- return temp+i;
+ int nparams = currState->f->code[exp-1];
+ exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
+ currState->f->code[exp] = nparams;
+ if (nresults != MULT_RET)
+ deltastack(nresults);
+ deltastack(-(nparams+1));
+ return temp+nresults;
}
}
@@ -430,9 +425,9 @@ static void adjust_mult_assign (int vars, long exps)
static void code_args (int dots)
{
if (!dots)
- code_opb(ARGS, currState->nlocalvar, currState->nlocalvar);
+ code_oparg(ARGS, 0, currState->nlocalvar, currState->nlocalvar);
else {
- code_opb(VARARGS, currState->nlocalvar, currState->nlocalvar+1);
+ code_oparg(VARARGS, 0, currState->nlocalvar, currState->nlocalvar+1);
add_localvar(luaS_new("arg"));
}
}
@@ -441,9 +436,9 @@ static void code_args (int dots)
static void lua_pushvar (vardesc number)
{
if (number > 0) /* global var */
- code_oparg(GETGLOBAL0, GETGLOBALB, number-1, 1);
+ code_oparg(GETGLOBAL, 8, number-1, 1);
else if (number < 0) /* local var */
- code_oparg(PUSHLOCAL0, PUSHLOCAL, (-number)-1, 1);
+ code_oparg(PUSHLOCAL, 8, (-number)-1, 1);
else
code_pop(GETTABLE);
}
@@ -454,9 +449,9 @@ static void storevar (vardesc number)
if (number == 0) /* indexed var */
code_opcode(SETTABLE0, -3);
else if (number > 0) /* global var */
- code_opborw(SETGLOBALB, number-1, -1);
+ code_oparg(SETGLOBAL, 8, number-1, -1);
else /* number < 0 - local var */
- code_oparg(SETLOCAL0, SETLOCAL, (-number)-1, -1);
+ code_oparg(SETLOCAL, 8, (-number)-1, -1);
}
@@ -469,35 +464,16 @@ static int lua_codestore (int i, int left)
return left;
}
else { /* indexed var with values in between*/
- code_pop(SETTABLE);
- code_byte(left+i); /* number of elements between table/index and value */
+ code_oparg(SETTABLE, 0, left+i, -1);
return left+2; /* table/index are not poped, since they are not on top */
}
}
-static int fix_opcode (int pc, OpCode op, int n)
-{
- if (n <= 255) {
- currState->f->code[pc] = op;
- currState->f->code[pc+1] = n;
- return 0;
- }
- else {
- check_pc(1); /* open space */
- movecode_up(pc+1, pc, currState->pc-pc);
- currState->pc++;
- currState->f->code[pc] = op+1; /* opcode must be word variant */
- code_word_at(pc+1, n);
- return 1;
- }
-}
-
-
static int fix_jump (int pc, OpCode op, int n)
{
/* jump is relative to position following jump instruction */
- return fix_opcode(pc, op, n-(pc+JMPSIZE));
+ return fix_opcode(pc, op, 0, n-(pc+JMPSIZE));
}
@@ -505,7 +481,7 @@ static void fix_upjmp (OpCode op, int pos)
{
int delta = currState->pc+JMPSIZE - pos; /* jump is relative */
if (delta > 255) delta++;
- code_opborw(op, delta, 0);
+ code_oparg(op, 0, delta, 0);
}
@@ -517,25 +493,20 @@ static void codeIf (int thenAdd, int elseAdd)
elseinit = currState->pc;
}
else
- elseinit += fix_jump(elseAdd, JMPB, currState->pc);
- fix_jump(thenAdd, IFFJMPB, elseinit);
+ elseinit += fix_jump(elseAdd, JMP, currState->pc);
+ fix_jump(thenAdd, IFFJMP, elseinit);
}
-static void code_shortcircuit (OpCode op, int pos)
+static void code_shortcircuit (int pc, OpCode op)
{
- int dist = currState->pc - (pos+JMPSIZE);
- if (dist > 255)
- luaY_error("and/or expression too long");
- currState->f->code[pos] = op;
- currState->f->code[pos+1] = dist;
+ fix_jump(pc, op, currState->pc);
}
static void codereturn (void)
{
- code_neutralop(RETCODE);
- code_byte(currState->nlocalvar);
+ code_oparg(RETCODE, 0, currState->nlocalvar, 0);
currState->stacksize = currState->nlocalvar;
}
@@ -549,7 +520,7 @@ static void func_onstack (TProtoFunc *f)
currState->f->consts[c].value.tf = (currState+1)->f;
for (i=0; i<nupvalues; i++)
lua_pushvar((currState+1)->upvalues[i]);
- code_opborw(CLOSUREB, c, 1-nupvalues);
+ code_oparg(CLOSURE, 0, c, 1-nupvalues);
}
@@ -585,7 +556,6 @@ static void init_func (void)
currState->f->lineDefined = luaX_linenumber;
}
-
static TProtoFunc *close_func (void)
{
TProtoFunc *f = currState->f;
@@ -687,13 +657,13 @@ stat : IF cond THEN block SaveWord elsepart END
memcpy(&currState->f->code[currState->pc],
&currState->f->code[$2], expsize);
movecode_down($2, $3, currState->pc-$2);
- newpos += fix_jump($2, JMPB, currState->pc-expsize);
- fix_upjmp(IFTUPJMPB, newpos);
+ newpos += fix_jump($2, JMP, currState->pc-expsize);
+ fix_upjmp(IFTUPJMP, newpos);
}}
| REPEAT GetPC block UNTIL expr1
{
- fix_upjmp(IFFUPJMPB, $2);
+ fix_upjmp(IFFUPJMP, $2);
deltastack(-1); /* pops condition */
}
@@ -706,7 +676,7 @@ stat : IF cond THEN block SaveWord elsepart END
left = lua_codestore(i, left);
adjuststack(left); /* remove eventual 'garbage' left on stack */
}}
- | functioncall
+ | functioncall { adjust_functioncall($1, 0); }
| LOCAL localdeclist decinit
{
currState->nlocalvar += $2;
@@ -757,7 +727,11 @@ ret : /* empty */
GetPC : /* empty */ { $$ = currState->pc; }
;
-SaveWord : GetPC { $$ = $1; code_word(0); /* open space */ }
+SaveWord : /* empty */
+ { $$ = currState->pc;
+ check_pc(JMPSIZE);
+ currState->pc += JMPSIZE; /* open space */
+ }
;
SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ }
@@ -787,33 +761,33 @@ expr : '(' expr ')' { $$ = $2; }
| expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
| '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
| NOT expr1 { code_unop(NOTOP); $$ = 0;}
- | table { $$ = 0; }
+ | table { $$ = 0; }
| varexp { $$ = 0;}
| NUMBER { code_number($1); $$ = 0; }
| STRING { code_string($1); $$ = 0; }
- | NIL {code_opcode(PUSHNIL, 1); $$ = 0; }
+ | NIL { adjuststack(-1); $$ = 0; }
| functioncall { $$ = $1; }
- | FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
- | expr1 AND SaveWordPop expr1 { code_shortcircuit(ONFJMP, $3); $$ = 0; }
- | expr1 OR SaveWordPop expr1 { code_shortcircuit(ONTJMP, $3); $$ = 0; }
+ | FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
+ | expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
+ | expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
;
-table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAYB, $3); }
+table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); }
;
functioncall : funcvalue funcParams
{
- code_opcode(CALLFUNC, -($1+$2+1)); /* ajdust counts results */
- code_byte($1+$2);
+ code_byte(0); /* save space for opcode */
+ code_byte($1+$2); /* number of parameters */
$$ = currState->pc;
- code_byte(0); /* may be adjusted by other rules */
+ code_byte(0); /* must be adjusted by other rules */
}
;
funcvalue : varexp { $$ = 0; }
| varexp ':' NAME
{
- code_opborw(PUSHSELFB, string_constant($3, currState), 1);
+ code_oparg(PUSHSELF, 0, string_constant($3, currState), 1);
$$ = 1;
}
;
diff --git a/lvm.c b/lvm.c
@@ -1,5 +1,5 @@
/*
-** $Id: lvm.c,v 1.7 1997/10/01 20:05:34 roberto Exp roberto $
+** $Id: lvm.c,v 1.8 1997/10/06 14:51:11 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -284,25 +284,25 @@ StkId luaV_execute (Closure *cl, StkId base)
int aux;
switch ((OpCode)(aux = *pc++)) {
- case PUSHNIL:
+ case PUSHNIL0:
ttype(luaD_stack.top++) = LUA_T_NIL;
break;
- case PUSHNILS: {
- int n = *pc++;
- while (n--)
+ case PUSHNIL:
+ aux = *pc++;
+ do {
ttype(luaD_stack.top++) = LUA_T_NIL;
+ } while (aux--);
break;
- }
- case PUSHBYTE:
+ case PUSHNUMBER:
aux = *pc++; goto pushnumber;
- case PUSHWORD:
+ case PUSHNUMBERW:
aux = next_word(pc); goto pushnumber;
- case PUSH0: case PUSH1: case PUSH2:
- aux -= PUSH0;
+ case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
+ aux -= PUSHNUMBER0;
pushnumber:
ttype(luaD_stack.top) = LUA_T_NUMBER;
nvalue(luaD_stack.top) = aux;
@@ -312,25 +312,21 @@ StkId luaV_execute (Closure *cl, StkId base)
case PUSHLOCAL:
aux = *pc++; goto pushlocal;
- case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
- case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
- case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
- case PUSHLOCAL9:
+ case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
+ case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
aux -= PUSHLOCAL0;
pushlocal:
*luaD_stack.top++ = *((luaD_stack.stack+base) + aux);
break;
- case GETGLOBAL:
+ case GETGLOBALW:
aux = next_word(pc); goto getglobal;
- case GETGLOBALB:
+ case GETGLOBAL:
aux = *pc++; goto getglobal;
- case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2:
- case GETGLOBAL3: case GETGLOBAL4: case GETGLOBAL5:
- case GETGLOBAL6: case GETGLOBAL7: case GETGLOBAL8:
- case GETGLOBAL9:
+ case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2: case GETGLOBAL3:
+ case GETGLOBAL4: case GETGLOBAL5: case GETGLOBAL6: case GETGLOBAL7:
aux -= GETGLOBAL0;
getglobal:
luaV_getglobal(tsvalue(&consts[aux]));
@@ -340,10 +336,10 @@ StkId luaV_execute (Closure *cl, StkId base)
luaV_gettable();
break;
- case PUSHSELF:
+ case PUSHSELFW:
aux = next_word(pc); goto pushself;
- case PUSHSELFB:
+ case PUSHSELF:
aux = *pc++;
pushself: {
TObject receiver = *(luaD_stack.top-1);
@@ -353,16 +349,15 @@ StkId luaV_execute (Closure *cl, StkId base)
break;
}
- case PUSHCONSTANT:
+ case PUSHCONSTANTW:
aux = next_word(pc); goto pushconstant;
- case PUSHCONSTANTB:
+ case PUSHCONSTANT:
aux = *pc++; goto pushconstant;
case PUSHCONSTANT0: case PUSHCONSTANT1: case PUSHCONSTANT2:
case PUSHCONSTANT3: case PUSHCONSTANT4: case PUSHCONSTANT5:
- case PUSHCONSTANT6: case PUSHCONSTANT7: case PUSHCONSTANT8:
- case PUSHCONSTANT9:
+ case PUSHCONSTANT6: case PUSHCONSTANT7:
aux -= PUSHCONSTANT0;
pushconstant:
*luaD_stack.top++ = consts[aux];
@@ -380,20 +375,22 @@ StkId luaV_execute (Closure *cl, StkId base)
case SETLOCAL:
aux = *pc++; goto setlocal;
- case SETLOCAL0: case SETLOCAL1: case SETLOCAL2:
- case SETLOCAL3: case SETLOCAL4: case SETLOCAL5:
- case SETLOCAL6: case SETLOCAL7: case SETLOCAL8:
- case SETLOCAL9:
+ case SETLOCAL0: case SETLOCAL1: case SETLOCAL2: case SETLOCAL3:
+ case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
aux -= SETLOCAL0;
setlocal:
*((luaD_stack.stack+base) + aux) = *(--luaD_stack.top);
break;
- case SETGLOBAL:
+ case SETGLOBALW:
aux = next_word(pc); goto setglobal;
- case SETGLOBALB:
- aux = *pc++;
+ case SETGLOBAL:
+ aux = *pc++; goto setglobal;
+
+ case SETGLOBAL0: case SETGLOBAL1: case SETGLOBAL2: case SETGLOBAL3:
+ case SETGLOBAL4: case SETGLOBAL5: case SETGLOBAL6: case SETGLOBAL7:
+ aux -= SETGLOBAL0;
setglobal:
luaV_setglobal(tsvalue(&consts[aux]));
break;
@@ -406,6 +403,9 @@ StkId luaV_execute (Closure *cl, StkId base)
luaV_settable(luaD_stack.top-3-(*pc++), 2);
break;
+ case SETLISTW:
+ aux = next_word(pc); aux *= LFIELDS_PER_FLUSH; goto setlist;
+
case SETLIST:
aux = *(pc++) * LFIELDS_PER_FLUSH; goto setlist;
@@ -423,23 +423,27 @@ StkId luaV_execute (Closure *cl, StkId base)
break;
}
- case SETMAP: {
- int n = *(pc++);
- TObject *arr = luaD_stack.top-(2*n)-1;
- while (n--) {
+ case SETMAP0:
+ aux = 0; goto setmap;
+
+ case SETMAP:
+ aux = *(pc++);
+ setmap: {
+ TObject *arr = luaD_stack.top-(2*aux)-3;
+ do {
*(luaH_set (avalue(arr), luaD_stack.top-2)) = *(luaD_stack.top-1);
luaD_stack.top-=2;
- }
+ } while (aux--);
break;
}
- case POPS:
+ case POP:
aux = *pc++; goto pop;
- case POP1: case POP2:
- aux -= (POP1-1);
+ case POP0: case POP1:
+ aux -= POP0;
pop:
- luaD_stack.top -= aux;
+ luaD_stack.top -= (aux+1);
break;
case ARGS:
@@ -451,10 +455,13 @@ StkId luaV_execute (Closure *cl, StkId base)
adjust_varargs(base + *(pc++));
break;
- case CREATEARRAY:
+ case CREATEARRAYW:
aux = next_word(pc); goto createarray;
- case CREATEARRAYB:
+ case CREATEARRAY0: case CREATEARRAY1:
+ aux -= CREATEARRAY0; goto createarray;
+
+ case CREATEARRAY:
aux = *pc++;
createarray:
luaC_checkGC();
@@ -569,58 +576,66 @@ StkId luaV_execute (Closure *cl, StkId base)
nvalue(luaD_stack.top-1) = 1;
break;
+ case ONTJMPW:
+ aux = next_word(pc); goto ontjmp;
+
case ONTJMP:
aux = *pc++;
+ ontjmp:
if (ttype(luaD_stack.top-1) != LUA_T_NIL) pc += aux;
else luaD_stack.top--;
break;
+ case ONFJMPW:
+ aux = next_word(pc); goto onfjmp;
+
case ONFJMP:
aux = *pc++;
+ onfjmp:
if (ttype(luaD_stack.top-1) == LUA_T_NIL) pc += aux;
else luaD_stack.top--;
break;
- case JMP:
+ case JMPW:
aux = next_word(pc); goto jmp;
- case JMPB:
+ case JMP:
aux = *pc++;
jmp:
pc += aux;
break;
- case IFFJMP:
+ case IFFJMPW:
aux = next_word(pc); goto iffjmp;
- case IFFJMPB:
+ case IFFJMP:
aux = *pc++;
iffjmp:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += aux;
break;
- case IFTUPJMP:
+ case IFTUPJMPW:
aux = next_word(pc); goto iftupjmp;
- case IFTUPJMPB:
+ case IFTUPJMP:
aux = *pc++;
iftupjmp:
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= aux;
break;
- case IFFUPJMP:
+ case IFFUPJMPW:
aux = next_word(pc); goto iffupjmp;
- case IFFUPJMPB:
+ case IFFUPJMP:
aux = *pc++;
iffupjmp:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= aux;
break;
- case CLOSURE:
+ case CLOSUREW:
aux = next_word(pc); goto closure;
- case CLOSUREB:
+ case CLOSURE:
aux = *pc++;
closure:
*luaD_stack.top++ = consts[aux];
@@ -628,9 +643,14 @@ StkId luaV_execute (Closure *cl, StkId base)
luaC_checkGC();
break;
- case CALLFUNC: {
+ case CALLFUNC:
+ aux = *pc++; goto callfunc;
+
+ case CALLFUNC0: case CALLFUNC1:
+ aux -= CALLFUNC0;
+ callfunc: {
StkId newBase = (luaD_stack.top-luaD_stack.stack)-(*pc++);
- luaD_call(newBase, *pc++);
+ luaD_call(newBase, aux);
break;
}
@@ -642,19 +662,22 @@ StkId luaV_execute (Closure *cl, StkId base)
luaD_callHook(base, LUA_T_MARK, 1);
return (base + ((aux==RETCODE) ? *pc : 0));
- case SETLINE: {
- int line = next_word(pc);
+ case SETLINEW:
+ aux = next_word(pc); goto setline;
+
+ case SETLINE:
+ aux = *pc++;
+ setline:
if ((luaD_stack.stack+base-1)->ttype != LUA_T_LINE) {
/* open space for LINE value */
luaD_openstack((luaD_stack.top-luaD_stack.stack)-base);
base++;
(luaD_stack.stack+base-1)->ttype = LUA_T_LINE;
}
- (luaD_stack.stack+base-1)->value.i = line;
+ (luaD_stack.stack+base-1)->value.i = aux;
if (lua_linehook)
- luaD_lineHook(line);
+ luaD_lineHook(aux);
break;
- }
#ifdef DEBUG
default: