Browse Source

jumps are relative to next instruction

v5-2
Roberto Ierusalimschy 27 years ago
parent
commit
f6a9cc9a67
  1. 135
      lopcodes.h
  2. 68
      lua.stx
  3. 56
      lvm.c

135
lopcodes.h

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.h,v 1.5 1997/09/24 19:43:11 roberto Exp roberto $
** $Id: lopcodes.h,v 1.6 1997/10/01 20:05:34 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -18,60 +18,60 @@
typedef enum {
/* name parm before after side effect
-----------------------------------------------------------------------------*/
ENDCODE,
ENDCODE,/* - - - */
PUSHNIL,/* - nil */
PUSHNIL,/* - - nil */
PUSHNILS,/* b - nil_1...nil_b */
PUSH0,/* - 0.0 */
PUSH1,/* - 1.0 */
PUSH2,/* - 2.0 */
PUSH0,/* - - 0.0 */
PUSH1,/* - - 1.0 */
PUSH2,/* - - 2.0 */
PUSHBYTE,/* b - (float)b */
PUSHWORD,/* w - (float)w */
PUSHCONSTANT0,/* - CNST[0] */
PUSHCONSTANT1,/* - CNST[1] */
PUSHCONSTANT2,/* - CNST[2] */
PUSHCONSTANT3,/* - CNST[3] */
PUSHCONSTANT4,/* - CNST[4] */
PUSHCONSTANT5,/* - CNST[5] */
PUSHCONSTANT6,/* - CNST[6] */
PUSHCONSTANT7,/* - CNST[7] */
PUSHCONSTANT8,/* - CNST[8] */
PUSHCONSTANT9,/* - CNST[9] */
PUSHCONSTANT0,/*- - CNST[0] */
PUSHCONSTANT1,/*- - CNST[1] */
PUSHCONSTANT2,/*- - CNST[2] */
PUSHCONSTANT3,/*- - CNST[3] */
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] */
PUSHUPVALUE0,
PUSHUPVALUE1,
PUSHUPVALUE0,/* - - Closure[0] */
PUSHUPVALUE1,/* - - Closure[1] */
PUSHUPVALUE,/* b - Closure[b] */
PUSHLOCAL0,/* - LOC[0] */
PUSHLOCAL1,/* - LOC[1] */
PUSHLOCAL2,/* - LOC[2] */
PUSHLOCAL3,/* - LOC[3] */
PUSHLOCAL4,/* - LOC[4] */
PUSHLOCAL5,/* - LOC[5] */
PUSHLOCAL6,/* - LOC[6] */
PUSHLOCAL7,/* - LOC[7] */
PUSHLOCAL8,/* - LOC[8] */
PUSHLOCAL9,/* - LOC[9] */
PUSHLOCAL0,/* - - LOC[0] */
PUSHLOCAL1,/* - - LOC[1] */
PUSHLOCAL2,/* - - LOC[2] */
PUSHLOCAL3,/* - - LOC[3] */
PUSHLOCAL4,/* - - LOC[4] */
PUSHLOCAL5,/* - - LOC[5] */
PUSHLOCAL6,/* - - LOC[6] */
PUSHLOCAL7,/* - - LOC[7] */
PUSHLOCAL8,/* - - LOC[8] */
PUSHLOCAL9,/* - - LOC[9] */
PUSHLOCAL,/* b - LOC[b] */
GETGLOBAL0,/* - VAR[CNST[0]] */
GETGLOBAL1,/* - VAR[CNST[1]] */
GETGLOBAL2,/* - VAR[CNST[2]] */
GETGLOBAL3,/* - VAR[CNST[3]] */
GETGLOBAL4,/* - VAR[CNST[4]] */
GETGLOBAL5,/* - VAR[CNST[5]] */
GETGLOBAL6,/* - VAR[CNST[6]] */
GETGLOBAL7,/* - VAR[CNST[7]] */
GETGLOBAL8,/* - VAR[CNST[8]] */
GETGLOBAL9,/* - VAR[CNST[9]] */
GETGLOBAL0,/* - - VAR[CNST[0]] */
GETGLOBAL1,/* - - VAR[CNST[1]] */
GETGLOBAL2,/* - - VAR[CNST[2]] */
GETGLOBAL3,/* - - VAR[CNST[3]] */
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]] */
GETTABLE,/* i t t[i] */
GETTABLE,/* - i t t[i] */
PUSHSELFB,/* b t t t[CNST[b]] */
PUSHSELF,/* w t t t[CNST[w]] */
@ -79,44 +79,43 @@ PUSHSELF,/* w t t t[CNST[w]] */
CREATEARRAYB,/* b - newarray(size = b) */
CREATEARRAY,/* w - newarray(size = w) */
SETLOCAL0,/* x - LOC[0]=x */
SETLOCAL1,/* x - LOC[1]=x */
SETLOCAL2,/* x - LOC[2]=x */
SETLOCAL3,/* x - LOC[3]=x */
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 */
SETLOCAL0,/* - x - LOC[0]=x */
SETLOCAL1,/* - x - LOC[1]=x */
SETLOCAL2,/* - x - LOC[2]=x */
SETLOCAL3,/* - x - LOC[3]=x */
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 */
SETTABLE0,/* v i t - t[i]=v */
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 */
EQOP,/* y x (x==y)? 1 : nil */
NEQOP,/* y x (x~=y)? 1 : nil */
LTOP,/* y x (x<y)? 1 : nil */
LEOP,/* y x (x<y)? 1 : nil */
GTOP,/* y x (x>y)? 1 : nil */
GEOP,/* y x (x>=y)? 1 : nil */
ADDOP,/* y x x+y */
SUBOP,/* y x x-y */
MULTOP,/* y x x*y */
DIVOP,/* y x x/y */
POWOP,/* y x x^y */
CONCOP,/* y x x..y */
MINUSOP,/* x -x */
NOTOP,/* x (x==nil)? 1 : nil */
/* NOTICE: all jumps are relative to the position following the opcode */
EQOP,/* - y x (x==y)? 1 : nil */
NEQOP,/* - y x (x~=y)? 1 : nil */
LTOP,/* - y x (x<y)? 1 : nil */
LEOP,/* - y x (x<y)? 1 : nil */
GTOP,/* - y x (x>y)? 1 : nil */
GEOP,/* - y x (x>=y)? 1 : nil */
ADDOP,/* - y x x+y */
SUBOP,/* - y x x-y */
MULTOP,/* - y x x*y */
DIVOP,/* - y x x/y */
POWOP,/* - y x x^y */
CONCOP,/* - y x x..y */
MINUSOP,/* - x -x */
NOTOP,/* - x (x==nil)? 1 : nil */
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
JMPB,/* b - - PC+=b */
@ -134,8 +133,8 @@ 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) */
RETCODE,/* b - - */
SETLINE,/* w - - LINE=w */
POP1,/* - - TOP-=1 */
POP2,/* - - TOP-=2 */
POP1,/* - - - TOP-=1 */
POP2,/* - - - TOP-=2 */
POPS,/* b - - TOP-=b */
ARGS,/* b - - TOP=BASE+b */
VARARGS/* b v_x...v_1 {v_1...v_x;n=x} TOP=BASE+b+1 */

68
lua.stx

@ -1,6 +1,6 @@
%{
/*
** $Id: lua.stx,v 1.6 1997/09/26 15:02:26 roberto Exp roberto $
** $Id: lua.stx,v 1.7 1997/10/01 20:05:34 roberto Exp roberto $
** Syntax analizer and code generator
** See Copyright Notice in lua.h
*/
@ -135,31 +135,6 @@ static void code_word (int n)
}
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)
{
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);
}
static void deltastack (int delta)
{
currState->stacksize += delta;
@ -501,6 +476,39 @@ static int lua_codestore (int i, int left)
}
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));
}
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);
}
static void codeIf (int thenAdd, int elseAdd)
{
int elseinit = elseAdd+JMPSIZE;
@ -516,7 +524,7 @@ static void codeIf (int thenAdd, int elseAdd)
static void code_shortcircuit (OpCode op, int pos)
{
int dist = currState->pc - (pos+1);
int dist = currState->pc - (pos+JMPSIZE);
if (dist > 255)
luaY_error("and/or expression too long");
currState->f->code[pos] = op;
@ -578,7 +586,6 @@ static void init_func (void)
}
static TProtoFunc *close_func (void)
{
TProtoFunc *f = currState->f;
@ -681,12 +688,13 @@ stat : IF cond THEN block SaveWord elsepart END
&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);
fix_upjmp(IFTUPJMPB, newpos);
}}
| REPEAT GetPC block UNTIL expr1
{
code_opborw(IFFUPJMPB, currState->pc+1 - $2, -1);
fix_upjmp(IFFUPJMPB, $2);
deltastack(-1); /* pops condition */
}
| varlist1 '=' exprlist1

56
lvm.c

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 1.6 1997/09/26 15:02:26 roberto Exp roberto $
** $Id: lvm.c,v 1.7 1997/10/01 20:05:34 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -570,51 +570,51 @@ StkId luaV_execute (Closure *cl, StkId base)
break;
case ONTJMP:
if (ttype(luaD_stack.top-1) != LUA_T_NIL) pc += *pc;
else { pc++; luaD_stack.top--; }
aux = *pc++;
if (ttype(luaD_stack.top-1) != LUA_T_NIL) pc += aux;
else luaD_stack.top--;
break;
case ONFJMP:
if (ttype(luaD_stack.top-1) == LUA_T_NIL) pc += *pc;
else { pc++; luaD_stack.top--; }
break;
case JMPB:
pc += *pc;
aux = *pc++;
if (ttype(luaD_stack.top-1) == LUA_T_NIL) pc += aux;
else luaD_stack.top--;
break;
case JMP:
pc += get_word(pc);
break;
aux = next_word(pc); goto jmp;
case IFFJMPB:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += *pc;
else pc++;
case JMPB:
aux = *pc++;
jmp:
pc += aux;
break;
case IFFJMP:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += get_word(pc);
else skip_word(pc);
break;
aux = next_word(pc); goto iffjmp;
case IFTUPJMPB:
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= *pc;
else pc++;
case IFFJMPB:
aux = *pc++;
iffjmp:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc += aux;
break;
case IFTUPJMP:
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= get_word(pc);
else skip_word(pc);
break;
aux = next_word(pc); goto iftupjmp;
case IFFUPJMPB:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= *pc;
else pc++;
case IFTUPJMPB:
aux = *pc++;
iftupjmp:
if (ttype(--luaD_stack.top) != LUA_T_NIL) pc -= aux;
break;
case IFFUPJMP:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= get_word(pc);
else skip_word(pc);
aux = next_word(pc); goto iffupjmp;
case IFFUPJMPB:
aux = *pc++;
iffupjmp:
if (ttype(--luaD_stack.top) == LUA_T_NIL) pc -= aux;
break;
case CLOSURE:

Loading…
Cancel
Save