lopcodes.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ** $Id: lopcodes.h,v 1.77 2001/07/03 17:01:34 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 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. `Bc' : 18 bits (`B' and `C' together)
  17. `sBc' : signed Bc
  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, iABc, iAsBc}; /* 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_Bc (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_Bc 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_Bc < BITS_INT-1
  43. #define MAXARG_Bc ((1<<SIZE_Bc)-1)
  44. #define MAXARG_sBc (MAXARG_Bc>>1) /* `sBc' is signed */
  45. #else
  46. #define MAXARG_Bc MAX_INT
  47. #define MAXARG_sBc 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) ((OpCode)((i)&MASK1(SIZE_OP,0)))
  60. #define SET_OPCODE(i,o) (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))
  61. #define GETARG_A(i) ((int)((i)>>POS_A))
  62. #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
  63. ((Instruction)(u)<<POS_A)))
  64. #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
  65. #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
  66. ((Instruction)(b)<<POS_B)))
  67. #define GETARG_C(i) ((int)(((i)>>POS_C) & MASK1(SIZE_C,0)))
  68. #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
  69. ((Instruction)(b)<<POS_C)))
  70. #define GETARG_Bc(i) ((int)(((i)>>POS_Bc) & MASK1(SIZE_Bc,0)))
  71. #define SETARG_Bc(i,b) ((i) = (((i)&MASK0(SIZE_Bc,POS_Bc)) | \
  72. ((Instruction)(b)<<POS_Bc)))
  73. #define GETARG_sBc(i) (GETARG_Bc(i)-MAXARG_sBc)
  74. #define SETARG_sBc(i,b) SETARG_Bc((i),(unsigned int)((b)+MAXARG_sBc))
  75. #define CREATE_ABC(o,a,b,c) ((Instruction)(o) \
  76. | ((Instruction)(a)<<POS_A) \
  77. | ((Instruction)(b)<<POS_B) \
  78. | ((Instruction)(c)<<POS_C))
  79. #define CREATE_ABc(o,a,bc) ((Instruction)(o) \
  80. | ((Instruction)(a)<<POS_A) \
  81. | ((Instruction)(bc)<<POS_Bc))
  82. /*
  83. ** an invalid register that fits in 8 bits
  84. */
  85. #define NO_REG MAXARG_A
  86. /*
  87. ** R(x) - register
  88. ** Kst(x) - constant (in constant table)
  89. ** R/K(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
  90. */
  91. typedef enum {
  92. /*----------------------------------------------------------------------
  93. name args description
  94. ------------------------------------------------------------------------*/
  95. OP_MOVE,/* A B R(A) := R(B) */
  96. OP_LOADK,/* A Bc R(A) := Kst(Bc) */
  97. OP_LOADINT,/* A sBc R(A) := (Number)sBc */
  98. OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
  99. OP_LOADUPVAL,/* A Bc R(A) := UpValue[Bc] */
  100. OP_GETGLOBAL,/* A Bc R(A) := Gbl[Kst(Bc)] */
  101. OP_GETTABLE,/* A B C R(A) := R(B)[R/K(C)] */
  102. OP_SETGLOBAL,/* A Bc Gbl[Kst(Bc)] := R(A) */
  103. OP_SETTABLE,/* A B C R(B)[R/K(C)] := R(A) */
  104. OP_NEWTABLE,/* A Bc R(A) := {} (size = Bc) */
  105. OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[R/K(C)] */
  106. OP_ADD,/* A B C R(A) := R(B) + R/K(C) */
  107. OP_SUB,/* A B C R(A) := R(B) - R/K(C) */
  108. OP_MUL,/* A B C R(A) := R(B) * R/K(C) */
  109. OP_DIV,/* A B C R(A) := R(B) / R/K(C) */
  110. OP_POW,/* A B C R(A) := R(B) ^ R/K(C) */
  111. OP_UNM,/* A B R(A) := -R(B) */
  112. OP_NOT,/* A B R(A) := not R(B) */
  113. OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
  114. OP_JMP,/* sBc PC += sBc */
  115. OP_CJMP,/* sBc if test then PC += sBc (see (1)) */
  116. OP_TESTEQ,/* A C test := (R(A) == R/K(C)) */
  117. OP_TESTNE,/* A C test := (R(A) ~= R/K(C)) */
  118. OP_TESTLT,/* A C test := (R(A) < R/K(C)) */
  119. OP_TESTLE,/* A C test := (R(A) <= R/K(C)) */
  120. OP_TESTGT,/* A C test := (R(A) > R/K(C)) */
  121. OP_TESTGE,/* A C test := (R(A) >= R/K(C)) */
  122. OP_TESTT,/* A B test := R(B); if (test) R(A) := R(B) */
  123. OP_TESTF,/* A B test := not R(B); if (test) R(A) := nil */
  124. OP_NILJMP,/* A Bc R(A) := nil; PC++; */
  125. OP_CALL,/* A B C R(A), ... ,R(A+C-1) := R(A)(R(A+1), ... ,R(A+B))*/
  126. OP_RETURN,/* A B return R(A), ... ,R(A+B-1) (see (3)) */
  127. OP_FORPREP,/* A sBc */
  128. OP_FORLOOP,/* A sBc */
  129. OP_TFORPREP,/* A sBc */
  130. OP_TFORLOOP,/* A sBc */
  131. OP_SETLIST,/* A Bc R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1 */
  132. OP_SETLISTO,/* A Bc */
  133. OP_CLOSURE /* A Bc R(A) := closure(KPROTO[Bc], R(A), ... ,R(A+n)) */
  134. } OpCode;
  135. #define NUM_OPCODES ((int)OP_CLOSURE+1)
  136. /*===========================================================================
  137. Notes:
  138. (1) In the current implementation there is no `test' variable;
  139. instructions OP_TEST* and OP_CJMP must always occur together.
  140. (2) In OP_CALL, if (B == NO_REG) then B = top. C is the number of returns,
  141. and can be NO_REG. OP_CALL can set `top' to last_result+1, so
  142. next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
  143. (3) In OP_RETURN, if (B == NO_REG) then return up to `top'
  144. ===========================================================================*/
  145. /*
  146. ** masks for instruction properties
  147. */
  148. enum OpModeMask {
  149. OpModeBreg = 2, /* B is a register */
  150. OpModeCreg, /* C is a register/constant */
  151. OpModesetA, /* instruction set register A */
  152. OpModeK, /* Bc is a constant */
  153. OpModeT /* operator is a test */
  154. };
  155. extern const lu_byte luaP_opmodes[];
  156. #define getOpMode(m) ((enum OpMode)(luaP_opmodes[m] & 3))
  157. #define testOpMode(m, b) (luaP_opmodes[m] & (1 << (b)))
  158. /*
  159. ** opcode names (only included when compiled with LUA_OPNAMES)
  160. */
  161. extern const l_char *const luaP_opnames[];
  162. #endif