- Main Page
- Classes
- Files

# lopcodes.h

Go to the documentation of this file.

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