lopcodes.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 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
  14. 'B' : 9 bits
  15. 'C' : 9 bits
  16. 'Ax' : 26 bits ('A', 'B', and 'C' together)
  17. 'Bx' : 18 bits ('B' and 'C' together)
  18. 'sBx' : signed Bx
  19. A signed argument is represented in excess K; that is, the number
  20. value is the unsigned value minus K. K is exactly the maximum value
  21. for that argument (so that -max is represented by 0, and +max is
  22. represented by 2*max), which is half the maximum for the corresponding
  23. unsigned argument.
  24. ===========================================================================*/
  25. enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
  26. /*
  27. ** size and position of opcode arguments.
  28. */
  29. #define SIZE_C 9
  30. #define SIZE_B 9
  31. #define SIZE_Bx (SIZE_C + SIZE_B)
  32. #define SIZE_A 8
  33. #define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)
  34. #define SIZE_OP 6
  35. #define POS_OP 0
  36. #define POS_A (POS_OP + SIZE_OP)
  37. #define POS_C (POS_A + SIZE_A)
  38. #define POS_B (POS_C + SIZE_C)
  39. #define POS_Bx POS_C
  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. /* creates a mask with 'n' 1 bits at position 'p' */
  62. #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
  63. /* creates a mask with 'n' 0 bits at position 'p' */
  64. #define MASK0(n,p) (~MASK1(n,p))
  65. /*
  66. ** the following macros help to manipulate instructions
  67. */
  68. #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
  69. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
  70. ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
  71. #define getarg(i,pos,size) (cast(int, ((i)>>pos) & MASK1(size,0)))
  72. #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
  73. ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
  74. #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
  75. #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
  76. #define GETARG_B(i) getarg(i, POS_B, SIZE_B)
  77. #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
  78. #define GETARG_C(i) getarg(i, POS_C, SIZE_C)
  79. #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
  80. #define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx)
  81. #define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
  82. #define GETARG_Ax(i) getarg(i, POS_Ax, SIZE_Ax)
  83. #define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
  84. #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
  85. #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
  86. #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
  87. | (cast(Instruction, a)<<POS_A) \
  88. | (cast(Instruction, b)<<POS_B) \
  89. | (cast(Instruction, c)<<POS_C))
  90. #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
  91. | (cast(Instruction, a)<<POS_A) \
  92. | (cast(Instruction, bc)<<POS_Bx))
  93. #define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
  94. | (cast(Instruction, a)<<POS_Ax))
  95. /*
  96. ** Macros to operate RK indices
  97. */
  98. /* this bit 1 means constant (0 means register) */
  99. #define BITRK (1 << (SIZE_B - 1))
  100. /* test whether value is a constant */
  101. #define ISK(x) ((x) & BITRK)
  102. /* gets the index of the constant */
  103. #define INDEXK(r) ((int)(r) & ~BITRK)
  104. #if !defined(MAXINDEXRK) /* (for debugging only) */
  105. #define MAXINDEXRK (BITRK - 1)
  106. #endif
  107. /* code a constant index as a RK value */
  108. #define RKASK(x) ((x) | BITRK)
  109. /*
  110. ** invalid register that fits in 8 bits
  111. */
  112. #define NO_REG MAXARG_A
  113. /*
  114. ** R(x) - register
  115. ** Kst(x) - constant (in constant table)
  116. ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
  117. */
  118. /*
  119. ** grep "ORDER OP" if you change these enums
  120. */
  121. typedef enum {
  122. /*----------------------------------------------------------------------
  123. name args description
  124. ------------------------------------------------------------------------*/
  125. OP_MOVE,/* A B R(A) := R(B) */
  126. OP_LOADK,/* A Bx R(A) := Kst(Bx) */
  127. OP_LOADKX,/* A R(A) := Kst(extra arg) */
  128. OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
  129. OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
  130. OP_GETUPVAL,/* A B R(A) := UpValue[B] */
  131. OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
  132. OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
  133. OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */
  134. OP_SETUPVAL,/* A B UpValue[B] := R(A) */
  135. OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
  136. OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
  137. OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
  138. OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
  139. OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
  140. OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
  141. OP_MOD,/* A B C R(A) := RK(B) % RK(C) */
  142. OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
  143. OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
  144. OP_IDIV,/* A B C R(A) := RK(B) // RK(C) */
  145. OP_BAND,/* A B C R(A) := RK(B) & RK(C) */
  146. OP_BOR,/* A B C R(A) := RK(B) | RK(C) */
  147. OP_BXOR,/* A B C R(A) := RK(B) ~ RK(C) */
  148. OP_SHL,/* A B C R(A) := RK(B) << RK(C) */
  149. OP_SHR,/* A B C R(A) := RK(B) >> RK(C) */
  150. OP_UNM,/* A B R(A) := -R(B) */
  151. OP_BNOT,/* A B R(A) := ~R(B) */
  152. OP_NOT,/* A B R(A) := not R(B) */
  153. OP_LEN,/* A B R(A) := length of R(B) */
  154. OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
  155. OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
  156. OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
  157. OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
  158. OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
  159. OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
  160. OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
  161. OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
  162. OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
  163. OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
  164. OP_FORLOOP,/* A sBx R(A)+=R(A+2);
  165. if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
  166. OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
  167. OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
  168. OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
  169. OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
  170. OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
  171. OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
  172. OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
  173. } OpCode;
  174. #define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1)
  175. /*===========================================================================
  176. Notes:
  177. (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
  178. set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
  179. OP_SETLIST) may use 'top'.
  180. (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
  181. set top (like in OP_CALL with C == 0).
  182. (*) In OP_RETURN, if (B == 0) then return up to 'top'.
  183. (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
  184. 'instruction' is EXTRAARG(real C).
  185. (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
  186. (*) For comparisons, A specifies what condition the test should accept
  187. (true or false).
  188. (*) All 'skips' (pc++) assume that next instruction is a jump.
  189. ===========================================================================*/
  190. /*
  191. ** masks for instruction properties. The format is:
  192. ** bits 0-1: op mode
  193. ** bits 2-3: C arg mode
  194. ** bits 4-5: B arg mode
  195. ** bit 6: instruction set register A
  196. ** bit 7: operator is a test (next instruction must be a jump)
  197. */
  198. enum OpArgMask {
  199. OpArgN, /* argument is not used */
  200. OpArgU, /* argument is used */
  201. OpArgR, /* argument is a register or a jump offset */
  202. OpArgK /* argument is a constant or register/constant */
  203. };
  204. LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
  205. #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
  206. #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
  207. #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
  208. #define testAMode(m) (luaP_opmodes[m] & (1 << 6))
  209. #define testTMode(m) (luaP_opmodes[m] & (1 << 7))
  210. LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
  211. /* number of list items to accumulate before a SETLIST instruction */
  212. #define LFIELDS_PER_FLUSH 50
  213. #endif