lua/lopcodes.h

Go to the documentation of this file.
00001 /*
00002 ** $Id: lopcodes.h,v 1.142 2011/07/15 12:50:29 roberto Exp $
00003 ** Opcodes for Lua virtual machine
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 #ifndef lopcodes_h
00008 #define lopcodes_h
00009 
00010 #include "llimits.h"
00011 
00012 
00013 /*===========================================================================
00014   We assume that instructions are unsigned numbers.
00015   All instructions have an opcode in the first 6 bits.
00016   Instructions can have the following fields:
00017     `A' : 8 bits
00018     `B' : 9 bits
00019     `C' : 9 bits
00020     'Ax' : 26 bits ('A', 'B', and 'C' together)
00021     `Bx' : 18 bits (`B' and `C' together)
00022     `sBx' : signed Bx
00023 
00024   A signed argument is represented in excess K; that is, the number
00025   value is the unsigned value minus K. K is exactly the maximum value
00026   for that argument (so that -max is represented by 0, and +max is
00027   represented by 2*max), which is half the maximum for the corresponding
00028   unsigned argument.
00029 ===========================================================================*/
00030 
00031 
00032 enum OpMode {iABC, iABx, iAsBx, iAx};  /* basic instruction format */
00033 
00034 
00035 /*
00036 ** size and position of opcode arguments.
00037 */
00038 #define SIZE_C      9
00039 #define SIZE_B      9
00040 #define SIZE_Bx     (SIZE_C + SIZE_B)
00041 #define SIZE_A      8
00042 #define SIZE_Ax     (SIZE_C + SIZE_B + SIZE_A)
00043 
00044 #define SIZE_OP     6
00045 
00046 #define POS_OP      0
00047 #define POS_A       (POS_OP + SIZE_OP)
00048 #define POS_C       (POS_A + SIZE_A)
00049 #define POS_B       (POS_C + SIZE_C)
00050 #define POS_Bx      POS_C
00051 #define POS_Ax      POS_A
00052 
00053 
00054 /*
00055 ** limits for opcode arguments.
00056 ** we use (signed) int to manipulate most arguments,
00057 ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
00058 */
00059 #if SIZE_Bx < LUAI_BITSINT-1
00060 #define MAXARG_Bx        ((1<<SIZE_Bx)-1)
00061 #define MAXARG_sBx        (MAXARG_Bx>>1)         /* `sBx' is signed */
00062 #else
00063 #define MAXARG_Bx        MAX_INT
00064 #define MAXARG_sBx        MAX_INT
00065 #endif
00066 
00067 #if SIZE_Ax < LUAI_BITSINT-1
00068 #define MAXARG_Ax   ((1<<SIZE_Ax)-1)
00069 #else
00070 #define MAXARG_Ax   MAX_INT
00071 #endif
00072 
00073 
00074 #define MAXARG_A        ((1<<SIZE_A)-1)
00075 #define MAXARG_B        ((1<<SIZE_B)-1)
00076 #define MAXARG_C        ((1<<SIZE_C)-1)
00077 
00078 
00079 /* creates a mask with `n' 1 bits at position `p' */
00080 #define MASK1(n,p)  ((~((~(Instruction)0)<<(n)))<<(p))
00081 
00082 /* creates a mask with `n' 0 bits at position `p' */
00083 #define MASK0(n,p)  (~MASK1(n,p))
00084 
00085 /*
00086 ** the following macros help to manipulate instructions
00087 */
00088 
00089 #define GET_OPCODE(i)   (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
00090 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
00091         ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
00092 
00093 #define getarg(i,pos,size)  (cast(int, ((i)>>pos) & MASK1(size,0)))
00094 #define setarg(i,v,pos,size)    ((i) = (((i)&MASK0(size,pos)) | \
00095                 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
00096 
00097 #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
00098 #define SETARG_A(i,v)   setarg(i, v, POS_A, SIZE_A)
00099 
00100 #define GETARG_B(i) getarg(i, POS_B, SIZE_B)
00101 #define SETARG_B(i,v)   setarg(i, v, POS_B, SIZE_B)
00102 
00103 #define GETARG_C(i) getarg(i, POS_C, SIZE_C)
00104 #define SETARG_C(i,v)   setarg(i, v, POS_C, SIZE_C)
00105 
00106 #define GETARG_Bx(i)    getarg(i, POS_Bx, SIZE_Bx)
00107 #define SETARG_Bx(i,v)  setarg(i, v, POS_Bx, SIZE_Bx)
00108 
00109 #define GETARG_Ax(i)    getarg(i, POS_Ax, SIZE_Ax)
00110 #define SETARG_Ax(i,v)  setarg(i, v, POS_Ax, SIZE_Ax)
00111 
00112 #define GETARG_sBx(i)   (GETARG_Bx(i)-MAXARG_sBx)
00113 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
00114 
00115 
00116 #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
00117             | (cast(Instruction, a)<<POS_A) \
00118             | (cast(Instruction, b)<<POS_B) \
00119             | (cast(Instruction, c)<<POS_C))
00120 
00121 #define CREATE_ABx(o,a,bc)  ((cast(Instruction, o)<<POS_OP) \
00122             | (cast(Instruction, a)<<POS_A) \
00123             | (cast(Instruction, bc)<<POS_Bx))
00124 
00125 #define CREATE_Ax(o,a)      ((cast(Instruction, o)<<POS_OP) \
00126             | (cast(Instruction, a)<<POS_Ax))
00127 
00128 
00129 /*
00130 ** Macros to operate RK indices
00131 */
00132 
00133 /* this bit 1 means constant (0 means register) */
00134 #define BITRK       (1 << (SIZE_B - 1))
00135 
00136 /* test whether value is a constant */
00137 #define ISK(x)      ((x) & BITRK)
00138 
00139 /* gets the index of the constant */
00140 #define INDEXK(r)   ((int)(r) & ~BITRK)
00141 
00142 #define MAXINDEXRK  (BITRK - 1)
00143 
00144 /* code a constant index as a RK value */
00145 #define RKASK(x)    ((x) | BITRK)
00146 
00147 
00148 /*
00149 ** invalid register that fits in 8 bits
00150 */
00151 #define NO_REG      MAXARG_A
00152 
00153 
00154 /*
00155 ** R(x) - register
00156 ** Kst(x) - constant (in constant table)
00157 ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
00158 */
00159 
00160 
00161 /*
00162 ** grep "ORDER OP" if you change these enums
00163 */
00164 
00165 typedef enum {
00166 /*----------------------------------------------------------------------
00167 name        args    description
00168 ------------------------------------------------------------------------*/
00169 OP_MOVE,/*  A B R(A) := R(B)                    */
00170 OP_LOADK,/* A Bx    R(A) := Kst(Bx)                 */
00171 OP_LOADKX,/*    A   R(A) := Kst(extra arg)              */
00172 OP_LOADBOOL,/*  A B C   R(A) := (Bool)B; if (C) pc++            */
00173 OP_LOADNIL,/*   A B R(A), R(A+1), ..., R(A+B) := nil        */
00174 OP_GETUPVAL,/*  A B R(A) := UpValue[B]              */
00175 
00176 OP_GETTABUP,/*  A B C   R(A) := UpValue[B][RK(C)]           */
00177 OP_GETTABLE,/*  A B C   R(A) := R(B)[RK(C)]             */
00178 
00179 OP_SETTABUP,/*  A B C   UpValue[A][RK(B)] := RK(C)          */
00180 OP_SETUPVAL,/*  A B UpValue[B] := R(A)              */
00181 OP_SETTABLE,/*  A B C   R(A)[RK(B)] := RK(C)                */
00182 
00183 OP_NEWTABLE,/*  A B C   R(A) := {} (size = B,C)             */
00184 
00185 OP_SELF,/*  A B C   R(A+1) := R(B); R(A) := R(B)[RK(C)]     */
00186 
00187 OP_ADD,/*   A B C   R(A) := RK(B) + RK(C)               */
00188 OP_SUB,/*   A B C   R(A) := RK(B) - RK(C)               */
00189 OP_MUL,/*   A B C   R(A) := RK(B) * RK(C)               */
00190 OP_DIV,/*   A B C   R(A) := RK(B) / RK(C)               */
00191 OP_MOD,/*   A B C   R(A) := RK(B) % RK(C)               */
00192 OP_POW,/*   A B C   R(A) := RK(B) ^ RK(C)               */
00193 OP_UNM,/*   A B R(A) := -R(B)                   */
00194 OP_NOT,/*   A B R(A) := not R(B)                */
00195 OP_LEN,/*   A B R(A) := length of R(B)              */
00196 
00197 OP_CONCAT,/*    A B C   R(A) := R(B).. ... ..R(C)           */
00198 
00199 OP_JMP,/*   A sBx   pc+=sBx; if (A) close all upvalues >= R(A) + 1  */
00200 OP_EQ,/*    A B C   if ((RK(B) == RK(C)) ~= A) then pc++        */
00201 OP_LT,/*    A B C   if ((RK(B) <  RK(C)) ~= A) then pc++        */
00202 OP_LE,/*    A B C   if ((RK(B) <= RK(C)) ~= A) then pc++        */
00203 
00204 OP_TEST,/*  A C if not (R(A) <=> C) then pc++           */
00205 OP_TESTSET,/*   A B C   if (R(B) <=> C) then R(A) := R(B) else pc++ */
00206 
00207 OP_CALL,/*  A B C   R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
00208 OP_TAILCALL,/*  A B C   return R(A)(R(A+1), ... ,R(A+B-1))      */
00209 OP_RETURN,/*    A B return R(A), ... ,R(A+B-2)  (see note)  */
00210 
00211 OP_FORLOOP,/*   A sBx   R(A)+=R(A+2);
00212             if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
00213 OP_FORPREP,/*   A sBx   R(A)-=R(A+2); pc+=sBx               */
00214 
00215 OP_TFORCALL,/*  A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));  */
00216 OP_TFORLOOP,/*  A sBx   if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
00217 
00218 OP_SETLIST,/*   A B C   R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B    */
00219 
00220 OP_CLOSURE,/*   A Bx    R(A) := closure(KPROTO[Bx])         */
00221 
00222 OP_VARARG,/*    A B R(A), R(A+1), ..., R(A+B-2) = vararg        */
00223 
00224 OP_EXTRAARG/*   Ax  extra (larger) argument for previous opcode */
00225 } OpCode;
00226 
00227 
00228 #define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1)
00229 
00230 
00231 
00232 /*===========================================================================
00233   Notes:
00234   (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is
00235   set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
00236   OP_SETLIST) may use `top'.
00237 
00238   (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
00239   set top (like in OP_CALL with C == 0).
00240 
00241   (*) In OP_RETURN, if (B == 0) then return up to `top'.
00242 
00243   (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
00244   'instruction' is EXTRAARG(real C).
00245 
00246   (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
00247 
00248   (*) For comparisons, A specifies what condition the test should accept
00249   (true or false).
00250 
00251   (*) All `skips' (pc++) assume that next instruction is a jump.
00252 
00253 ===========================================================================*/
00254 
00255 
00256 /*
00257 ** masks for instruction properties. The format is:
00258 ** bits 0-1: op mode
00259 ** bits 2-3: C arg mode
00260 ** bits 4-5: B arg mode
00261 ** bit 6: instruction set register A
00262 ** bit 7: operator is a test (next instruction must be a jump)
00263 */
00264 
00265 enum OpArgMask {
00266   OpArgN,  /* argument is not used */
00267   OpArgU,  /* argument is used */
00268   OpArgR,  /* argument is a register or a jump offset */
00269   OpArgK   /* argument is a constant or register/constant */
00270 };
00271 
00272 LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
00273 
00274 #define getOpMode(m)    (cast(enum OpMode, luaP_opmodes[m] & 3))
00275 #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
00276 #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
00277 #define testAMode(m)    (luaP_opmodes[m] & (1 << 6))
00278 #define testTMode(m)    (luaP_opmodes[m] & (1 << 7))
00279 
00280 
00281 LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */
00282 
00283 
00284 /* number of list items to accumulate before a SETLIST instruction */
00285 #define LFIELDS_PER_FLUSH   50
00286 
00287 
00288 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:04 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs