lopcodes.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. ** $Id: lopcodes.h,v 1.73 2001/06/05 18:17:01 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. /*
  25. ** size and position of opcode arguments.
  26. */
  27. #define SIZE_C 10
  28. #define SIZE_B 8
  29. #define SIZE_Bc (SIZE_C + SIZE_B)
  30. #define SIZE_A 8
  31. #define SIZE_OP 6
  32. #define POS_C SIZE_OP
  33. #define POS_B (POS_C + SIZE_C)
  34. #define POS_Bc POS_C
  35. #define POS_A (POS_B + SIZE_B)
  36. /*
  37. ** limits for opcode arguments.
  38. ** we use (signed) int to manipulate most arguments,
  39. ** so they must fit in BITS_INT-1 bits (-1 for sign)
  40. */
  41. #if SIZE_Bc < BITS_INT-1
  42. #define MAXARG_Bc ((1<<SIZE_Bc)-1)
  43. #define MAXARG_sBc (MAXARG_Bc>>1) /* `sBc' is signed */
  44. #else
  45. #define MAXARG_Bc MAX_INT
  46. #define MAXARG_sBc MAX_INT
  47. #endif
  48. #define MAXARG_A ((1<<SIZE_A)-1)
  49. #define MAXARG_B ((1<<SIZE_B)-1)
  50. #define MAXARG_C ((1<<SIZE_C)-1)
  51. /* creates a mask with `n' 1 bits at position `p' */
  52. #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
  53. /* creates a mask with `n' 0 bits at position `p' */
  54. #define MASK0(n,p) (~MASK1(n,p))
  55. /*
  56. ** the following macros help to manipulate instructions
  57. */
  58. #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
  59. #define SET_OPCODE(i,o) (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))
  60. #define GETARG_A(i) ((int)((i)>>POS_A))
  61. #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
  62. ((Instruction)(u)<<POS_A)))
  63. #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
  64. #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
  65. ((Instruction)(b)<<POS_B)))
  66. #define GETARG_C(i) ((int)(((i)>>POS_C) & MASK1(SIZE_C,0)))
  67. #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
  68. ((Instruction)(b)<<POS_C)))
  69. #define GETARG_Bc(i) ((int)(((i)>>POS_Bc) & MASK1(SIZE_Bc,0)))
  70. #define SETARG_Bc(i,b) ((i) = (((i)&MASK0(SIZE_Bc,POS_Bc)) | \
  71. ((Instruction)(b)<<POS_Bc)))
  72. #define GETARG_sBc(i) (GETARG_Bc(i)-MAXARG_sBc)
  73. #define SETARG_sBc(i,b) SETARG_Bc((i),(b)+MAXARG_sBc)
  74. #define CREATE_ABC(o,a,b,c) ((Instruction)(o) \
  75. | ((Instruction)(a)<<POS_A) \
  76. | ((Instruction)(b)<<POS_B) \
  77. | ((Instruction)(c)<<POS_C))
  78. #define CREATE_ABc(o,a,bc) ((Instruction)(o) \
  79. | ((Instruction)(a)<<POS_A) \
  80. | ((Instruction)(bc)<<POS_Bc))
  81. /*
  82. ** an invalid register that fits in 8 bits
  83. */
  84. #define NO_REG MAXARG_A
  85. /*
  86. ** R(x) - register
  87. ** Kst(x) - constant (in constant table)
  88. ** R/K(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
  89. */
  90. typedef enum {
  91. /*----------------------------------------------------------------------
  92. name args description
  93. ------------------------------------------------------------------------*/
  94. OP_MOVE,/* A B R(A) := R(B) */
  95. OP_LOADK,/* A Bc R(A) := Kst(Bc) */
  96. OP_LOADINT,/* A sBc R(A) := (Number)sBc */
  97. OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
  98. OP_LOADUPVAL,/* A Bc R(A) := UpValue[Bc] */
  99. OP_GETGLOBAL,/* A Bc R(A) := Gbl[Kst(Bc)] */
  100. OP_GETTABLE,/* A B C R(A) := R(B)[R/K(C)] */
  101. OP_SETGLOBAL,/* A Bc Gbl[Kst(Bc)] := R(A) */
  102. OP_SETTABLE,/* A B C R(B)[R/K(C)] := R(A) */
  103. OP_NEWTABLE,/* A Bc R(A) := {} (size = Bc) */
  104. OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[R/K(C)] */
  105. OP_ADD,/* A B C R(A) := R(B) + R/K(C) */
  106. OP_SUB,/* A B C R(A) := R(B) - R/K(C) */
  107. OP_MUL,/* A B C R(A) := R(B) * R/K(C) */
  108. OP_DIV,/* A B C R(A) := R(B) / R/K(C) */
  109. OP_POW,/* A B C R(A) := R(B) ^ R/K(C) */
  110. OP_UNM,/* A B R(A) := -R(B) */
  111. OP_NOT,/* A B R(A) := not R(B) */
  112. OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
  113. OP_JMP,/* sBc PC += sBc */
  114. OP_CJMP,/* sBc if test then PC += sBc (see (1)) */
  115. OP_TESTEQ,/* B C test := (R(B) == R/K(C)) */
  116. OP_TESTNE,/* B C test := (R(B) ~= R/K(C)) */
  117. OP_TESTLT,/* B C test := (R(B) < R/K(C)) */
  118. OP_TESTLE,/* B C test := (R(B) <= R/K(C)) */
  119. OP_TESTGT,/* B C test := (R(B) > R/K(C)) */
  120. OP_TESTGE,/* B C test := (R(B) >= R/K(C)) */
  121. OP_TESTT,/* A B test := R(B); if (test) R(A) := R(B) */
  122. OP_TESTF,/* A B test := not R(B); if (test) R(A) := nil */
  123. OP_NILJMP,/* A R(A) := nil; PC++; */
  124. OP_CALL,/* A B C R(A), ... ,R(C-1) := R(A)(R(A+1), ... ,R(B-1)) */
  125. OP_RETURN,/* A B return R(A), ... ,R(B-1) (see (3)) */
  126. OP_FORPREP,/* A sBc */
  127. OP_FORLOOP,/* A sBc */
  128. OP_TFORPREP,/* A sBc */
  129. OP_TFORLOOP,/* A sBc */
  130. OP_SETLIST,/* A Bc R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1 */
  131. OP_SETLISTO,/* A Bc */
  132. OP_CLOSURE /* A Bc R(A) := closure(KPROTO[Bc], R(A), ... ,R(A+n)) */
  133. } OpCode;
  134. #define NUM_OPCODES ((int)OP_CLOSURE+1)
  135. /*===========================================================================
  136. Notes:
  137. (1) In the current implementation there is no `test' variable;
  138. instructions OP_TEST* and OP_CJMP must always occur together.
  139. (2) In OP_CALL, if (B == NO_REG) then B = top. C is the number of returns,
  140. and can be NO_REG. OP_CALL always set "top" to last_result+1, so
  141. next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use "top".
  142. (3) In OP_RETURN, if (B == NO_REG) then B = top.
  143. ===========================================================================*/
  144. #endif