From 4e7e9e8de5b9e50694e161653ef03b5c15a53054 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 4 Apr 2000 17:48:44 -0300 Subject: [PATCH] new opcode INCLOCAL. --- lcode.c | 89 +++++++++++++++++++++++++++++++++++------------------- llimits.h | 6 ++-- lopcodes.h | 77 ++++++++++++++++++++++++++-------------------- lvm.c | 41 +++++++++++++++++-------- 4 files changed, 132 insertions(+), 81 deletions(-) diff --git a/lcode.c b/lcode.c index d507e179..137cfcdc 100644 --- a/lcode.c +++ b/lcode.c @@ -1,10 +1,12 @@ /* -** $Id: lcode.c,v 1.17 2000/03/24 12:18:30 roberto Exp roberto $ +** $Id: lcode.c,v 1.18 2000/03/24 17:26:08 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ +#include "stdlib.h" + #define LUA_REENTRANT #include "lcode.h" @@ -73,11 +75,12 @@ static void setprevious (FuncState *fs, Instruction i) { static void luaK_minus (FuncState *fs) { + /* PUSHINT s; MINUS -> PUSHINT -s (-k) */ + /* PUSHNUM u; MINUS -> PUSHNEGNUM u (-k) */ Instruction previous = prepare(fs, CREATE_0(OP_MINUS), 0); switch(GET_OPCODE(previous)) { case OP_PUSHINT: SETARG_S(previous, -GETARG_S(previous)); break; case OP_PUSHNUM: SET_OPCODE(previous, OP_PUSHNEGNUM); break; - case OP_PUSHNEGNUM: SET_OPCODE(previous, OP_PUSHNUM); break; default: return; } setprevious(fs, previous); @@ -85,6 +88,7 @@ static void luaK_minus (FuncState *fs) { static void luaK_gettable (FuncState *fs) { + /* PUSHSTRING u; GETTABLE -> GETDOTTED u (t.x) */ Instruction previous = prepare(fs, CREATE_0(OP_GETTABLE), -1); switch(GET_OPCODE(previous)) { case OP_PUSHSTRING: SET_OPCODE(previous, OP_GETDOTTED); break; @@ -95,6 +99,7 @@ static void luaK_gettable (FuncState *fs) { static void luaK_add (FuncState *fs) { + /* PUSHINT s; ADD -> ADDI s (a+k) */ Instruction previous = prepare(fs, CREATE_0(OP_ADD), -1); switch(GET_OPCODE(previous)) { case OP_PUSHINT: SET_OPCODE(previous, OP_ADDI); break; @@ -105,6 +110,7 @@ static void luaK_add (FuncState *fs) { static void luaK_sub (FuncState *fs) { + /* PUSHINT s; SUB -> ADDI -s (a-k) */ Instruction previous = prepare(fs, CREATE_0(OP_SUB), -1); switch(GET_OPCODE(previous)) { case OP_PUSHINT: @@ -118,6 +124,7 @@ static void luaK_sub (FuncState *fs) { static void luaK_conc (FuncState *fs) { + /* CONC u; CONC 2 -> CONC u+1 (a..b..c) */ Instruction previous = prepare(fs, CREATE_U(OP_CONC, 2), -1); switch(GET_OPCODE(previous)) { case OP_CONC: SETARG_U(previous, GETARG_U(previous)+1); break; @@ -127,8 +134,27 @@ static void luaK_conc (FuncState *fs) { } +static void luaK_setlocal (FuncState *fs, int l) { + /* PUSHLOCAL l; ADDI k, SETLOCAL l -> INCLOCAL k, l ((local)a=a+k) */ + Instruction *code = fs->f->code; + int pc = fs->pc; + if (pc-1 > fs->lasttarget && /* no jumps in-between instructions? */ + code[pc-2] == CREATE_U(OP_PUSHLOCAL, l) && + GET_OPCODE(code[pc-1]) == OP_ADDI && + abs(GETARG_S(code[pc-1])) <= MAXARG_sA) { + int inc = GETARG_S(code[pc-1]); + fs->pc = pc-1; + code[pc-2] = CREATE_sAB(OP_INCLOCAL, inc, l); + luaK_deltastack(fs, -1); + } + else + luaK_U(fs, OP_SETLOCAL, l, -1); +} + + static void luaK_eq (FuncState *fs) { - Instruction previous = prepare(fs, CREATE_S(OP_IFEQJMP, 0), -2); + /* PUSHNIL 1; JMPEQ -> NOT (a==nil) */ + Instruction previous = prepare(fs, CREATE_S(OP_JMPEQ, 0), -2); if (previous == CREATE_U(OP_PUSHNIL, 1)) { setprevious(fs, CREATE_0(OP_NOT)); luaK_deltastack(fs, 1); /* undo delta from `prepare' */ @@ -137,9 +163,10 @@ static void luaK_eq (FuncState *fs) { static void luaK_neq (FuncState *fs) { - Instruction previous = prepare(fs, CREATE_S(OP_IFNEQJMP, 0), -2); + /* PUSHNIL 1; JMPNEQ -> JMPT (a~=nil) */ + Instruction previous = prepare(fs, CREATE_S(OP_JMPNEQ, 0), -2); if (previous == CREATE_U(OP_PUSHNIL, 1)) { - setprevious(fs, CREATE_S(OP_IFTJMP, 0)); + setprevious(fs, CREATE_S(OP_JMPT, 0)); } } @@ -171,7 +198,7 @@ void luaK_fixjump (FuncState *fs, int pc, int dest) { SETARG_S(*jmp, 0); /* absolute value to represent end of list */ else { /* jump is relative to position following jump instruction */ int offset = dest-(pc+1); - if (offset < -MAXARG_S || offset > MAXARG_S) + if (abs(offset) > MAXARG_S) luaK_error(fs->ls, "control structure too long"); SETARG_S(*jmp, offset); } @@ -298,7 +325,7 @@ void luaK_storevar (LexState *ls, const expdesc *var) { FuncState *fs = ls->fs; switch (var->k) { case VLOCAL: - luaK_U(fs, OP_SETLOCAL, var->u.index, -1); + luaK_setlocal(fs, var->u.index); break; case VGLOBAL: luaK_U(fs, OP_SETGLOBAL, var->u.index, -1); @@ -315,14 +342,14 @@ void luaK_storevar (LexState *ls, const expdesc *var) { static OpCode invertjump (OpCode op) { switch (op) { - case OP_IFNEQJMP: return OP_IFEQJMP; - case OP_IFEQJMP: return OP_IFNEQJMP; - case OP_IFLTJMP: return OP_IFGEJMP; - case OP_IFLEJMP: return OP_IFGTJMP; - case OP_IFGTJMP: return OP_IFLEJMP; - case OP_IFGEJMP: return OP_IFLTJMP; - case OP_IFTJMP: case OP_ONTJMP: return OP_IFFJMP; - case OP_IFFJMP: case OP_ONFJMP: return OP_IFTJMP; + case OP_JMPNEQ: return OP_JMPEQ; + case OP_JMPEQ: return OP_JMPNEQ; + case OP_JMPLT: return OP_JMPGE; + case OP_JMPLE: return OP_JMPGT; + case OP_JMPGT: return OP_JMPLE; + case OP_JMPGE: return OP_JMPLT; + case OP_JMPT: case OP_JMPONT: return OP_JMPF; + case OP_JMPF: case OP_JMPONF: return OP_JMPT; default: LUA_INTERNALERROR(NULL, "invalid jump instruction"); return OP_END; /* to avoid warnings */ @@ -334,8 +361,8 @@ static void luaK_jump (FuncState *fs, OpCode jump) { Instruction previous = prepare(fs, CREATE_S(jump, 0), -1); switch (GET_OPCODE(previous)) { case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break; - case OP_PUSHINT: - if (jump == OP_IFTJMP) { + case OP_PUSHNIL: /* optimize `repeat until nil' */ + if (GETARG_U(previous) == 1 && jump == OP_JMPF) { previous = CREATE_S(OP_JMP, 0); break; } @@ -364,10 +391,10 @@ static void luaK_patchlistaux (FuncState *fs, int list, int target, luaK_fixjump(fs, list, special_target); else { luaK_fixjump(fs, list, target); /* do the patch */ - if (op == OP_ONTJMP) /* remove eventual values */ - SET_OPCODE(*i, OP_IFTJMP); - else if (op == OP_ONFJMP) - SET_OPCODE(*i, OP_IFFJMP); + if (op == OP_JMPONT) /* remove eventual values */ + SET_OPCODE(*i, OP_JMPT); + else if (op == OP_JMPONF) + SET_OPCODE(*i, OP_JMPF); } list = next; } @@ -427,12 +454,12 @@ static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) { void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) { - luaK_testgo(fs, v, 1, keepvalue ? OP_ONFJMP : OP_IFFJMP); + luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF); } void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) { - luaK_testgo(fs, v, 0, keepvalue ? OP_ONTJMP : OP_IFTJMP); + luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT); } @@ -456,8 +483,8 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { p_1 = luaK_S(fs, OP_PUSHINT, 1, 1); } else { /* still may need a PUSHNIL or a PUSHINT */ - 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_nil = need_value(fs, v->u.l.f, OP_JMPONF); + int need_1 = need_value(fs, v->u.l.t, OP_JMPONT); if (need_nil && need_1) { luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */ p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); @@ -472,8 +499,8 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { } } final = luaK_getlabel(fs); - luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final); - luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final); + luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final); + luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final); v->u.l.f = v->u.l.t = NO_JUMP; } } @@ -536,10 +563,10 @@ void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { case TK_CONC: luaK_conc(fs); break; case TK_EQ: luaK_eq(fs); break; case TK_NE: luaK_neq(fs); break; - case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break; - case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break; - case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break; - case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break; + case '>': luaK_S(fs, OP_JMPGT, 0, -2); break; + case '<': luaK_S(fs, OP_JMPLT, 0, -2); break; + case TK_GE: luaK_S(fs, OP_JMPGE, 0, -2); break; + case TK_LE: luaK_S(fs, OP_JMPLE, 0, -2); break; } } } diff --git a/llimits.h b/llimits.h index ea2298f7..06421ead 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.3 2000/03/27 20:08:33 roberto Exp roberto $ +** $Id: llimits.h,v 1.4 2000/03/31 16:28:45 roberto Exp roberto $ ** Limits, basic types, and some other "instalation-dependent" definitions ** See Copyright Notice in lua.h */ @@ -69,14 +69,12 @@ typedef unsigned long Instruction; #define SIZE_U (SIZE_INSTRUCTION-SIZE_OP) #define POS_U SIZE_OP -#define SIZE_S (SIZE_INSTRUCTION-SIZE_OP) -#define POS_S SIZE_OP #define POS_B SIZE_OP #define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B)) #define POS_A (SIZE_OP+SIZE_B) #define MAXARG_U ((1<>1) /* `S' is signed */ #define MAXARG_A ((1<>1) /* max value for a signed A */ /* creates a mask with `n' 1 bits at position `p' */ @@ -45,27 +49,33 @@ ** the following macros help to manipulate instructions */ +#define CREATE_0(o) ((Instruction)(o)) #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0))) -#define GETARG_U(i) ((int)((i)>>POS_U)) -#define GETARG_S(i) ((int)((i)>>POS_S)-EXCESS_S) -#define GETARG_A(i) ((int)((i)>>POS_A)) -#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0))) - #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))) + +#define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<>POS_U)) #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \ ((Instruction)(u)<>POS_A)) #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ ((Instruction)(a)<>POS_B) & MASK1(SIZE_B,0))) #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ ((Instruction)(b)<y)? PC+=s */ -OP_IFGEJMP,/* J y x - (x>=y)? PC+=s */ - -OP_IFTJMP,/* J x - (x!=nil)? PC+=s */ -OP_IFFJMP,/* J x - (x==nil)? PC+=s */ -OP_ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */ -OP_ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */ +OP_JMPNEQ,/* J y x - (x~=y)? PC+=s */ +OP_JMPEQ,/* J y x - (x==y)? PC+=s */ +OP_JMPLT,/* J y x - (xy)? PC+=s */ +OP_JMPGE,/* J y x - (x>=y)? PC+=s */ + +OP_JMPT,/* J x - (x!=nil)? PC+=s */ +OP_JMPF,/* J x - (x==nil)? PC+=s */ +OP_JMPONT,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */ +OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */ OP_JMP,/* J - - PC+=s */ OP_PUSHNILJMP,/* - - nil PC++; */ @@ -145,7 +156,7 @@ OP_SETLINE/* U - - LINE=u */ -#define ISJUMP(o) (OP_IFNEQJMP <= (o) && (o) <= OP_JMP) +#define ISJUMP(o) (OP_JMPNEQ <= (o) && (o) <= OP_JMP) #endif diff --git a/lvm.c b/lvm.c index 96159c0c..2a8e2484 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.97 2000/03/27 20:10:21 roberto Exp roberto $ +** $Id: lvm.c,v 1.98 2000/03/29 20:19:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -365,9 +365,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { case OP_PUSHNIL: { int n = GETARG_U(i); LUA_ASSERT(L, n>0, "invalid argument"); - do { + do { ttype(top++) = TAG_NIL; - } while (--n > 0); + } while (--n > 0); break; } @@ -491,6 +491,21 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { top--; break; + case OP_INCLOCAL: { + TObject *var = base+GETARG_B(i); + int n = GETARG_sA(i); + if (tonumber(var)) { + *top = *var; /* PUSHLOCAL */ + ttype(top+1) = TAG_NUMBER; + nvalue(top+1) = (Number)n; /* PUSHINT */ + call_arith(L, top+2, IM_ADD); + *var = *top; /* SETLOCAL */ + } + else + nvalue(var) += (Number)n; + break; + } + case OP_ADDI: if (tonumber(top-1)) { ttype(top) = TAG_NUMBER; @@ -554,50 +569,50 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { nvalue(top-1) = 1; break; - case OP_IFNEQJMP: + case OP_JMPNEQ: top -= 2; if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i); break; - case OP_IFEQJMP: + case OP_JMPEQ: top -= 2; if (luaO_equalObj(top, top+1)) pc += GETARG_S(i); break; - case OP_IFLTJMP: + case OP_JMPLT: top -= 2; if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); break; - case OP_IFLEJMP: /* a <= b === !(b b === (b b === (b= b === !(a= b === !(a