commit 4e7e9e8de5b9e50694e161653ef03b5c15a53054
parent 3e45496295bef65692f690d1633a9ce6ad9408b0
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 4 Apr 2000 17:48:22 -0300
new opcode INCLOCAL.
Diffstat:
M | lcode.c | | | 89 | +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
M | llimits.h | | | 6 | ++---- |
M | lopcodes.h | | | 77 | ++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
M | lvm.c | | | 41 | ++++++++++++++++++++++++++++------------- |
4 files changed, 132 insertions(+), 81 deletions(-)
diff --git 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
@@ -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<<SIZE_U)-1)
-#define MAXARG_S ((1<<(SIZE_S-1))-1) /* `S' is signed */
+#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
#define MAXARG_A ((1<<SIZE_A)-1)
#define MAXARG_B ((1<<SIZE_B)-1)
diff --git a/lopcodes.h b/lopcodes.h
@@ -1,5 +1,5 @@
/*
-** $Id: lopcodes.h,v 1.52 2000/03/24 19:49:23 roberto Exp roberto $
+** $Id: lopcodes.h,v 1.53 2000/03/27 14:31:12 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -13,26 +13,30 @@
/*===========================================================================
We assume that instructions are unsigned numbers.
All instructions have an opcode in the first 6 bits. Moreover,
- an instruction can have 0, 1, or 2 arguments. There are 4 types of
- Instructions:
+ an instruction can have 0, 1, or 2 arguments. Instructions can
+ have the following types:
type 0: no arguments
type 1: 1 unsigned argument in the higher bits (called `U')
type 2: 1 signed argument in the higher bits (`S')
type 3: 1st unsigned argument in the higher bits (`A')
2nd unsigned argument in the middle bits (`B')
- The signed argument is represented in excess 2^K; that is, the number
- value is the usigned value minus 2^K.
+ A signed argument is represented in excess K; that is, the number
+ value is the unsigned value minus K. K is exactly the maximum value
+ for that argument (so that -max is represented by 0, and +max is
+ represented by 2*max), which is half the maximum for the corresponding
+ unsigned argument.
The size of each argument is defined in `llimits.h'. The usual is an
- instruction with 32 bits, U and S arguments with 26 bits (32-6), B
- argument with 9 bits, and A argument with 17 bits (32-6-9). For small
- instalations, the instruction size can be 16, so U and S have 10 bits,
+ instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
+ with 9 bits, and A arguments with 17 bits (32-6-9). For small
+ instalations, the instruction size can be 16, so U has 10 bits,
and A and B have 5 bits each.
===========================================================================*/
-#define EXCESS_S (1<<(SIZE_S-1)) /* == 2^K */
+
+#define MAXARG_sA (MAXARG_A>>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 GETARG_U(i) ((int)((i)>>POS_U))
#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
((Instruction)(u)<<POS_U)))
-#define SETARG_S(i,s) ((i) = (((i)&MASK0(SIZE_S,POS_S)) | \
- ((Instruction)((s)+EXCESS_S)<<POS_S)))
+
+#define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
+#define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
+#define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
+
+
+#define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
+ | ((Instruction)(b)<<POS_B))
+#define GETARG_A(i) ((int)((i)>>POS_A))
#define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
((Instruction)(a)<<POS_A)))
+#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
((Instruction)(b)<<POS_B)))
-#define CREATE_0(o) ((Instruction)(o))
-#define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U)
-#define CREATE_S(o,s) ((Instruction)(o) | ((Instruction)(s)+EXCESS_S)<<POS_S)
-#define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
- | ((Instruction)(b)<<POS_B))
+
+#define CREATE_sAB(o,a,b) (CREATE_AB((o),(a)+MAXARG_sA,(b)))
+#define GETARG_sA(i) (GETARG_A(i)-MAXARG_sA)
+
/*
@@ -112,6 +122,7 @@ OP_SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */
OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */
OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */
+OP_INCLOCAL,/* sA B - - LOC[B]+=sA */
OP_ADD,/* - y x x+y */
OP_ADDI,/* S x x+s */
OP_SUB,/* - y x x-y */
@@ -122,17 +133,17 @@ OP_CONC,/* U v_u-v_1 v1..-..v_u */
OP_MINUS,/* - x -x */
OP_NOT,/* - x (x==nil)? 1 : nil */
-OP_IFNEQJMP,/* J y x - (x~=y)? PC+=s */
-OP_IFEQJMP,/* J y x - (x==y)? PC+=s */
-OP_IFLTJMP,/* J y x - (x<y)? PC+=s */
-OP_IFLEJMP,/* J y x - (x<y)? PC+=s */
-OP_IFGTJMP,/* J y x - (x>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 - (x<y)? PC+=s */
+OP_JMPLE,/* J y x - (x<y)? PC+=s */
+OP_JMPGT,/* J y x - (x>y)? 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
@@ -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<a) */
+ case OP_JMPLE: /* a <= b === !(b<a) */
top -= 2;
if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
break;
- case OP_IFGTJMP: /* a > b === (b<a) */
+ case OP_JMPGT: /* a > b === (b<a) */
top -= 2;
if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
break;
- case OP_IFGEJMP: /* a >= b === !(a<b) */
+ case OP_JMPGE: /* a >= b === !(a<b) */
top -= 2;
if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
break;
- case OP_IFTJMP:
+ case OP_JMPT:
if (ttype(--top) != TAG_NIL) pc += GETARG_S(i);
break;
- case OP_IFFJMP:
+ case OP_JMPF:
if (ttype(--top) == TAG_NIL) pc += GETARG_S(i);
break;
- case OP_ONTJMP:
+ case OP_JMPONT:
if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i);
else top--;
break;
- case OP_ONFJMP:
+ case OP_JMPONF:
if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i);
else top--;
break;