misc/liblua/lopcodes.h
changeset 10017 de822cd3df3a
parent 3697 d5b30d6373fc
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    12 
    12 
    13 /*===========================================================================
    13 /*===========================================================================
    14   We assume that instructions are unsigned numbers.
    14   We assume that instructions are unsigned numbers.
    15   All instructions have an opcode in the first 6 bits.
    15   All instructions have an opcode in the first 6 bits.
    16   Instructions can have the following fields:
    16   Instructions can have the following fields:
    17 	`A' : 8 bits
    17     `A' : 8 bits
    18 	`B' : 9 bits
    18     `B' : 9 bits
    19 	`C' : 9 bits
    19     `C' : 9 bits
    20 	`Bx' : 18 bits (`B' and `C' together)
    20     `Bx' : 18 bits (`B' and `C' together)
    21 	`sBx' : signed Bx
    21     `sBx' : signed Bx
    22 
    22 
    23   A signed argument is represented in excess K; that is, the number
    23   A signed argument is represented in excess K; that is, the number
    24   value is the unsigned value minus K. K is exactly the maximum value
    24   value is the unsigned value minus K. K is exactly the maximum value
    25   for that argument (so that -max is represented by 0, and +max is
    25   for that argument (so that -max is represented by 0, and +max is
    26   represented by 2*max), which is half the maximum for the corresponding
    26   represented by 2*max), which is half the maximum for the corresponding
    32 
    32 
    33 
    33 
    34 /*
    34 /*
    35 ** size and position of opcode arguments.
    35 ** size and position of opcode arguments.
    36 */
    36 */
    37 #define SIZE_C		9
    37 #define SIZE_C      9
    38 #define SIZE_B		9
    38 #define SIZE_B      9
    39 #define SIZE_Bx		(SIZE_C + SIZE_B)
    39 #define SIZE_Bx     (SIZE_C + SIZE_B)
    40 #define SIZE_A		8
    40 #define SIZE_A      8
    41 
    41 
    42 #define SIZE_OP		6
    42 #define SIZE_OP     6
    43 
    43 
    44 #define POS_OP		0
    44 #define POS_OP      0
    45 #define POS_A		(POS_OP + SIZE_OP)
    45 #define POS_A       (POS_OP + SIZE_OP)
    46 #define POS_C		(POS_A + SIZE_A)
    46 #define POS_C       (POS_A + SIZE_A)
    47 #define POS_B		(POS_C + SIZE_C)
    47 #define POS_B       (POS_C + SIZE_C)
    48 #define POS_Bx		POS_C
    48 #define POS_Bx      POS_C
    49 
    49 
    50 
    50 
    51 /*
    51 /*
    52 ** limits for opcode arguments.
    52 ** limits for opcode arguments.
    53 ** we use (signed) int to manipulate most arguments,
    53 ** we use (signed) int to manipulate most arguments,
    66 #define MAXARG_B        ((1<<SIZE_B)-1)
    66 #define MAXARG_B        ((1<<SIZE_B)-1)
    67 #define MAXARG_C        ((1<<SIZE_C)-1)
    67 #define MAXARG_C        ((1<<SIZE_C)-1)
    68 
    68 
    69 
    69 
    70 /* creates a mask with `n' 1 bits at position `p' */
    70 /* creates a mask with `n' 1 bits at position `p' */
    71 #define MASK1(n,p)	((~((~(Instruction)0)<<n))<<p)
    71 #define MASK1(n,p)  ((~((~(Instruction)0)<<n))<<p)
    72 
    72 
    73 /* creates a mask with `n' 0 bits at position `p' */
    73 /* creates a mask with `n' 0 bits at position `p' */
    74 #define MASK0(n,p)	(~MASK1(n,p))
    74 #define MASK0(n,p)  (~MASK1(n,p))
    75 
    75 
    76 /*
    76 /*
    77 ** the following macros help to manipulate instructions
    77 ** the following macros help to manipulate instructions
    78 */
    78 */
    79 
    79 
    80 #define GET_OPCODE(i)	(cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
    80 #define GET_OPCODE(i)   (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
    81 #define SET_OPCODE(i,o)	((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
    81 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
    82 		((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
    82         ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
    83 
    83 
    84 #define GETARG_A(i)	(cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))
    84 #define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))
    85 #define SETARG_A(i,u)	((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
    85 #define SETARG_A(i,u)   ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
    86 		((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
    86         ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
    87 
    87 
    88 #define GETARG_B(i)	(cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
    88 #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
    89 #define SETARG_B(i,b)	((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
    89 #define SETARG_B(i,b)   ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
    90 		((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
    90         ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
    91 
    91 
    92 #define GETARG_C(i)	(cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
    92 #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
    93 #define SETARG_C(i,b)	((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
    93 #define SETARG_C(i,b)   ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
    94 		((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
    94         ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
    95 
    95 
    96 #define GETARG_Bx(i)	(cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
    96 #define GETARG_Bx(i)    (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
    97 #define SETARG_Bx(i,b)	((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
    97 #define SETARG_Bx(i,b)  ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
    98 		((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
    98         ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
    99 
    99 
   100 #define GETARG_sBx(i)	(GETARG_Bx(i)-MAXARG_sBx)
   100 #define GETARG_sBx(i)   (GETARG_Bx(i)-MAXARG_sBx)
   101 #define SETARG_sBx(i,b)	SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
   101 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
   102 
   102 
   103 
   103 
   104 #define CREATE_ABC(o,a,b,c)	((cast(Instruction, o)<<POS_OP) \
   104 #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
   105 			| (cast(Instruction, a)<<POS_A) \
   105             | (cast(Instruction, a)<<POS_A) \
   106 			| (cast(Instruction, b)<<POS_B) \
   106             | (cast(Instruction, b)<<POS_B) \
   107 			| (cast(Instruction, c)<<POS_C))
   107             | (cast(Instruction, c)<<POS_C))
   108 
   108 
   109 #define CREATE_ABx(o,a,bc)	((cast(Instruction, o)<<POS_OP) \
   109 #define CREATE_ABx(o,a,bc)  ((cast(Instruction, o)<<POS_OP) \
   110 			| (cast(Instruction, a)<<POS_A) \
   110             | (cast(Instruction, a)<<POS_A) \
   111 			| (cast(Instruction, bc)<<POS_Bx))
   111             | (cast(Instruction, bc)<<POS_Bx))
   112 
   112 
   113 
   113 
   114 /*
   114 /*
   115 ** Macros to operate RK indices
   115 ** Macros to operate RK indices
   116 */
   116 */
   117 
   117 
   118 /* this bit 1 means constant (0 means register) */
   118 /* this bit 1 means constant (0 means register) */
   119 #define BITRK		(1 << (SIZE_B - 1))
   119 #define BITRK       (1 << (SIZE_B - 1))
   120 
   120 
   121 /* test whether value is a constant */
   121 /* test whether value is a constant */
   122 #define ISK(x)		((x) & BITRK)
   122 #define ISK(x)      ((x) & BITRK)
   123 
   123 
   124 /* gets the index of the constant */
   124 /* gets the index of the constant */
   125 #define INDEXK(r)	((int)(r) & ~BITRK)
   125 #define INDEXK(r)   ((int)(r) & ~BITRK)
   126 
   126 
   127 #define MAXINDEXRK	(BITRK - 1)
   127 #define MAXINDEXRK  (BITRK - 1)
   128 
   128 
   129 /* code a constant index as a RK value */
   129 /* code a constant index as a RK value */
   130 #define RKASK(x)	((x) | BITRK)
   130 #define RKASK(x)    ((x) | BITRK)
   131 
   131 
   132 
   132 
   133 /*
   133 /*
   134 ** invalid register that fits in 8 bits
   134 ** invalid register that fits in 8 bits
   135 */
   135 */
   136 #define NO_REG		MAXARG_A
   136 #define NO_REG      MAXARG_A
   137 
   137 
   138 
   138 
   139 /*
   139 /*
   140 ** R(x) - register
   140 ** R(x) - register
   141 ** Kst(x) - constant (in constant table)
   141 ** Kst(x) - constant (in constant table)
   147 ** grep "ORDER OP" if you change these enums
   147 ** grep "ORDER OP" if you change these enums
   148 */
   148 */
   149 
   149 
   150 typedef enum {
   150 typedef enum {
   151 /*----------------------------------------------------------------------
   151 /*----------------------------------------------------------------------
   152 name		args	description
   152 name        args    description
   153 ------------------------------------------------------------------------*/
   153 ------------------------------------------------------------------------*/
   154 OP_MOVE,/*	A B	R(A) := R(B)					*/
   154 OP_MOVE,/*  A B R(A) := R(B)                    */
   155 OP_LOADK,/*	A Bx	R(A) := Kst(Bx)					*/
   155 OP_LOADK,/* A Bx    R(A) := Kst(Bx)                 */
   156 OP_LOADBOOL,/*	A B C	R(A) := (Bool)B; if (C) pc++			*/
   156 OP_LOADBOOL,/*  A B C   R(A) := (Bool)B; if (C) pc++            */
   157 OP_LOADNIL,/*	A B	R(A) := ... := R(B) := nil			*/
   157 OP_LOADNIL,/*   A B R(A) := ... := R(B) := nil          */
   158 OP_GETUPVAL,/*	A B	R(A) := UpValue[B]				*/
   158 OP_GETUPVAL,/*  A B R(A) := UpValue[B]              */
   159 
   159 
   160 OP_GETGLOBAL,/*	A Bx	R(A) := Gbl[Kst(Bx)]				*/
   160 OP_GETGLOBAL,/* A Bx    R(A) := Gbl[Kst(Bx)]                */
   161 OP_GETTABLE,/*	A B C	R(A) := R(B)[RK(C)]				*/
   161 OP_GETTABLE,/*  A B C   R(A) := R(B)[RK(C)]             */
   162 
   162 
   163 OP_SETGLOBAL,/*	A Bx	Gbl[Kst(Bx)] := R(A)				*/
   163 OP_SETGLOBAL,/* A Bx    Gbl[Kst(Bx)] := R(A)                */
   164 OP_SETUPVAL,/*	A B	UpValue[B] := R(A)				*/
   164 OP_SETUPVAL,/*  A B UpValue[B] := R(A)              */
   165 OP_SETTABLE,/*	A B C	R(A)[RK(B)] := RK(C)				*/
   165 OP_SETTABLE,/*  A B C   R(A)[RK(B)] := RK(C)                */
   166 
   166 
   167 OP_NEWTABLE,/*	A B C	R(A) := {} (size = B,C)				*/
   167 OP_NEWTABLE,/*  A B C   R(A) := {} (size = B,C)             */
   168 
   168 
   169 OP_SELF,/*	A B C	R(A+1) := R(B); R(A) := R(B)[RK(C)]		*/
   169 OP_SELF,/*  A B C   R(A+1) := R(B); R(A) := R(B)[RK(C)]     */
   170 
   170 
   171 OP_ADD,/*	A B C	R(A) := RK(B) + RK(C)				*/
   171 OP_ADD,/*   A B C   R(A) := RK(B) + RK(C)               */
   172 OP_SUB,/*	A B C	R(A) := RK(B) - RK(C)				*/
   172 OP_SUB,/*   A B C   R(A) := RK(B) - RK(C)               */
   173 OP_MUL,/*	A B C	R(A) := RK(B) * RK(C)				*/
   173 OP_MUL,/*   A B C   R(A) := RK(B) * RK(C)               */
   174 OP_DIV,/*	A B C	R(A) := RK(B) / RK(C)				*/
   174 OP_DIV,/*   A B C   R(A) := RK(B) / RK(C)               */
   175 OP_MOD,/*	A B C	R(A) := RK(B) % RK(C)				*/
   175 OP_MOD,/*   A B C   R(A) := RK(B) % RK(C)               */
   176 OP_POW,/*	A B C	R(A) := RK(B) ^ RK(C)				*/
   176 OP_POW,/*   A B C   R(A) := RK(B) ^ RK(C)               */
   177 OP_UNM,/*	A B	R(A) := -R(B)					*/
   177 OP_UNM,/*   A B R(A) := -R(B)                   */
   178 OP_NOT,/*	A B	R(A) := not R(B)				*/
   178 OP_NOT,/*   A B R(A) := not R(B)                */
   179 OP_LEN,/*	A B	R(A) := length of R(B)				*/
   179 OP_LEN,/*   A B R(A) := length of R(B)              */
   180 
   180 
   181 OP_CONCAT,/*	A B C	R(A) := R(B).. ... ..R(C)			*/
   181 OP_CONCAT,/*    A B C   R(A) := R(B).. ... ..R(C)           */
   182 
   182 
   183 OP_JMP,/*	sBx	pc+=sBx					*/
   183 OP_JMP,/*   sBx pc+=sBx                 */
   184 
   184 
   185 OP_EQ,/*	A B C	if ((RK(B) == RK(C)) ~= A) then pc++		*/
   185 OP_EQ,/*    A B C   if ((RK(B) == RK(C)) ~= A) then pc++        */
   186 OP_LT,/*	A B C	if ((RK(B) <  RK(C)) ~= A) then pc++  		*/
   186 OP_LT,/*    A B C   if ((RK(B) <  RK(C)) ~= A) then pc++        */
   187 OP_LE,/*	A B C	if ((RK(B) <= RK(C)) ~= A) then pc++  		*/
   187 OP_LE,/*    A B C   if ((RK(B) <= RK(C)) ~= A) then pc++        */
   188 
   188 
   189 OP_TEST,/*	A C	if not (R(A) <=> C) then pc++			*/
   189 OP_TEST,/*  A C if not (R(A) <=> C) then pc++           */
   190 OP_TESTSET,/*	A B C	if (R(B) <=> C) then R(A) := R(B) else pc++	*/
   190 OP_TESTSET,/*   A B C   if (R(B) <=> C) then R(A) := R(B) else pc++ */
   191 
   191 
   192 OP_CALL,/*	A B C	R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
   192 OP_CALL,/*  A B C   R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
   193 OP_TAILCALL,/*	A B C	return R(A)(R(A+1), ... ,R(A+B-1))		*/
   193 OP_TAILCALL,/*  A B C   return R(A)(R(A+1), ... ,R(A+B-1))      */
   194 OP_RETURN,/*	A B	return R(A), ... ,R(A+B-2)	(see note)	*/
   194 OP_RETURN,/*    A B return R(A), ... ,R(A+B-2)  (see note)  */
   195 
   195 
   196 OP_FORLOOP,/*	A sBx	R(A)+=R(A+2);
   196 OP_FORLOOP,/*   A sBx   R(A)+=R(A+2);
   197 			if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
   197             if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
   198 OP_FORPREP,/*	A sBx	R(A)-=R(A+2); pc+=sBx				*/
   198 OP_FORPREP,/*   A sBx   R(A)-=R(A+2); pc+=sBx               */
   199 
   199 
   200 OP_TFORLOOP,/*	A C	R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
   200 OP_TFORLOOP,/*  A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
   201                         if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++	*/
   201                         if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++   */
   202 OP_SETLIST,/*	A B C	R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B	*/
   202 OP_SETLIST,/*   A B C   R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B    */
   203 
   203 
   204 OP_CLOSE,/*	A 	close all variables in the stack up to (>=) R(A)*/
   204 OP_CLOSE,/* A   close all variables in the stack up to (>=) R(A)*/
   205 OP_CLOSURE,/*	A Bx	R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))	*/
   205 OP_CLOSURE,/*   A Bx    R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))  */
   206 
   206 
   207 OP_VARARG/*	A B	R(A), R(A+1), ..., R(A+B-1) = vararg		*/
   207 OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg        */
   208 } OpCode;
   208 } OpCode;
   209 
   209 
   210 
   210 
   211 #define NUM_OPCODES	(cast(int, OP_VARARG) + 1)
   211 #define NUM_OPCODES (cast(int, OP_VARARG) + 1)
   212 
   212 
   213 
   213 
   214 
   214 
   215 /*===========================================================================
   215 /*===========================================================================
   216   Notes:
   216   Notes:
   249   OpArgK   /* argument is a constant or register/constant */
   249   OpArgK   /* argument is a constant or register/constant */
   250 };
   250 };
   251 
   251 
   252 LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
   252 LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
   253 
   253 
   254 #define getOpMode(m)	(cast(enum OpMode, luaP_opmodes[m] & 3))
   254 #define getOpMode(m)    (cast(enum OpMode, luaP_opmodes[m] & 3))
   255 #define getBMode(m)	(cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
   255 #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
   256 #define getCMode(m)	(cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
   256 #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
   257 #define testAMode(m)	(luaP_opmodes[m] & (1 << 6))
   257 #define testAMode(m)    (luaP_opmodes[m] & (1 << 6))
   258 #define testTMode(m)	(luaP_opmodes[m] & (1 << 7))
   258 #define testTMode(m)    (luaP_opmodes[m] & (1 << 7))
   259 
   259 
   260 
   260 
   261 LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */
   261 LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */
   262 
   262 
   263 
   263 
   264 /* number of list items to accumulate before a SETLIST instruction */
   264 /* number of list items to accumulate before a SETLIST instruction */
   265 #define LFIELDS_PER_FLUSH	50
   265 #define LFIELDS_PER_FLUSH   50
   266 
   266 
   267 
   267 
   268 #endif
   268 #endif