Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:37:57

0001 /*
0002 ** $Id: lopcodes.h $
0003 ** Opcodes for Lua virtual machine
0004 ** See Copyright Notice in lua.h
0005 */
0006 
0007 #ifndef lopcodes_h
0008 #define lopcodes_h
0009 
0010 #include "llimits.h"
0011 
0012 
0013 /*===========================================================================
0014   We assume that instructions are unsigned 32-bit integers.
0015   All instructions have an opcode in the first 7 bits.
0016   Instructions can have the following formats:
0017 
0018         3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0019         1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
0020 iABC          C(8)     |      B(8)     |k|     A(8)      |   Op(7)     |
0021 iABx                Bx(17)               |     A(8)      |   Op(7)     |
0022 iAsBx              sBx (signed)(17)      |     A(8)      |   Op(7)     |
0023 iAx                           Ax(25)                     |   Op(7)     |
0024 isJ                           sJ (signed)(25)            |   Op(7)     |
0025 
0026   A signed argument is represented in excess K: the represented value is
0027   the written unsigned value minus K, where K is half the maximum for the
0028   corresponding unsigned argument.
0029 ===========================================================================*/
0030 
0031 
0032 enum OpMode {iABC, iABx, iAsBx, iAx, isJ};  /* basic instruction formats */
0033 
0034 
0035 /*
0036 ** size and position of opcode arguments.
0037 */
0038 #define SIZE_C      8
0039 #define SIZE_B      8
0040 #define SIZE_Bx     (SIZE_C + SIZE_B + 1)
0041 #define SIZE_A      8
0042 #define SIZE_Ax     (SIZE_Bx + SIZE_A)
0043 #define SIZE_sJ     (SIZE_Bx + SIZE_A)
0044 
0045 #define SIZE_OP     7
0046 
0047 #define POS_OP      0
0048 
0049 #define POS_A       (POS_OP + SIZE_OP)
0050 #define POS_k       (POS_A + SIZE_A)
0051 #define POS_B       (POS_k + 1)
0052 #define POS_C       (POS_B + SIZE_B)
0053 
0054 #define POS_Bx      POS_k
0055 
0056 #define POS_Ax      POS_A
0057 
0058 #define POS_sJ      POS_A
0059 
0060 
0061 /*
0062 ** limits for opcode arguments.
0063 ** we use (signed) 'int' to manipulate most arguments,
0064 ** so they must fit in ints.
0065 */
0066 
0067 /* Check whether type 'int' has at least 'b' bits ('b' < 32) */
0068 #define L_INTHASBITS(b)     ((UINT_MAX >> ((b) - 1)) >= 1)
0069 
0070 
0071 #if L_INTHASBITS(SIZE_Bx)
0072 #define MAXARG_Bx   ((1<<SIZE_Bx)-1)
0073 #else
0074 #define MAXARG_Bx   MAX_INT
0075 #endif
0076 
0077 #define OFFSET_sBx  (MAXARG_Bx>>1)         /* 'sBx' is signed */
0078 
0079 
0080 #if L_INTHASBITS(SIZE_Ax)
0081 #define MAXARG_Ax   ((1<<SIZE_Ax)-1)
0082 #else
0083 #define MAXARG_Ax   MAX_INT
0084 #endif
0085 
0086 #if L_INTHASBITS(SIZE_sJ)
0087 #define MAXARG_sJ   ((1 << SIZE_sJ) - 1)
0088 #else
0089 #define MAXARG_sJ   MAX_INT
0090 #endif
0091 
0092 #define OFFSET_sJ   (MAXARG_sJ >> 1)
0093 
0094 
0095 #define MAXARG_A    ((1<<SIZE_A)-1)
0096 #define MAXARG_B    ((1<<SIZE_B)-1)
0097 #define MAXARG_C    ((1<<SIZE_C)-1)
0098 #define OFFSET_sC   (MAXARG_C >> 1)
0099 
0100 #define int2sC(i)   ((i) + OFFSET_sC)
0101 #define sC2int(i)   ((i) - OFFSET_sC)
0102 
0103 
0104 /* creates a mask with 'n' 1 bits at position 'p' */
0105 #define MASK1(n,p)  ((~((~(Instruction)0)<<(n)))<<(p))
0106 
0107 /* creates a mask with 'n' 0 bits at position 'p' */
0108 #define MASK0(n,p)  (~MASK1(n,p))
0109 
0110 /*
0111 ** the following macros help to manipulate instructions
0112 */
0113 
0114 #define GET_OPCODE(i)   (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
0115 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
0116         ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
0117 
0118 #define checkopm(i,m)   (getOpMode(GET_OPCODE(i)) == m)
0119 
0120 
0121 #define getarg(i,pos,size)  (cast_int(((i)>>(pos)) & MASK1(size,0)))
0122 #define setarg(i,v,pos,size)    ((i) = (((i)&MASK0(size,pos)) | \
0123                 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
0124 
0125 #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
0126 #define SETARG_A(i,v)   setarg(i, v, POS_A, SIZE_A)
0127 
0128 #define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
0129 #define GETARG_sB(i)    sC2int(GETARG_B(i))
0130 #define SETARG_B(i,v)   setarg(i, v, POS_B, SIZE_B)
0131 
0132 #define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
0133 #define GETARG_sC(i)    sC2int(GETARG_C(i))
0134 #define SETARG_C(i,v)   setarg(i, v, POS_C, SIZE_C)
0135 
0136 #define TESTARG_k(i)    check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
0137 #define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
0138 #define SETARG_k(i,v)   setarg(i, v, POS_k, 1)
0139 
0140 #define GETARG_Bx(i)    check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
0141 #define SETARG_Bx(i,v)  setarg(i, v, POS_Bx, SIZE_Bx)
0142 
0143 #define GETARG_Ax(i)    check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
0144 #define SETARG_Ax(i,v)  setarg(i, v, POS_Ax, SIZE_Ax)
0145 
0146 #define GETARG_sBx(i)  \
0147     check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
0148 #define SETARG_sBx(i,b) SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))
0149 
0150 #define GETARG_sJ(i)  \
0151     check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
0152 #define SETARG_sJ(i,j) \
0153     setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
0154 
0155 
0156 #define CREATE_ABCk(o,a,b,c,k)  ((cast(Instruction, o)<<POS_OP) \
0157             | (cast(Instruction, a)<<POS_A) \
0158             | (cast(Instruction, b)<<POS_B) \
0159             | (cast(Instruction, c)<<POS_C) \
0160             | (cast(Instruction, k)<<POS_k))
0161 
0162 #define CREATE_ABx(o,a,bc)  ((cast(Instruction, o)<<POS_OP) \
0163             | (cast(Instruction, a)<<POS_A) \
0164             | (cast(Instruction, bc)<<POS_Bx))
0165 
0166 #define CREATE_Ax(o,a)      ((cast(Instruction, o)<<POS_OP) \
0167             | (cast(Instruction, a)<<POS_Ax))
0168 
0169 #define CREATE_sJ(o,j,k)    ((cast(Instruction, o) << POS_OP) \
0170             | (cast(Instruction, j) << POS_sJ) \
0171             | (cast(Instruction, k) << POS_k))
0172 
0173 
0174 #if !defined(MAXINDEXRK)  /* (for debugging only) */
0175 #define MAXINDEXRK  MAXARG_B
0176 #endif
0177 
0178 
0179 /*
0180 ** invalid register that fits in 8 bits
0181 */
0182 #define NO_REG      MAXARG_A
0183 
0184 
0185 /*
0186 ** R[x] - register
0187 ** K[x] - constant (in constant table)
0188 ** RK(x) == if k(i) then K[x] else R[x]
0189 */
0190 
0191 
0192 /*
0193 ** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
0194 ** has extra descriptions in the notes after the enumeration.
0195 */
0196 
0197 typedef enum {
0198 /*----------------------------------------------------------------------
0199   name      args    description
0200 ------------------------------------------------------------------------*/
0201 OP_MOVE,/*  A B R[A] := R[B]                    */
0202 OP_LOADI,/* A sBx   R[A] := sBx                 */
0203 OP_LOADF,/* A sBx   R[A] := (lua_Number)sBx             */
0204 OP_LOADK,/* A Bx    R[A] := K[Bx]                   */
0205 OP_LOADKX,/*    A   R[A] := K[extra arg]                */
0206 OP_LOADFALSE,/* A   R[A] := false                   */
0207 OP_LFALSESKIP,/*A   R[A] := false; pc++ (*)         */
0208 OP_LOADTRUE,/*  A   R[A] := true                    */
0209 OP_LOADNIL,/*   A B R[A], R[A+1], ..., R[A+B] := nil        */
0210 OP_GETUPVAL,/*  A B R[A] := UpValue[B]              */
0211 OP_SETUPVAL,/*  A B UpValue[B] := R[A]              */
0212 
0213 OP_GETTABUP,/*  A B C   R[A] := UpValue[B][K[C]:shortstring]        */
0214 OP_GETTABLE,/*  A B C   R[A] := R[B][R[C]]              */
0215 OP_GETI,/*  A B C   R[A] := R[B][C]                 */
0216 OP_GETFIELD,/*  A B C   R[A] := R[B][K[C]:shortstring]          */
0217 
0218 OP_SETTABUP,/*  A B C   UpValue[A][K[B]:shortstring] := RK(C)       */
0219 OP_SETTABLE,/*  A B C   R[A][R[B]] := RK(C)             */
0220 OP_SETI,/*  A B C   R[A][B] := RK(C)                */
0221 OP_SETFIELD,/*  A B C   R[A][K[B]:shortstring] := RK(C)         */
0222 
0223 OP_NEWTABLE,/*  A B C k R[A] := {}                  */
0224 
0225 OP_SELF,/*  A B C   R[A+1] := R[B]; R[A] := R[B][RK(C):string]  */
0226 
0227 OP_ADDI,/*  A B sC  R[A] := R[B] + sC               */
0228 
0229 OP_ADDK,/*  A B C   R[A] := R[B] + K[C]:number          */
0230 OP_SUBK,/*  A B C   R[A] := R[B] - K[C]:number          */
0231 OP_MULK,/*  A B C   R[A] := R[B] * K[C]:number          */
0232 OP_MODK,/*  A B C   R[A] := R[B] % K[C]:number          */
0233 OP_POWK,/*  A B C   R[A] := R[B] ^ K[C]:number          */
0234 OP_DIVK,/*  A B C   R[A] := R[B] / K[C]:number          */
0235 OP_IDIVK,/* A B C   R[A] := R[B] // K[C]:number         */
0236 
0237 OP_BANDK,/* A B C   R[A] := R[B] & K[C]:integer         */
0238 OP_BORK,/*  A B C   R[A] := R[B] | K[C]:integer         */
0239 OP_BXORK,/* A B C   R[A] := R[B] ~ K[C]:integer         */
0240 
0241 OP_SHRI,/*  A B sC  R[A] := R[B] >> sC              */
0242 OP_SHLI,/*  A B sC  R[A] := sC << R[B]              */
0243 
0244 OP_ADD,/*   A B C   R[A] := R[B] + R[C]             */
0245 OP_SUB,/*   A B C   R[A] := R[B] - R[C]             */
0246 OP_MUL,/*   A B C   R[A] := R[B] * R[C]             */
0247 OP_MOD,/*   A B C   R[A] := R[B] % R[C]             */
0248 OP_POW,/*   A B C   R[A] := R[B] ^ R[C]             */
0249 OP_DIV,/*   A B C   R[A] := R[B] / R[C]             */
0250 OP_IDIV,/*  A B C   R[A] := R[B] // R[C]                */
0251 
0252 OP_BAND,/*  A B C   R[A] := R[B] & R[C]             */
0253 OP_BOR,/*   A B C   R[A] := R[B] | R[C]             */
0254 OP_BXOR,/*  A B C   R[A] := R[B] ~ R[C]             */
0255 OP_SHL,/*   A B C   R[A] := R[B] << R[C]                */
0256 OP_SHR,/*   A B C   R[A] := R[B] >> R[C]                */
0257 
0258 OP_MMBIN,/* A B C   call C metamethod over R[A] and R[B]    (*) */
0259 OP_MMBINI,/*    A sB C k    call C metamethod over R[A] and sB  */
0260 OP_MMBINK,/*    A B C k     call C metamethod over R[A] and K[B]    */
0261 
0262 OP_UNM,/*   A B R[A] := -R[B]                   */
0263 OP_BNOT,/*  A B R[A] := ~R[B]                   */
0264 OP_NOT,/*   A B R[A] := not R[B]                */
0265 OP_LEN,/*   A B R[A] := #R[B] (length operator)         */
0266 
0267 OP_CONCAT,/*    A B R[A] := R[A].. ... ..R[A + B - 1]       */
0268 
0269 OP_CLOSE,/* A   close all upvalues >= R[A]          */
0270 OP_TBC,/*   A   mark variable A "to be closed"          */
0271 OP_JMP,/*   sJ  pc += sJ                    */
0272 OP_EQ,/*    A B k   if ((R[A] == R[B]) ~= k) then pc++      */
0273 OP_LT,/*    A B k   if ((R[A] <  R[B]) ~= k) then pc++      */
0274 OP_LE,/*    A B k   if ((R[A] <= R[B]) ~= k) then pc++      */
0275 
0276 OP_EQK,/*   A B k   if ((R[A] == K[B]) ~= k) then pc++      */
0277 OP_EQI,/*   A sB k  if ((R[A] == sB) ~= k) then pc++        */
0278 OP_LTI,/*   A sB k  if ((R[A] < sB) ~= k) then pc++         */
0279 OP_LEI,/*   A sB k  if ((R[A] <= sB) ~= k) then pc++        */
0280 OP_GTI,/*   A sB k  if ((R[A] > sB) ~= k) then pc++         */
0281 OP_GEI,/*   A sB k  if ((R[A] >= sB) ~= k) then pc++        */
0282 
0283 OP_TEST,/*  A k if (not R[A] == k) then pc++            */
0284 OP_TESTSET,/*   A B k   if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
0285 
0286 OP_CALL,/*  A B C   R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
0287 OP_TAILCALL,/*  A B C k return R[A](R[A+1], ... ,R[A+B-1])      */
0288 
0289 OP_RETURN,/*    A B C k return R[A], ... ,R[A+B-2]  (see note)  */
0290 OP_RETURN0,/*       return                      */
0291 OP_RETURN1,/*   A   return R[A]                 */
0292 
0293 OP_FORLOOP,/*   A Bx    update counters; if loop continues then pc-=Bx; */
0294 OP_FORPREP,/*   A Bx    <check values and prepare counters>;
0295                         if not to run then pc+=Bx+1;            */
0296 
0297 OP_TFORPREP,/*  A Bx    create upvalue for R[A + 3]; pc+=Bx     */
0298 OP_TFORCALL,/*  A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]);  */
0299 OP_TFORLOOP,/*  A Bx    if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
0300 
0301 OP_SETLIST,/*   A B C k R[A][C+i] := R[A+i], 1 <= i <= B        */
0302 
0303 OP_CLOSURE,/*   A Bx    R[A] := closure(KPROTO[Bx])         */
0304 
0305 OP_VARARG,/*    A C R[A], R[A+1], ..., R[A+C-2] = vararg        */
0306 
0307 OP_VARARGPREP,/*A   (adjust vararg parameters)          */
0308 
0309 OP_EXTRAARG/*   Ax  extra (larger) argument for previous opcode */
0310 } OpCode;
0311 
0312 
0313 #define NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
0314 
0315 
0316 
0317 /*===========================================================================
0318   Notes:
0319 
0320   (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
0321   value, in a code equivalent to (not cond ? false : true).  (It
0322   produces false and skips the next instruction producing true.)
0323 
0324   (*) Opcodes OP_MMBIN and variants follow each arithmetic and
0325   bitwise opcode. If the operation succeeds, it skips this next
0326   opcode. Otherwise, this opcode calls the corresponding metamethod.
0327 
0328   (*) Opcode OP_TESTSET is used in short-circuit expressions that need
0329   both to jump and to produce a value, such as (a = b or c).
0330 
0331   (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
0332   'top' is set to last_result+1, so next open instruction (OP_CALL,
0333   OP_RETURN*, OP_SETLIST) may use 'top'.
0334 
0335   (*) In OP_VARARG, if (C == 0) then use actual number of varargs and
0336   set top (like in OP_CALL with C == 0).
0337 
0338   (*) In OP_RETURN, if (B == 0) then return up to 'top'.
0339 
0340   (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
0341   OP_EXTRAARG.
0342 
0343   (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
0344   real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
0345   bits of C).
0346 
0347   (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
0348   power of 2) plus 1, or zero for size zero. If not k, the array size
0349   is C. Otherwise, the array size is EXTRAARG _ C.
0350 
0351   (*) For comparisons, k specifies what condition the test should accept
0352   (true or false).
0353 
0354   (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
0355    (the constant is the first operand).
0356 
0357   (*) All 'skips' (pc++) assume that next instruction is a jump.
0358 
0359   (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
0360   function builds upvalues, which may need to be closed. C > 0 means
0361   the function is vararg, so that its 'func' must be corrected before
0362   returning; in this case, (C - 1) is its number of fixed parameters.
0363 
0364   (*) In comparisons with an immediate operand, C signals whether the
0365   original operand was a float. (It must be corrected in case of
0366   metamethods.)
0367 
0368 ===========================================================================*/
0369 
0370 
0371 /*
0372 ** masks for instruction properties. The format is:
0373 ** bits 0-2: op mode
0374 ** bit 3: instruction set register A
0375 ** bit 4: operator is a test (next instruction must be a jump)
0376 ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
0377 ** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
0378 ** bit 7: instruction is an MM instruction (call a metamethod)
0379 */
0380 
0381 LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
0382 
0383 #define getOpMode(m)    (cast(enum OpMode, luaP_opmodes[m] & 7))
0384 #define testAMode(m)    (luaP_opmodes[m] & (1 << 3))
0385 #define testTMode(m)    (luaP_opmodes[m] & (1 << 4))
0386 #define testITMode(m)   (luaP_opmodes[m] & (1 << 5))
0387 #define testOTMode(m)   (luaP_opmodes[m] & (1 << 6))
0388 #define testMMMode(m)   (luaP_opmodes[m] & (1 << 7))
0389 
0390 /* "out top" (set top for next instruction) */
0391 #define isOT(i)  \
0392     ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \
0393           GET_OPCODE(i) == OP_TAILCALL)
0394 
0395 /* "in top" (uses top from previous instruction) */
0396 #define isIT(i)     (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
0397 
0398 #define opmode(mm,ot,it,t,a,m)  \
0399     (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
0400 
0401 
0402 /* number of list items to accumulate before a SETLIST instruction */
0403 #define LFIELDS_PER_FLUSH   50
0404 
0405 #endif