small optimization in opcodes for "and" and "or"

This commit is contained in:
Roberto Ierusalimschy 1997-08-04 16:15:05 -03:00
parent abbf14cd32
commit dd1aa28390
4 changed files with 16 additions and 30 deletions

View File

@ -1,6 +1,6 @@
%{ %{
char *rcs_luastx = "$Id: lua.stx,v 3.49 1997/07/30 22:00:50 roberto Exp roberto $"; char *rcs_luastx = "$Id: lua.stx,v 3.50 1997/07/31 20:46:59 roberto Exp roberto $";
#include <stdlib.h> #include <stdlib.h>
@ -660,12 +660,12 @@ expr : '(' expr ')' { $$ = $2; }
| NIL {code_byte(PUSHNIL); $$ = 0; } | NIL {code_byte(PUSHNIL); $$ = 0; }
| functioncall { $$ = $1; } | functioncall { $$ = $1; }
| NOT expr1 { code_byte(NOTOP); $$ = 0;} | NOT expr1 { code_byte(NOTOP); $$ = 0;}
| expr1 AND PrepJump {code_byte(POP); } expr1 | expr1 AND PrepJump expr1
{ {
code_shortcircuit($3, ONFJMP); code_shortcircuit($3, ONFJMP);
$$ = 0; $$ = 0;
} }
| expr1 OR PrepJump {code_byte(POP); } expr1 | expr1 OR PrepJump expr1
{ {
code_shortcircuit($3, ONTJMP); code_shortcircuit($3, ONTJMP);
$$ = 0; $$ = 0;

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 4.20 1997/07/30 22:00:50 roberto Exp roberto $"; char *rcs_opcode="$Id: opcode.c,v 4.21 1997/07/31 19:37:37 roberto Exp roberto $";
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h> #include <stdio.h>
@ -1384,6 +1384,7 @@ static StkId lua_execute (TFunc *func, StkId base)
Word w; Word w;
get_word(w,pc); get_word(w,pc);
if (ttype(top-1) != LUA_T_NIL) pc += w; if (ttype(top-1) != LUA_T_NIL) pc += w;
else top--;
} }
break; break;
@ -1392,6 +1393,7 @@ static StkId lua_execute (TFunc *func, StkId base)
Word w; Word w;
get_word(w,pc); get_word(w,pc);
if (ttype(top-1) == LUA_T_NIL) pc += w; if (ttype(top-1) == LUA_T_NIL) pc += w;
else top--;
} }
break; break;
@ -1429,8 +1431,6 @@ static StkId lua_execute (TFunc *func, StkId base)
} }
break; break;
case POP: --top; break;
case CALLFUNC: case CALLFUNC:
{ {
int nParams = *(pc++); int nParams = *(pc++);

View File

@ -1,6 +1,6 @@
/* /*
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: opcode.h,v 3.36 1997/07/29 20:38:06 roberto Exp roberto $ ** $Id: opcode.h,v 3.37 1997/07/30 22:00:50 roberto Exp roberto $
*/ */
#ifndef opcode_h #ifndef opcode_h
@ -60,7 +60,7 @@ PUSHLOCAL9,/* - LOC[9] */
PUSHLOCAL,/* b - LOC[b] */ PUSHLOCAL,/* b - LOC[b] */
PUSHGLOBAL,/* w - VAR[w] */ PUSHGLOBAL,/* w - VAR[w] */
PUSHINDEXED,/* i t t[i] */ PUSHINDEXED,/* i t t[i] */
PUSHSELF,/* w t t t[STR[w]] */ PUSHSELF,/* w t t t[CNST[w]] */
STORELOCAL0,/* x - LOC[0]=x */ STORELOCAL0,/* x - LOC[0]=x */
STORELOCAL1,/* x - LOC[1]=x */ STORELOCAL1,/* x - LOC[1]=x */
STORELOCAL2,/* x - LOC[2]=x */ STORELOCAL2,/* x - LOC[2]=x */
@ -78,7 +78,7 @@ STOREINDEXED,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
STORELIST0,/* b v_b...v_1 t - t[i]=v_i */ STORELIST0,/* b v_b...v_1 t - t[i]=v_i */
STORELIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */ STORELIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
STORERECORD,/* b STORERECORD,/* b
w_b...w_1 v_b...v_1 t - t[STR[w_i]]=v_i */ w_b...w_1 v_b...v_1 t - t[CNST[w_i]]=v_i */
ADJUST0,/* - - TOP=BASE */ ADJUST0,/* - - TOP=BASE */
ADJUST,/* b - - TOP=BASE+b */ ADJUST,/* b - - TOP=BASE+b */
CREATEARRAY,/* w - newarray(size = w) */ CREATEARRAY,/* w - newarray(size = w) */
@ -95,21 +95,20 @@ POWOP,/* y x x^y */
CONCOP,/* y x x..y */ CONCOP,/* y x x..y */
MINUSOP,/* x -x */ MINUSOP,/* x -x */
NOTOP,/* x (x==nil)? 1 : nil */ NOTOP,/* x (x==nil)? 1 : nil */
ONTJMP,/* w x - (x!=nil)? PC+=w */ ONTJMP,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
ONFJMP,/* w x - (x==nil)? PC+=w */ ONFJMP,/* w x (x==nil)? x : - (x==nil)? PC+=w */
JMP,/* w - - PC+=w */ JMP,/* w - - PC+=w */
UPJMP,/* w - - PC-=w */ UPJMP,/* w - - PC-=w */
IFFJMP,/* w x - (x==nil)? PC+=w */ IFFJMP,/* w x - (x==nil)? PC+=w */
IFFUPJMP,/* w x - (x==nil)? PC-=w */ IFFUPJMP,/* w x - (x==nil)? PC-=w */
POP,/* x - */
CALLFUNC,/* b c v_b...v_1 f r_c...r_1 f(v1,...,v_b) */ CALLFUNC,/* b c v_b...v_1 f r_c...r_1 f(v1,...,v_b) */
RETCODE0, RETCODE0,
RETCODE,/* b - - */ RETCODE,/* b - - */
SETLINE,/* w - - LINE=w */ SETLINE,/* w - - LINE=w */
VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */ VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */
STOREMAP,/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */ STOREMAP,/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */
PUSHCONSTANTB,/*b - const[b] */ PUSHCONSTANTB,/*b - CNST[b] */
PUSHCONSTANT,/* w - const[w] */ PUSHCONSTANT,/* w - CNST[w] */
ENDCODE = 127 ENDCODE = 127
} OpCode; } OpCode;

View File

@ -3,7 +3,7 @@
** load bytecodes from files ** load bytecodes from files
*/ */
char* rcs_undump="$Id: undump.c,v 1.24 1997/06/17 18:19:17 roberto Exp roberto $"; char* rcs_undump="$Id: undump.c,v 1.25 1997/07/29 19:44:02 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -67,7 +67,6 @@ static void FixCode(Byte* code, Byte* end) /* swap words */
case CONCOP: case CONCOP:
case MINUSOP: case MINUSOP:
case NOTOP: case NOTOP:
case POP:
case RETCODE0: case RETCODE0:
p++; p++;
break; break;
@ -86,9 +85,6 @@ static void FixCode(Byte* code, Byte* end) /* swap words */
case CALLFUNC: case CALLFUNC:
p+=3; p+=3;
break; break;
case PUSHFUNCTION:
p+=5; /* TODO: use sizeof(TFunc*) or old? */
break;
case PUSHWORD: case PUSHWORD:
case PUSHSELF: case PUSHSELF:
case CREATEARRAY: case CREATEARRAY:
@ -99,7 +95,6 @@ static void FixCode(Byte* code, Byte* end) /* swap words */
case IFFJMP: case IFFJMP:
case IFFUPJMP: case IFFUPJMP:
case SETLINE: case SETLINE:
case PUSHSTRING:
case PUSHGLOBAL: case PUSHGLOBAL:
case STOREGLOBAL: case STOREGLOBAL:
{ {
@ -108,14 +103,6 @@ static void FixCode(Byte* code, Byte* end) /* swap words */
p+=3; p+=3;
break; break;
} }
case PUSHFLOAT: /* assumes sizeof(float)==4 */
{
Byte t;
t=p[1]; p[1]=p[4]; p[4]=t;
t=p[2]; p[2]=p[3]; p[3]=t;
p+=5;
break;
}
case STORERECORD: case STORERECORD:
{ {
int n=*++p; int n=*++p;
@ -226,7 +213,7 @@ static void LoadFunction(ZIO* Z)
{ {
int i=LoadWord(Z); int i=LoadWord(Z);
char* s=LoadString(Z); char* s=LoadString(Z);
int v=luaI_findconstantbyname(s); int v; /*=luaI_findconstantbyname(s); ??????? */
Unthread(tf->code,i,v); Unthread(tf->code,i,v);
} }
else else
@ -324,7 +311,7 @@ int luaI_undump(ZIO* Z)
while ((m=luaI_undump1(Z))) while ((m=luaI_undump1(Z)))
{ {
int status=luaI_dorun(m); int status=luaI_dorun(m);
luaI_freefunc(m); /* luaI_freefunc(m); ???*/
if (status!=0) return status; if (status!=0) return status;
} }
return 0; return 0;