lopcodes.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ** $Id: lopcodes.h,v 1.165 2017/10/04 15:49:24 roberto Exp roberto $
  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 32-bit integers.
  11. All instructions have an opcode in the first 7 bits.
  12. Instructions can have the following formats:
  13. 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
  14. 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
  15. iABC |k| C(8) | | B(8) | | A(8) | | Op(7) |
  16. iABx | Bx(17) | | A(8) | | Op(7) |
  17. iAsBx | sBx (signed)(17) | | A(8) | | Op(7) |
  18. iAx | Ax(25) | | Op(7) |
  19. A signed argument is represented in excess K: the represented value is
  20. the written unsigned value minus K, where K is half the maximum for the
  21. corresponding unsigned argument.
  22. ===========================================================================*/
  23. enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
  24. /*
  25. ** size and position of opcode arguments.
  26. */
  27. #define SIZE_C 8
  28. #define SIZE_Cx (SIZE_C + 1)
  29. #define SIZE_B 8
  30. #define SIZE_Bx (SIZE_Cx + SIZE_B)
  31. #define SIZE_A 8
  32. #define SIZE_Ax (SIZE_Cx + SIZE_B + SIZE_A)
  33. #define SIZE_OP 7
  34. #define POS_OP 0
  35. #define POS_A (POS_OP + SIZE_OP)
  36. #define POS_B (POS_A + SIZE_A)
  37. #define POS_C (POS_B + SIZE_B)
  38. #define POS_k (POS_C + SIZE_C)
  39. #define POS_Bx POS_B
  40. #define POS_Ax POS_A
  41. /*
  42. ** limits for opcode arguments.
  43. ** we use (signed) int to manipulate most arguments,
  44. ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
  45. */
  46. #if SIZE_Bx < LUAI_BITSINT-1
  47. #define MAXARG_Bx ((1<<SIZE_Bx)-1)
  48. #define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
  49. #else
  50. #define MAXARG_Bx MAX_INT
  51. #define MAXARG_sBx MAX_INT
  52. #endif
  53. #if SIZE_Ax < LUAI_BITSINT-1
  54. #define MAXARG_Ax ((1<<SIZE_Ax)-1)
  55. #else
  56. #define MAXARG_Ax MAX_INT
  57. #endif
  58. #define MAXARG_A ((1<<SIZE_A)-1)
  59. #define MAXARG_B ((1<<SIZE_B)-1)
  60. #define MAXARG_C ((1<<SIZE_C)-1)
  61. #define MAXARG_sC (MAXARG_C >> 1)
  62. #define MAXARG_Cx ((1<<(SIZE_C + 1))-1)
  63. /* creates a mask with 'n' 1 bits at position 'p' */
  64. #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
  65. /* creates a mask with 'n' 0 bits at position 'p' */
  66. #define MASK0(n,p) (~MASK1(n,p))
  67. /*
  68. ** the following macros help to manipulate instructions
  69. */
  70. #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
  71. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
  72. ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
  73. #define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
  74. #define getarg(i,pos,size) (cast(int, ((i)>>(pos)) & MASK1(size,0)))
  75. #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
  76. ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
  77. #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
  78. #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
  79. #define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
  80. #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
  81. #define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
  82. #define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC)
  83. #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
  84. #define GETARG_k(i) (cast(int, ((i) & (1 << POS_k))))
  85. #define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
  86. #define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
  87. #define GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
  88. #define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
  89. #define GETARG_sBx(i) \
  90. check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - MAXARG_sBx)
  91. #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
  92. #define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \
  93. | (cast(Instruction, a)<<POS_A) \
  94. | (cast(Instruction, b)<<POS_B) \
  95. | (cast(Instruction, c)<<POS_C)) \
  96. | (cast(Instruction, k)<<POS_k)
  97. #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
  98. | (cast(Instruction, a)<<POS_A) \
  99. | (cast(Instruction, bc)<<POS_Bx))
  100. #define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
  101. | (cast(Instruction, a)<<POS_Ax))
  102. #if !defined(MAXINDEXRK) /* (for debugging only) */
  103. #define MAXINDEXRK MAXARG_B
  104. #endif
  105. /*
  106. ** invalid register that fits in 8 bits
  107. */
  108. #define NO_REG MAXARG_A
  109. /*
  110. ** R(x) - register
  111. ** K(x) - constant (in constant table)
  112. ** RK(x) == if k(i) then K(x) else R(x)
  113. */
  114. /*
  115. ** grep "ORDER OP" if you change these enums
  116. */
  117. typedef enum {
  118. /*----------------------------------------------------------------------
  119. name args description
  120. ------------------------------------------------------------------------*/
  121. OP_MOVE,/* A B R(A) := R(B) */
  122. OP_LOADI,/* A sBx R(A) := sBx */
  123. OP_LOADF,/* A sBx R(A) := (lua_Number)sBx */
  124. OP_LOADK,/* A Bx R(A) := K(Bx) */
  125. OP_LOADKX,/* A R(A) := K(extra arg) */
  126. OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
  127. OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
  128. OP_GETUPVAL,/* A B R(A) := UpValue[B] */
  129. OP_SETUPVAL,/* A B UpValue[B] := R(A) */
  130. OP_GETTABUP,/* A B C R(A) := UpValue[B][K(C):string] */
  131. OP_GETTABLE,/* A B C R(A) := R(B)[R(C)] */
  132. OP_GETI,/* A B C R(A) := R(B)[C] */
  133. OP_GETFIELD,/* A B C R(A) := R(B)[K(C):string] */
  134. OP_SETTABUP,/* A B C UpValue[A][K(B):string] := RK(C) */
  135. OP_SETTABLE,/* A B C R(A)[R(B)] := RK(C) */
  136. OP_SETI,/* A B C R(A)[B] := RK(C) */
  137. OP_SETFIELD,/* A B C R(A)[K(B):string] := RK(C) */
  138. OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
  139. OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */
  140. OP_ADDI,/* A B sC R(A) := R(B) + C */
  141. OP_SUBI,/* A B sC R(A) := R(B) - C */
  142. OP_MULI,/* A B sC R(A) := R(B) * C */
  143. OP_MODI,/* A B sC R(A) := R(B) % C */
  144. OP_POWI,/* A B sC R(A) := R(B) ^ C */
  145. OP_DIVI,/* A B sC R(A) := R(B) / C */
  146. OP_IDIVI,/* A B sC R(A) := R(B) // C */
  147. OP_ADD,/* A B C R(A) := R(B) + R(C) */
  148. OP_SUB,/* A B C R(A) := R(B) - R(C) */
  149. OP_MUL,/* A B C R(A) := R(B) * R(C) */
  150. OP_MOD,/* A B C R(A) := R(B) % R(C) */
  151. OP_POW,/* A B C R(A) := R(B) ^ R(C) */
  152. OP_DIV,/* A B C R(A) := R(B) / R(C) */
  153. OP_IDIV,/* A B C R(A) := R(B) // R(C) */
  154. OP_BAND,/* A B C R(A) := R(B) & R(C) */
  155. OP_BOR,/* A B C R(A) := R(B) | R(C) */
  156. OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */
  157. OP_SHL,/* A B C R(A) := R(B) << R(C) */
  158. OP_SHR,/* A B C R(A) := R(B) >> R(C) */
  159. OP_UNM,/* A B R(A) := -R(B) */
  160. OP_BNOT,/* A B R(A) := ~R(B) */
  161. OP_NOT,/* A B R(A) := not R(B) */
  162. OP_LEN,/* A B R(A) := length of R(B) */
  163. OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
  164. OP_CLOSE,/* A close all upvalues >= R(A) */
  165. OP_JMP,/* sBx pc+=sBx */
  166. OP_EQ,/* A B C if ((R(B) == R(C)) ~= A) then pc++ */
  167. OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */
  168. OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */
  169. OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
  170. OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
  171. OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
  172. OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
  173. OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
  174. OP_FORLOOP,/* A Bx R(A)+=R(A+2);
  175. if R(A) <?= R(A+1) then { pc-=Bx; R(A+3)=R(A) }*/
  176. OP_FORPREP,/* A Bx R(A)-=R(A+2); pc+=Bx */
  177. OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
  178. OP_TFORLOOP,/* A Bx if R(A+1) ~= nil then { R(A)=R(A+1); pc -= Bx }*/
  179. OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
  180. OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
  181. OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+B-2) = vararg(C) */
  182. OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
  183. } OpCode;
  184. #define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1)
  185. /*===========================================================================
  186. Notes:
  187. (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
  188. set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
  189. OP_SETLIST) may use 'top'.
  190. (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
  191. set top (like in OP_CALL with C == 0). C is the vararg parameter.
  192. (*) In OP_RETURN, if (B == 0) then return up to 'top'.
  193. (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
  194. 'instruction' is EXTRAARG(real C).
  195. (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
  196. (*) For comparisons, A specifies what condition the test should accept
  197. (true or false).
  198. (*) All 'skips' (pc++) assume that next instruction is a jump.
  199. ===========================================================================*/
  200. /*
  201. ** masks for instruction properties. The format is:
  202. ** bits 0-2: op mode
  203. ** bit 3: instruction set register A
  204. ** bit 4: operator is a test (next instruction must be a jump)
  205. */
  206. LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
  207. #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
  208. #define testAMode(m) (luaP_opmodes[m] & (1 << 3))
  209. #define testTMode(m) (luaP_opmodes[m] & (1 << 4))
  210. #define opmode(t,a,m) (((t)<<4) | ((a)<<3) | (m))
  211. LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
  212. /* number of list items to accumulate before a SETLIST instruction */
  213. #define LFIELDS_PER_FLUSH 50
  214. #endif