lopcodes.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** $Id: lopcodes.h,v 1.99 2002/06/12 14:51:31 roberto Exp $
  3. ** Opcodes for Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lopcodes_h
  7. #define lopcodes_h
  8. #include "llimits.h"
  9. /*===========================================================================
  10. We assume that instructions are unsigned numbers.
  11. All instructions have an opcode in the first 6 bits.
  12. Instructions can have the following fields:
  13. `A' : 8 bits (25-32)
  14. `B' : 8 bits (17-24)
  15. `C' : 10 bits (7-16)
  16. `Bx' : 18 bits (`B' and `C' together)
  17. `sBx' : signed Bx
  18. A signed argument is represented in excess K; that is, the number
  19. value is the unsigned value minus K. K is exactly the maximum value
  20. for that argument (so that -max is represented by 0, and +max is
  21. represented by 2*max), which is half the maximum for the corresponding
  22. unsigned argument.
  23. ===========================================================================*/
  24. enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
  25. /*
  26. ** size and position of opcode arguments.
  27. */
  28. #define SIZE_C 10
  29. #define SIZE_B 8
  30. #define SIZE_Bx (SIZE_C + SIZE_B)
  31. #define SIZE_A 8
  32. #define SIZE_OP 6
  33. #define POS_C SIZE_OP
  34. #define POS_B (POS_C + SIZE_C)
  35. #define POS_Bx POS_C
  36. #define POS_A (POS_B + SIZE_B)
  37. /*
  38. ** limits for opcode arguments.
  39. ** we use (signed) int to manipulate most arguments,
  40. ** so they must fit in BITS_INT-1 bits (-1 for sign)
  41. */
  42. #if SIZE_Bx < BITS_INT-1
  43. #define MAXARG_Bx ((1<<SIZE_Bx)-1)
  44. #define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
  45. #else
  46. #define MAXARG_Bx MAX_INT
  47. #define MAXARG_sBx MAX_INT
  48. #endif
  49. #define MAXARG_A ((1<<SIZE_A)-1)
  50. #define MAXARG_B ((1<<SIZE_B)-1)
  51. #define MAXARG_C ((1<<SIZE_C)-1)
  52. /* creates a mask with `n' 1 bits at position `p' */
  53. #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
  54. /* creates a mask with `n' 0 bits at position `p' */
  55. #define MASK0(n,p) (~MASK1(n,p))
  56. /*
  57. ** the following macros help to manipulate instructions
  58. */
  59. #define GET_OPCODE(i) (cast(OpCode, (i)&MASK1(SIZE_OP,0)))
  60. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | cast(Instruction, o)))
  61. #define GETARG_A(i) (cast(int, (i)>>POS_A))
  62. #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
  63. ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
  64. #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
  65. #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
  66. ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
  67. #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
  68. #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
  69. ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
  70. #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
  71. #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
  72. ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
  73. #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
  74. #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
  75. #define CREATE_ABC(o,a,b,c) (cast(Instruction, o) \
  76. | (cast(Instruction, a)<<POS_A) \
  77. | (cast(Instruction, b)<<POS_B) \
  78. | (cast(Instruction, c)<<POS_C))
  79. #define CREATE_ABx(o,a,bc) (cast(Instruction, o) \
  80. | (cast(Instruction, a)<<POS_A) \
  81. | (cast(Instruction, bc)<<POS_Bx))
  82. /*
  83. ** invalid registers that fits in 8 bits
  84. */
  85. #define NO_REG MAXARG_A
  86. #define NO_REG1 (NO_REG+1)
  87. /*
  88. ** R(x) - register
  89. ** Kst(x) - constant (in constant table)
  90. ** R/K(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
  91. */
  92. typedef enum {
  93. /*----------------------------------------------------------------------
  94. name args description
  95. ------------------------------------------------------------------------*/
  96. OP_MOVE,/* A B R(A) := R(B) */
  97. OP_LOADK,/* A Bx R(A) := Kst(Bx) */
  98. OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) PC++ */
  99. OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
  100. OP_GETUPVAL,/* A B R(A) := UpValue[B] */
  101. OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
  102. OP_GETTABLE,/* A B C R(A) := R(B)[R/K(C)] */
  103. OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */
  104. OP_SETUPVAL,/* A B UpValue[B] := R(A) */
  105. OP_SETTABLE,/* A B C R(B)[R/K(C)] := R(A) */
  106. OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
  107. OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[R/K(C)] */
  108. OP_ADD,/* A B C R(A) := R(B) + R/K(C) */
  109. OP_SUB,/* A B C R(A) := R(B) - R/K(C) */
  110. OP_MUL,/* A B C R(A) := R(B) * R/K(C) */
  111. OP_DIV,/* A B C R(A) := R(B) / R/K(C) */
  112. OP_POW,/* A B C R(A) := R(B) ^ R/K(C) */
  113. OP_UNM,/* A B R(A) := -R(B) */
  114. OP_NOT,/* A B R(A) := not R(B) */
  115. OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
  116. OP_JMP,/* sBx PC += sBx */
  117. OP_EQ,/* A B C if ((R(A) == R/K(C)) ~= B) then pc++ */
  118. OP_LT,/* A B C if ((R(A) < R/K(C)) ~= B) then pc++ */
  119. OP_LE,/* A B C if ((R(A) <= R/K(C)) ~= B) then pc++ */
  120. OP_GT,/* A B C if ((R(A) > R/K(C)) ~= B) then pc++ */
  121. OP_GE,/* A B C if ((R(A) >= R/K(C)) ~= B) then pc++ */
  122. OP_TEST,/* A B C if (R(C) <=> B) then R(A) := R(C) else pc++ */
  123. OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
  124. OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
  125. OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
  126. OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx */
  127. OP_TFORLOOP,/* A C R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
  128. if R(A+2) ~= nil then pc++ */
  129. OP_TFORPREP,/* A sBx if type(R(A)) == table then R(A+1):=R(A), R(A):=next;
  130. PC += sBx */
  131. OP_SETLIST,/* A Bx R(A)[Bx-Bx%FPF+i] := R(A+i), 1 <= i <= Bx%FPF+1 */
  132. OP_SETLISTO,/* A Bx */
  133. OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
  134. OP_CLOSURE/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
  135. } OpCode;
  136. #define NUM_OPCODES (cast(int, OP_CLOSURE+1))
  137. /*===========================================================================
  138. Notes:
  139. (1) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
  140. and can be 0: OP_CALL then sets `top' to last_result+1, so
  141. next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
  142. (2) In OP_RETURN, if (B == 0) then return up to `top'
  143. (3) For comparisons, B specifies what conditions the test should accept.
  144. (4) All `skips' (pc++) assume that next instruction is a jump
  145. ===========================================================================*/
  146. /*
  147. ** masks for instruction properties
  148. */
  149. enum OpModeMask {
  150. OpModeBreg = 2, /* B is a register */
  151. OpModeCreg, /* C is a register/constant */
  152. OpModesetA, /* instruction set register A */
  153. OpModeK, /* Bx is a constant */
  154. OpModeT /* operator is a test */
  155. };
  156. extern const lu_byte luaP_opmodes[NUM_OPCODES];
  157. #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
  158. #define testOpMode(m, b) (luaP_opmodes[m] & (1 << (b)))
  159. #ifdef LUA_OPNAMES
  160. extern const char *const luaP_opnames[]; /* opcode names */
  161. #endif
  162. /* number of list items to accumulate before a SETLIST instruction */
  163. /* (must be a power of 2) */
  164. #define LFIELDS_PER_FLUSH 32
  165. #endif