2
0

lopcodes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. ** $Id: lopcodes.h $
  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. #include "lobject.h"
  10. /*===========================================================================
  11. We assume that instructions are unsigned 32-bit integers.
  12. All instructions have an opcode in the first 7 bits.
  13. Instructions can have the following formats:
  14. 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
  15. 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
  16. iABC C(8) | B(8) |k| A(8) | Op(7) |
  17. ivABC vC(10) | vB(6) |k| A(8) | Op(7) |
  18. iABx Bx(17) | A(8) | Op(7) |
  19. iAsBx sBx (signed)(17) | A(8) | Op(7) |
  20. iAx Ax(25) | Op(7) |
  21. isJ sJ (signed)(25) | Op(7) |
  22. ('v' stands for "variant", 's' for "signed", 'x' for "extended".)
  23. A signed argument is represented in excess K: The represented value is
  24. the written unsigned value minus K, where K is half (rounded down) the
  25. maximum value for the corresponding unsigned argument.
  26. ===========================================================================*/
  27. /* basic instruction formats */
  28. enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
  29. /*
  30. ** size and position of opcode arguments.
  31. */
  32. #define SIZE_C 8
  33. #define SIZE_vC 10
  34. #define SIZE_B 8
  35. #define SIZE_vB 6
  36. #define SIZE_Bx (SIZE_C + SIZE_B + 1)
  37. #define SIZE_A 8
  38. #define SIZE_Ax (SIZE_Bx + SIZE_A)
  39. #define SIZE_sJ (SIZE_Bx + SIZE_A)
  40. #define SIZE_OP 7
  41. #define POS_OP 0
  42. #define POS_A (POS_OP + SIZE_OP)
  43. #define POS_k (POS_A + SIZE_A)
  44. #define POS_B (POS_k + 1)
  45. #define POS_vB (POS_k + 1)
  46. #define POS_C (POS_B + SIZE_B)
  47. #define POS_vC (POS_vB + SIZE_vB)
  48. #define POS_Bx POS_k
  49. #define POS_Ax POS_A
  50. #define POS_sJ POS_A
  51. /*
  52. ** limits for opcode arguments.
  53. ** we use (signed) 'int' to manipulate most arguments,
  54. ** so they must fit in ints.
  55. */
  56. /*
  57. ** Check whether type 'int' has at least 'b' + 1 bits.
  58. ** 'b' < 32; +1 for the sign bit.
  59. */
  60. #define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1)
  61. #if L_INTHASBITS(SIZE_Bx)
  62. #define MAXARG_Bx ((1<<SIZE_Bx)-1)
  63. #else
  64. #define MAXARG_Bx INT_MAX
  65. #endif
  66. #define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
  67. #if L_INTHASBITS(SIZE_Ax)
  68. #define MAXARG_Ax ((1<<SIZE_Ax)-1)
  69. #else
  70. #define MAXARG_Ax INT_MAX
  71. #endif
  72. #if L_INTHASBITS(SIZE_sJ)
  73. #define MAXARG_sJ ((1 << SIZE_sJ) - 1)
  74. #else
  75. #define MAXARG_sJ INT_MAX
  76. #endif
  77. #define OFFSET_sJ (MAXARG_sJ >> 1)
  78. #define MAXARG_A ((1<<SIZE_A)-1)
  79. #define MAXARG_B ((1<<SIZE_B)-1)
  80. #define MAXARG_vB ((1<<SIZE_vB)-1)
  81. #define MAXARG_C ((1<<SIZE_C)-1)
  82. #define MAXARG_vC ((1<<SIZE_vC)-1)
  83. #define OFFSET_sC (MAXARG_C >> 1)
  84. #define int2sC(i) ((i) + OFFSET_sC)
  85. #define sC2int(i) ((i) - OFFSET_sC)
  86. /* creates a mask with 'n' 1 bits at position 'p' */
  87. #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
  88. /* creates a mask with 'n' 0 bits at position 'p' */
  89. #define MASK0(n,p) (~MASK1(n,p))
  90. /*
  91. ** the following macros help to manipulate instructions
  92. */
  93. #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
  94. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
  95. ((cast_Inst(o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
  96. #define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
  97. #define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
  98. #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
  99. ((cast_Inst(v)<<pos)&MASK1(size,pos))))
  100. #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
  101. #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
  102. #define GETARG_B(i) \
  103. check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
  104. #define GETARG_vB(i) \
  105. check_exp(checkopm(i, ivABC), getarg(i, POS_vB, SIZE_vB))
  106. #define GETARG_sB(i) sC2int(GETARG_B(i))
  107. #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
  108. #define SETARG_vB(i,v) setarg(i, v, POS_vB, SIZE_vB)
  109. #define GETARG_C(i) \
  110. check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
  111. #define GETARG_vC(i) \
  112. check_exp(checkopm(i, ivABC), getarg(i, POS_vC, SIZE_vC))
  113. #define GETARG_sC(i) sC2int(GETARG_C(i))
  114. #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
  115. #define SETARG_vC(i,v) setarg(i, v, POS_vC, SIZE_vC)
  116. #define TESTARG_k(i) (cast_int(((i) & (1u << POS_k))))
  117. #define GETARG_k(i) getarg(i, POS_k, 1)
  118. #define SETARG_k(i,v) setarg(i, v, POS_k, 1)
  119. #define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
  120. #define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
  121. #define GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
  122. #define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
  123. #define GETARG_sBx(i) \
  124. check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
  125. #define SETARG_sBx(i,b) SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))
  126. #define GETARG_sJ(i) \
  127. check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
  128. #define SETARG_sJ(i,j) \
  129. setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
  130. #define CREATE_ABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
  131. | (cast_Inst(a)<<POS_A) \
  132. | (cast_Inst(b)<<POS_B) \
  133. | (cast_Inst(c)<<POS_C) \
  134. | (cast_Inst(k)<<POS_k))
  135. #define CREATE_vABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
  136. | (cast_Inst(a)<<POS_A) \
  137. | (cast_Inst(b)<<POS_vB) \
  138. | (cast_Inst(c)<<POS_vC) \
  139. | (cast_Inst(k)<<POS_k))
  140. #define CREATE_ABx(o,a,bc) ((cast_Inst(o)<<POS_OP) \
  141. | (cast_Inst(a)<<POS_A) \
  142. | (cast_Inst(bc)<<POS_Bx))
  143. #define CREATE_Ax(o,a) ((cast_Inst(o)<<POS_OP) \
  144. | (cast_Inst(a)<<POS_Ax))
  145. #define CREATE_sJ(o,j,k) ((cast_Inst(o) << POS_OP) \
  146. | (cast_Inst(j) << POS_sJ) \
  147. | (cast_Inst(k) << POS_k))
  148. #if !defined(MAXINDEXRK) /* (for debugging only) */
  149. #define MAXINDEXRK MAXARG_B
  150. #endif
  151. /*
  152. ** Maximum size for the stack of a Lua function. It must fit in 8 bits.
  153. ** The highest valid register is one less than this value.
  154. */
  155. #define MAX_FSTACK MAXARG_A
  156. /*
  157. ** Invalid register (one more than last valid register).
  158. */
  159. #define NO_REG MAX_FSTACK
  160. /*
  161. ** R[x] - register
  162. ** K[x] - constant (in constant table)
  163. ** RK(x) == if k(i) then K[x] else R[x]
  164. */
  165. /*
  166. ** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
  167. ** has extra descriptions in the notes after the enumeration.
  168. */
  169. typedef enum {
  170. /*----------------------------------------------------------------------
  171. name args description
  172. ------------------------------------------------------------------------*/
  173. OP_MOVE,/* A B R[A] := R[B] */
  174. OP_LOADI,/* A sBx R[A] := sBx */
  175. OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
  176. OP_LOADK,/* A Bx R[A] := K[Bx] */
  177. OP_LOADKX,/* A R[A] := K[extra arg] */
  178. OP_LOADFALSE,/* A R[A] := false */
  179. OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */
  180. OP_LOADTRUE,/* A R[A] := true */
  181. OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
  182. OP_GETUPVAL,/* A B R[A] := UpValue[B] */
  183. OP_SETUPVAL,/* A B UpValue[B] := R[A] */
  184. OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */
  185. OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */
  186. OP_GETI,/* A B C R[A] := R[B][C] */
  187. OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */
  188. OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */
  189. OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
  190. OP_SETI,/* A B C R[A][B] := RK(C) */
  191. OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */
  192. OP_NEWTABLE,/* A vB vC k R[A] := {} */
  193. OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] */
  194. OP_ADDI,/* A B sC R[A] := R[B] + sC */
  195. OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
  196. OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
  197. OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
  198. OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
  199. OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
  200. OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
  201. OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
  202. OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
  203. OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
  204. OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
  205. OP_SHRI,/* A B sC R[A] := R[B] >> sC */
  206. OP_SHLI,/* A B sC R[A] := sC << R[B] */
  207. OP_ADD,/* A B C R[A] := R[B] + R[C] */
  208. OP_SUB,/* A B C R[A] := R[B] - R[C] */
  209. OP_MUL,/* A B C R[A] := R[B] * R[C] */
  210. OP_MOD,/* A B C R[A] := R[B] % R[C] */
  211. OP_POW,/* A B C R[A] := R[B] ^ R[C] */
  212. OP_DIV,/* A B C R[A] := R[B] / R[C] */
  213. OP_IDIV,/* A B C R[A] := R[B] // R[C] */
  214. OP_BAND,/* A B C R[A] := R[B] & R[C] */
  215. OP_BOR,/* A B C R[A] := R[B] | R[C] */
  216. OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
  217. OP_SHL,/* A B C R[A] := R[B] << R[C] */
  218. OP_SHR,/* A B C R[A] := R[B] >> R[C] */
  219. OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
  220. OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
  221. OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
  222. OP_UNM,/* A B R[A] := -R[B] */
  223. OP_BNOT,/* A B R[A] := ~R[B] */
  224. OP_NOT,/* A B R[A] := not R[B] */
  225. OP_LEN,/* A B R[A] := #R[B] (length operator) */
  226. OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
  227. OP_CLOSE,/* A close all upvalues >= R[A] */
  228. OP_TBC,/* A mark variable A "to be closed" */
  229. OP_JMP,/* sJ pc += sJ */
  230. OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */
  231. OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */
  232. OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
  233. OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */
  234. OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */
  235. OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */
  236. OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */
  237. OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
  238. OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
  239. OP_TEST,/* A k if (not R[A] == k) then pc++ */
  240. OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
  241. OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
  242. OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
  243. OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
  244. OP_RETURN0,/* return */
  245. OP_RETURN1,/* A return R[A] */
  246. OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */
  247. OP_FORPREP,/* A Bx <check values and prepare counters>;
  248. if not to run then pc+=Bx+1; */
  249. OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */
  250. OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
  251. OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
  252. OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */
  253. OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
  254. OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
  255. OP_VARARGPREP,/*A (adjust vararg parameters) */
  256. OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
  257. } OpCode;
  258. #define NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
  259. /*===========================================================================
  260. Notes:
  261. (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
  262. value, in a code equivalent to (not cond ? false : true). (It
  263. produces false and skips the next instruction producing true.)
  264. (*) Opcodes OP_MMBIN and variants follow each arithmetic and
  265. bitwise opcode. If the operation succeeds, it skips this next
  266. opcode. Otherwise, this opcode calls the corresponding metamethod.
  267. (*) Opcode OP_TESTSET is used in short-circuit expressions that need
  268. both to jump and to produce a value, such as (a = b or c).
  269. (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
  270. 'top' is set to last_result+1, so next open instruction (OP_CALL,
  271. OP_RETURN*, OP_SETLIST) may use 'top'.
  272. (*) In OP_VARARG, if (C == 0) then use actual number of varargs and
  273. set top (like in OP_CALL with C == 0).
  274. (*) In OP_RETURN, if (B == 0) then return up to 'top'.
  275. (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
  276. OP_EXTRAARG.
  277. (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
  278. real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
  279. bits of C).
  280. (*) In OP_NEWTABLE, vB is log2 of the hash size (which is always a
  281. power of 2) plus 1, or zero for size zero. If not k, the array size
  282. is vC. Otherwise, the array size is EXTRAARG _ vC.
  283. (*) For comparisons, k specifies what condition the test should accept
  284. (true or false).
  285. (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
  286. (the constant is the first operand).
  287. (*) All 'skips' (pc++) assume that next instruction is a jump.
  288. (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
  289. function builds upvalues, which may need to be closed. C > 0 means
  290. the function is vararg, so that its 'func' must be corrected before
  291. returning; in this case, (C - 1) is its number of fixed parameters.
  292. (*) In comparisons with an immediate operand, C signals whether the
  293. original operand was a float. (It must be corrected in case of
  294. metamethods.)
  295. ===========================================================================*/
  296. /*
  297. ** masks for instruction properties. The format is:
  298. ** bits 0-2: op mode
  299. ** bit 3: instruction set register A
  300. ** bit 4: operator is a test (next instruction must be a jump)
  301. ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
  302. ** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
  303. ** bit 7: instruction is an MM instruction (call a metamethod)
  304. */
  305. LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
  306. #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
  307. #define testAMode(m) (luaP_opmodes[m] & (1 << 3))
  308. #define testTMode(m) (luaP_opmodes[m] & (1 << 4))
  309. #define testITMode(m) (luaP_opmodes[m] & (1 << 5))
  310. #define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
  311. #define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
  312. LUAI_FUNC int luaP_isOT (Instruction i);
  313. LUAI_FUNC int luaP_isIT (Instruction i);
  314. #endif