mirror of
https://github.com/lua/lua.git
synced 2025-01-14 05:43:00 +08:00
all jumps have byte variants; WHILE optimization
This commit is contained in:
parent
eb617df2d8
commit
28d47a0aaa
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.h,v 1.4 1997/09/22 20:53:20 roberto Exp roberto $
|
** $Id: lopcodes.h,v 1.5 1997/09/24 19:43:11 roberto Exp roberto $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -76,6 +76,7 @@ GETTABLE,/* i t t[i] */
|
|||||||
PUSHSELFB,/* b t t t[CNST[b]] */
|
PUSHSELFB,/* b t t t[CNST[b]] */
|
||||||
PUSHSELF,/* w t t t[CNST[w]] */
|
PUSHSELF,/* w t t t[CNST[w]] */
|
||||||
|
|
||||||
|
CREATEARRAYB,/* b - newarray(size = b) */
|
||||||
CREATEARRAY,/* w - newarray(size = w) */
|
CREATEARRAY,/* w - newarray(size = w) */
|
||||||
|
|
||||||
SETLOCAL0,/* x - LOC[0]=x */
|
SETLOCAL0,/* x - LOC[0]=x */
|
||||||
@ -118,10 +119,12 @@ NOTOP,/* x (x==nil)? 1 : nil */
|
|||||||
/* NOTICE: all jumps are relative to the position following the opcode */
|
/* NOTICE: all jumps are relative to the position following the opcode */
|
||||||
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
|
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
|
||||||
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
|
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
|
||||||
|
JMPB,/* b - - PC+=b */
|
||||||
JMP,/* w - - PC+=w */
|
JMP,/* w - - PC+=w */
|
||||||
UPJMPB,/* b - - PC-=b */
|
IFFJMPB,/* b x - (x==nil)? PC+=b */
|
||||||
UPJMP,/* w - - PC-=w */
|
|
||||||
IFFJMP,/* w x - (x==nil)? PC+=w */
|
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 */
|
IFFUPJMPB,/* b x - (x==nil)? PC-=b */
|
||||||
IFFUPJMP,/* w x - (x==nil)? PC-=w */
|
IFFUPJMP,/* w x - (x==nil)? PC-=w */
|
||||||
|
|
||||||
|
133
lua.stx
133
lua.stx
@ -1,6 +1,6 @@
|
|||||||
%{
|
%{
|
||||||
/*
|
/*
|
||||||
** $Id: lua.stx,v 1.5 1997/09/24 19:43:11 roberto Exp roberto $
|
** $Id: lua.stx,v 1.6 1997/09/26 15:02:26 roberto Exp roberto $
|
||||||
** Syntax analizer and code generator
|
** Syntax analizer and code generator
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -28,6 +28,9 @@ int luaY_parse (void);
|
|||||||
#define free luaM_free
|
#define free luaM_free
|
||||||
|
|
||||||
|
|
||||||
|
/* size of a "normal" jump instruction: OpCode + 1 byte */
|
||||||
|
#define JMPSIZE 2
|
||||||
|
|
||||||
/* maximum number of local variables */
|
/* maximum number of local variables */
|
||||||
#define MAXLOCALS 32
|
#define MAXLOCALS 32
|
||||||
|
|
||||||
@ -85,11 +88,32 @@ void luaY_error (char *s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void code_byte (Byte c)
|
static void check_pc (int n)
|
||||||
{
|
{
|
||||||
if (currState->pc >= currState->maxcode)
|
if (currState->pc+n > currState->maxcode)
|
||||||
currState->maxcode = luaM_growvector(&currState->f->code,
|
currState->maxcode = luaM_growvector(&currState->f->code,
|
||||||
currState->maxcode, Byte, codeEM, MAX_INT);
|
currState->maxcode, Byte, codeEM, MAX_INT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void movecode_up (int d, int s, int n)
|
||||||
|
{
|
||||||
|
while (n--)
|
||||||
|
currState->f->code[d+n] = currState->f->code[s+n];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void movecode_down (int d, int s, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
currState->f->code[d+i] = currState->f->code[s+i];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void code_byte (Byte c)
|
||||||
|
{
|
||||||
|
check_pc(1);
|
||||||
currState->f->code[currState->pc++] = c;
|
currState->f->code[currState->pc++] = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,19 +127,36 @@ static void code_word_at (int pc, int n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void fix_jump (int pc, OpCode op, int n)
|
static void code_word (int n)
|
||||||
{
|
{
|
||||||
currState->f->code[pc] = op;
|
check_pc(2);
|
||||||
code_word_at(pc+1, n);
|
currState->pc += 2;
|
||||||
|
code_word_at(currState->pc-2, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void code_word (int n)
|
static int fix_opcode (int pc, OpCode op, int n)
|
||||||
{
|
{
|
||||||
if (n > MAX_WORD)
|
if (n <= 255) {
|
||||||
luaY_error("construction too big; unable to compile");
|
currState->f->code[pc] = op;
|
||||||
code_byte(n&0xFF);
|
currState->f->code[pc+1] = n;
|
||||||
code_byte(n>>8);
|
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)
|
||||||
|
{
|
||||||
|
n -= pc+1; /* jump is relative to position following jump opcode */
|
||||||
|
if (n > 255) n++; /* jump must be 1 bigger */
|
||||||
|
return fix_opcode(pc, op, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -462,14 +503,14 @@ static int lua_codestore (int i, int left)
|
|||||||
|
|
||||||
static void codeIf (int thenAdd, int elseAdd)
|
static void codeIf (int thenAdd, int elseAdd)
|
||||||
{
|
{
|
||||||
int elseinit = elseAdd+sizeof(Word)+1;
|
int elseinit = elseAdd+JMPSIZE;
|
||||||
if (currState->pc == elseinit) { /* no else part */
|
if (currState->pc == elseinit) { /* no else part */
|
||||||
currState->pc -= sizeof(Word)+1;
|
currState->pc -= JMPSIZE;
|
||||||
elseinit = currState->pc;
|
elseinit = currState->pc;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
fix_jump(elseAdd, JMP, currState->pc-(elseAdd+1));
|
elseinit += fix_jump(elseAdd, JMPB, currState->pc);
|
||||||
fix_jump(thenAdd, IFFJMP, elseinit-(thenAdd+1));
|
fix_jump(thenAdd, IFFJMPB, elseinit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -593,7 +634,7 @@ TProtoFunc *luaY_parser (ZIO *z, char *chunkname)
|
|||||||
%token <vReal> NUMBER
|
%token <vReal> NUMBER
|
||||||
%token <pTStr> NAME STRING
|
%token <pTStr> NAME STRING
|
||||||
|
|
||||||
%type <vInt> PrepJump, PrepJumpPop, PrepJumpSC
|
%type <vInt> SaveWord, cond, GetPC, SaveWordPop, SaveWordPush
|
||||||
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
||||||
counter (which has list length); if <= 0, -list lenght */
|
counter (which has list length); if <= 0, -list lenght */
|
||||||
%type <vLong> functioncall, expr /* if != 0, points to function return
|
%type <vLong> functioncall, expr /* if != 0, points to function return
|
||||||
@ -628,18 +669,24 @@ statlist : /* empty */
|
|||||||
|
|
||||||
sc : /* empty */ | ';' ;
|
sc : /* empty */ | ';' ;
|
||||||
|
|
||||||
stat : IF expr1 THEN PrepJumpPop block PrepJump elsepart END
|
stat : IF cond THEN block SaveWord elsepart END
|
||||||
{ codeIf($4, $6); }
|
{ codeIf($2, $5); }
|
||||||
|
|
||||||
| WHILE {$<vInt>$=currState->pc;} expr1 DO PrepJumpPop block END
|
| WHILE GetPC cond DO block END
|
||||||
{
|
{{
|
||||||
code_opborw(UPJMPB, currState->pc+1 - ($<vInt>2), 0);
|
int expsize = $3-$2;
|
||||||
fix_jump($5, IFFJMP, currState->pc - ($5+1));
|
int newpos = $2+JMPSIZE;
|
||||||
}
|
check_pc(expsize);
|
||||||
|
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);
|
||||||
|
code_opborw(IFTUPJMPB, currState->pc+1 - newpos, 0);
|
||||||
|
}}
|
||||||
|
|
||||||
| REPEAT {$<vInt>$=currState->pc;} block UNTIL expr1
|
| REPEAT GetPC block UNTIL expr1
|
||||||
{
|
{
|
||||||
code_opborw(IFFUPJMPB, currState->pc+1 - ($<vInt>2), -1);
|
code_opborw(IFFUPJMPB, currState->pc+1 - $2, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
| varlist1 '=' exprlist1
|
| varlist1 '=' exprlist1
|
||||||
@ -687,8 +734,8 @@ body : '(' parlist ')' chunk END { $$ = close_func(); }
|
|||||||
|
|
||||||
elsepart : /* empty */
|
elsepart : /* empty */
|
||||||
| ELSE block
|
| ELSE block
|
||||||
| ELSEIF expr1 THEN PrepJumpPop block PrepJump elsepart
|
| ELSEIF cond THEN block SaveWord elsepart
|
||||||
{ codeIf($4, $6); }
|
{ codeIf($2, $5); }
|
||||||
;
|
;
|
||||||
|
|
||||||
ret : /* empty */
|
ret : /* empty */
|
||||||
@ -699,19 +746,19 @@ ret : /* empty */
|
|||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
PrepJump : /* empty */
|
GetPC : /* empty */ { $$ = currState->pc; }
|
||||||
{
|
|
||||||
$$ = currState->pc;
|
|
||||||
code_byte(0); /* open space */
|
|
||||||
code_word(0);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
PrepJumpSC : /* empty */
|
|
||||||
{ $$ = currState->pc; code_opcode(0, -1); code_byte(0); }
|
|
||||||
;
|
;
|
||||||
|
|
||||||
PrepJumpPop : PrepJump { $$ = $1; deltastack(-1); /* pop condition */ }
|
SaveWord : GetPC { $$ = $1; code_word(0); /* open space */ }
|
||||||
|
;
|
||||||
|
|
||||||
|
SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ }
|
||||||
|
;
|
||||||
|
|
||||||
|
SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ }
|
||||||
|
;
|
||||||
|
|
||||||
|
cond : expr1 SaveWordPop { $$ = $2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
expr1 : expr { adjust_functioncall($1, 1); }
|
expr1 : expr { adjust_functioncall($1, 1); }
|
||||||
@ -739,14 +786,11 @@ expr : '(' expr ')' { $$ = $2; }
|
|||||||
| NIL {code_opcode(PUSHNIL, 1); $$ = 0; }
|
| NIL {code_opcode(PUSHNIL, 1); $$ = 0; }
|
||||||
| functioncall { $$ = $1; }
|
| functioncall { $$ = $1; }
|
||||||
| FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
|
| FUNCTION { init_func(); } body { func_onstack($3); $$ = 0; }
|
||||||
| expr1 AND PrepJumpSC expr1 { code_shortcircuit(ONFJMP, $3); $$ = 0; }
|
| expr1 AND SaveWordPop expr1 { code_shortcircuit(ONFJMP, $3); $$ = 0; }
|
||||||
| expr1 OR PrepJumpSC expr1 { code_shortcircuit(ONTJMP, $3); $$ = 0; }
|
| expr1 OR SaveWordPop expr1 { code_shortcircuit(ONTJMP, $3); $$ = 0; }
|
||||||
;
|
;
|
||||||
|
|
||||||
table :
|
table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAYB, $3); }
|
||||||
{ $<vInt>$ = currState->pc+1; code_opw(CREATEARRAY, 0, 1); }
|
|
||||||
'{' fieldlist '}'
|
|
||||||
{ code_word_at($<vInt>1, $3); }
|
|
||||||
;
|
;
|
||||||
|
|
||||||
functioncall : funcvalue funcParams
|
functioncall : funcvalue funcParams
|
||||||
@ -892,3 +936,4 @@ decinit : /* empty */ { $$ = 0; }
|
|||||||
;
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
66
lvm.c
66
lvm.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lvm.c,v 1.5 1997/09/24 19:43:11 roberto Exp roberto $
|
** $Id: lvm.c,v 1.6 1997/09/26 15:02:26 roberto Exp roberto $
|
||||||
** Lua virtual machine
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -452,8 +452,13 @@ StkId luaV_execute (Closure *cl, StkId base)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CREATEARRAY:
|
case CREATEARRAY:
|
||||||
|
aux = next_word(pc); goto createarray;
|
||||||
|
|
||||||
|
case CREATEARRAYB:
|
||||||
|
aux = *pc++;
|
||||||
|
createarray:
|
||||||
luaC_checkGC();
|
luaC_checkGC();
|
||||||
avalue(luaD_stack.top) = luaH_new(next_word(pc));
|
avalue(luaD_stack.top) = luaH_new(aux);
|
||||||
ttype(luaD_stack.top) = LUA_T_ARRAY;
|
ttype(luaD_stack.top) = LUA_T_ARRAY;
|
||||||
luaD_stack.top++;
|
luaD_stack.top++;
|
||||||
break;
|
break;
|
||||||
@ -565,54 +570,51 @@ StkId luaV_execute (Closure *cl, StkId base)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ONTJMP:
|
case ONTJMP:
|
||||||
if (ttype(luaD_stack.top-1) != LUA_T_NIL)
|
if (ttype(luaD_stack.top-1) != LUA_T_NIL) pc += *pc;
|
||||||
pc += *pc;
|
else { pc++; luaD_stack.top--; }
|
||||||
else {
|
|
||||||
pc++;
|
|
||||||
luaD_stack.top--;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ONFJMP:
|
case ONFJMP:
|
||||||
if (ttype(luaD_stack.top-1) == LUA_T_NIL)
|
if (ttype(luaD_stack.top-1) == LUA_T_NIL) pc += *pc;
|
||||||
pc += *pc;
|
else { pc++; luaD_stack.top--; }
|
||||||
else {
|
break;
|
||||||
pc++;
|
|
||||||
luaD_stack.top--;
|
case JMPB:
|
||||||
}
|
pc += *pc;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JMP:
|
case JMP:
|
||||||
pc += get_word(pc);
|
pc += get_word(pc);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case UPJMPB:
|
case IFFJMPB:
|
||||||
pc -= *pc;
|
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += *pc;
|
||||||
break;
|
else pc++;
|
||||||
|
|
||||||
case UPJMP:
|
|
||||||
pc -= get_word(pc);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IFFJMP:
|
case IFFJMP:
|
||||||
if (ttype(--luaD_stack.top) == LUA_T_NIL)
|
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += get_word(pc);
|
||||||
pc += get_word(pc);
|
else skip_word(pc);
|
||||||
else
|
break;
|
||||||
skip_word(pc);
|
|
||||||
|
case IFTUPJMPB:
|
||||||
|
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= *pc;
|
||||||
|
else pc++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IFTUPJMP:
|
||||||
|
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= get_word(pc);
|
||||||
|
else skip_word(pc);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IFFUPJMPB:
|
case IFFUPJMPB:
|
||||||
if (ttype(--luaD_stack.top) == LUA_T_NIL)
|
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= *pc;
|
||||||
pc -= *pc;
|
else pc++;
|
||||||
else
|
|
||||||
pc++;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IFFUPJMP:
|
case IFFUPJMP:
|
||||||
if (ttype(--luaD_stack.top) == LUA_T_NIL)
|
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= get_word(pc);
|
||||||
pc -= get_word(pc);
|
else skip_word(pc);
|
||||||
else
|
|
||||||
skip_word(pc);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CLOSURE:
|
case CLOSURE:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user