1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2000-03-04 23:18:15 +03:00
|
|
|
** $Id: lopcodes.h,v 1.44 2000/03/03 18:53:17 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Opcodes for Lua virtual machine
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lopcodes_h
|
|
|
|
#define lopcodes_h
|
|
|
|
|
|
|
|
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
/*===========================================================================
|
|
|
|
We assume that instructions are unsigned numbers with 4 bytes.
|
2000-03-09 16:57:37 +03:00
|
|
|
All instructions have an opcode in the 8 bits. Moreover,
|
2000-02-14 19:51:08 +03:00
|
|
|
an instruction can have 0, 1, or 2 arguments. There are 4 types of
|
|
|
|
Instructions:
|
|
|
|
type 0: no arguments
|
|
|
|
type 1: 1 unsigned argument in the higher 24 bits (called `U')
|
|
|
|
type 2: 1 signed argument in the higher 24 bits (`S')
|
|
|
|
type 3: 1st unsigned argument in the higher 16 bits (`A')
|
|
|
|
2nd unsigned argument in the middle 8 bits (`B')
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
The signed argument is represented in excess 2^23; that is, the real value
|
2000-03-03 17:58:26 +03:00
|
|
|
is the usigned value minus 2^23.
|
2000-02-14 19:51:08 +03:00
|
|
|
===========================================================================*/
|
1999-03-06 00:16:07 +03:00
|
|
|
|
2000-03-09 16:57:37 +03:00
|
|
|
#define SIZE_INSTRUCTION 32
|
|
|
|
|
2000-03-09 03:19:22 +03:00
|
|
|
#define SIZE_OP 8
|
2000-03-09 16:57:37 +03:00
|
|
|
#define SIZE_U (SIZE_INSTRUCTION-SIZE_OP)
|
|
|
|
#define POS_U SIZE_OP
|
|
|
|
#define SIZE_S (SIZE_INSTRUCTION-SIZE_OP)
|
|
|
|
#define POS_S SIZE_OP
|
2000-03-09 03:19:22 +03:00
|
|
|
#define SIZE_B 8
|
2000-03-09 16:57:37 +03:00
|
|
|
#define POS_B SIZE_OP
|
|
|
|
#define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B))
|
|
|
|
#define POS_A (SIZE_OP+SIZE_B)
|
2000-03-09 03:19:22 +03:00
|
|
|
|
|
|
|
#define EXCESS_S (1<<(SIZE_S-1)) /* == 2^23 */
|
|
|
|
|
|
|
|
|
|
|
|
/* creates a mask with `n' 1 bits at position `p' */
|
2000-03-09 16:57:37 +03:00
|
|
|
#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
|
2000-03-09 03:19:22 +03:00
|
|
|
|
|
|
|
/* creates a mask with `n' 0 bits at position `p' */
|
|
|
|
#define MASK0(n,p) (~MASK1(n,p))
|
2000-03-03 17:58:26 +03:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
/*
|
|
|
|
** the following macros help to manipulate instructions
|
|
|
|
*/
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-03-09 03:19:22 +03:00
|
|
|
#define MAXARG_U ((1<<SIZE_U)-1)
|
|
|
|
#define MAXARG_S ((1<<(SIZE_S-1))-1) /* `S' is signed */
|
|
|
|
#define MAXARG_A ((1<<SIZE_A)-1)
|
|
|
|
#define MAXARG_B ((1<<SIZE_B)-1)
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-03-09 03:19:22 +03:00
|
|
|
#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)))
|
1999-02-09 18:59:10 +03:00
|
|
|
|
2000-03-09 16:57:37 +03:00
|
|
|
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
|
|
|
|
#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 SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
|
|
|
((Instruction)(a)<<POS_A)))
|
|
|
|
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
|
|
|
|
((Instruction)(b)<<POS_B)))
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-03-03 21:53:17 +03:00
|
|
|
#define CREATE_0(o) ((Instruction)(o))
|
2000-03-09 03:19:22 +03:00
|
|
|
#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))
|
1997-09-23 00:53:20 +04:00
|
|
|
|
|
|
|
|
2000-02-22 16:31:43 +03:00
|
|
|
/*
|
|
|
|
** K = U argument used as index to `kstr'
|
|
|
|
** J = S argument used as jump offset (relative to pc of next instruction)
|
2000-03-02 15:32:53 +03:00
|
|
|
** L = U argument used as index of local variable
|
|
|
|
** N = U argument used as index to `knum'
|
2000-02-22 16:31:43 +03:00
|
|
|
*/
|
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
typedef enum {
|
2000-03-03 17:58:26 +03:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
name args stack before stack after side effects
|
|
|
|
------------------------------------------------------------------------*/
|
2000-02-14 19:51:08 +03:00
|
|
|
ENDCODE,/* - - (return) */
|
|
|
|
RETCODE,/* U - (return) */
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
|
|
|
|
TAILCALL,/* A B v_a-v_1 f (return) f(v1,...,v_a) */
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-03-04 23:18:15 +03:00
|
|
|
PUSHNIL,/* U - nil_1-nil_u */
|
2000-02-14 19:51:08 +03:00
|
|
|
POP,/* U a_u-a_1 - */
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
PUSHINT,/* S - (real)s */
|
2000-02-22 16:31:43 +03:00
|
|
|
PUSHSTRING,/* K - KSTR[k] */
|
2000-03-02 15:32:53 +03:00
|
|
|
PUSHNUM,/* N - KNUM[u] */
|
|
|
|
PUSHNEGNUM,/* N - -KNUM[u] */
|
1997-10-24 22:40:29 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
PUSHUPVALUE,/* U - Closure[u] */
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-03-02 15:32:53 +03:00
|
|
|
PUSHLOCAL,/* L - LOC[u] */
|
|
|
|
GETGLOBAL,/* K - VAR[KSTR[k]] */
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
GETTABLE,/* - i t t[i] */
|
2000-02-22 16:31:43 +03:00
|
|
|
GETDOTTED,/* K t t[KSTR[k]] */
|
|
|
|
PUSHSELF,/* K t t t[KSTR[k]] */
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
CREATETABLE,/* U - newarray(size = u) */
|
1997-09-23 00:53:20 +04:00
|
|
|
|
2000-03-02 15:32:53 +03:00
|
|
|
SETLOCAL,/* L x - LOC[u]=x */
|
2000-02-22 16:31:43 +03:00
|
|
|
SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
|
1999-02-26 00:07:26 +03:00
|
|
|
SETTABLEPOP,/* - v i t - t[i]=v */
|
2000-02-22 16:31:43 +03:00
|
|
|
SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */
|
1997-10-14 02:12:04 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */
|
|
|
|
SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */
|
1997-09-16 23:25:59 +04:00
|
|
|
|
1999-02-26 00:07:26 +03:00
|
|
|
ADDOP,/* - y x x+y */
|
2000-02-22 16:31:43 +03:00
|
|
|
ADDI,/* S x x+s */
|
1999-02-26 00:07:26 +03:00
|
|
|
SUBOP,/* - y x x-y */
|
|
|
|
MULTOP,/* - y x x*y */
|
|
|
|
DIVOP,/* - y x x/y */
|
|
|
|
POWOP,/* - y x x^y */
|
2000-03-09 03:19:22 +03:00
|
|
|
CONCOP,/* U v_u-v_1 v1..-..v_u */
|
1999-02-26 00:07:26 +03:00
|
|
|
MINUSOP,/* - x -x */
|
|
|
|
NOTOP,/* - x (x==nil)? 1 : nil */
|
1997-10-06 18:51:11 +04:00
|
|
|
|
2000-03-09 16:57:37 +03:00
|
|
|
IFNEQJMP,/* J y x - (x~=y)? PC+=s */
|
|
|
|
IFEQJMP,/* J y x - (x==y)? PC+=s */
|
|
|
|
IFLTJMP,/* J y x - (x<y)? PC+=s */
|
|
|
|
IFLEJMP,/* J y x - (x<y)? PC+=s */
|
|
|
|
IFGTJMP,/* J y x - (x>y)? PC+=s */
|
|
|
|
IFGEJMP,/* J y x - (x>=y)? PC+=s */
|
|
|
|
|
|
|
|
IFTJMP,/* J x - (x!=nil)? PC+=s */
|
|
|
|
IFFJMP,/* J x - (x==nil)? PC+=s */
|
2000-02-22 16:31:43 +03:00
|
|
|
ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */
|
|
|
|
ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */
|
|
|
|
JMP,/* J - - PC+=s */
|
1997-10-14 02:12:04 +04:00
|
|
|
|
2000-03-09 16:57:37 +03:00
|
|
|
PUSHNILJMP,/* - - nil PC++; */
|
|
|
|
|
|
|
|
CLOSURE,/* A B v_b-v_1 closure(CNST[a], v_1-v_b) */
|
1997-10-14 02:12:04 +04:00
|
|
|
|
2000-02-14 19:51:08 +03:00
|
|
|
SETLINE/* U - - LINE=u */
|
1998-01-12 16:35:37 +03:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
} OpCode;
|
|
|
|
|
|
|
|
|
2000-03-09 16:57:37 +03:00
|
|
|
|
|
|
|
#define ISJUMP(o) (IFNEQJMP <= (o) && (o) <= JMP)
|
|
|
|
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
2000-02-22 16:31:43 +03:00
|
|
|
#define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */
|
1999-02-04 19:36:16 +03:00
|
|
|
|
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#endif
|