lopcodes.h 13 KB

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