2
0

lopcodes.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ** $Id: lopcodes.h,v 1.66 2000/08/15 18:28:48 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. Moreover,
  12. an instruction can have 0, 1, or 2 arguments. Instructions can
  13. have the following types:
  14. type 0: no arguments
  15. type 1: 1 unsigned argument in the higher bits (called `U')
  16. type 2: 1 signed argument in the higher bits (`S')
  17. type 3: 1st unsigned argument in the higher bits (`A')
  18. 2nd unsigned argument in the middle bits (`B')
  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. The size of each argument is defined in `llimits.h'. The usual is an
  25. instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
  26. with 9 bits, and A arguments with 17 bits (32-6-9). For small
  27. installations, the instruction size can be 16, so U has 10 bits,
  28. and A and B have 5 bits each.
  29. ===========================================================================*/
  30. /* creates a mask with `n' 1 bits at position `p' */
  31. #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
  32. /* creates a mask with `n' 0 bits at position `p' */
  33. #define MASK0(n,p) (~MASK1(n,p))
  34. /*
  35. ** the following macros help to manipulate instructions
  36. */
  37. #define CREATE_0(o) ((Instruction)(o))
  38. #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
  39. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
  40. #define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
  41. #define GETARG_U(i) ((int)((i)>>POS_U))
  42. #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
  43. ((Instruction)(u)<<POS_U)))
  44. #define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
  45. #define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
  46. #define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
  47. #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
  48. | ((Instruction)(b)<<POS_B))
  49. #define GETARG_A(i) ((int)((i)>>POS_A))
  50. #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
  51. ((Instruction)(a)<<POS_A)))
  52. #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
  53. #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
  54. ((Instruction)(b)<<POS_B)))
  55. /*
  56. ** K = U argument used as index to `kstr'
  57. ** J = S argument used as jump offset (relative to pc of next instruction)
  58. ** L = unsigned argument used as index of local variable
  59. ** N = U argument used as index to `knum'
  60. */
  61. typedef enum {
  62. /*----------------------------------------------------------------------
  63. name args stack before stack after side effects
  64. ------------------------------------------------------------------------*/
  65. OP_END,/* - - (return) no results */
  66. OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
  67. OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
  68. OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
  69. OP_PUSHNIL,/* U - nil_1-nil_u */
  70. OP_POP,/* U a_u-a_1 - */
  71. OP_PUSHINT,/* S - (Number)s */
  72. OP_PUSHSTRING,/* K - KSTR[k] */
  73. OP_PUSHNUM,/* N - KNUM[n] */
  74. OP_PUSHNEGNUM,/* N - -KNUM[n] */
  75. OP_PUSHUPVALUE,/* U - Closure[u] */
  76. OP_GETLOCAL,/* L - LOC[l] */
  77. OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
  78. OP_GETTABLE,/* - i t t[i] */
  79. OP_GETDOTTED,/* K t t[KSTR[k]] */
  80. OP_GETINDEXED,/* L t t[LOC[l]] */
  81. OP_PUSHSELF,/* K t t t[KSTR[k]] */
  82. OP_CREATETABLE,/* U - newarray(size = u) */
  83. OP_SETLOCAL,/* L x - LOC[l]=x */
  84. OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
  85. OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
  86. OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
  87. OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
  88. OP_ADD,/* - y x x+y */
  89. OP_ADDI,/* S x x+s */
  90. OP_SUB,/* - y x x-y */
  91. OP_MULT,/* - y x x*y */
  92. OP_DIV,/* - y x x/y */
  93. OP_POW,/* - y x x^y */
  94. OP_CONCAT,/* U v_u-v_1 v1..-..v_u */
  95. OP_MINUS,/* - x -x */
  96. OP_NOT,/* - x (x==nil)? 1 : nil */
  97. OP_JMPNE,/* J y x - (x~=y)? PC+=s */
  98. OP_JMPEQ,/* J y x - (x==y)? PC+=s */
  99. OP_JMPLT,/* J y x - (x<y)? PC+=s */
  100. OP_JMPLE,/* J y x - (x<y)? PC+=s */
  101. OP_JMPGT,/* J y x - (x>y)? PC+=s */
  102. OP_JMPGE,/* J y x - (x>=y)? PC+=s */
  103. OP_JMPT,/* J x - (x~=nil)? PC+=s */
  104. OP_JMPF,/* J x - (x==nil)? PC+=s */
  105. OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
  106. OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
  107. OP_JMP,/* J - - PC+=s */
  108. OP_PUSHNILJMP,/* - - nil PC++; */
  109. OP_FORPREP,/* J */
  110. OP_FORLOOP,/* J */
  111. OP_LFORPREP,/* J */
  112. OP_LFORLOOP,/* J */
  113. OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
  114. } OpCode;
  115. #define NUM_OPCODES ((int)OP_CLOSURE+1)
  116. #define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
  117. /* special code to fit a LUA_MULTRET inside an argB */
  118. #define MULT_RET 255 /* (<=MAXARG_B) */
  119. #if MULT_RET>MAXARG_B
  120. #undef MULT_RET
  121. #define MULT_RET MAXARG_B
  122. #endif
  123. #endif