lopcodes.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ** $Id: lopcodes.h,v 1.49 2000/03/13 20:37:16 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. /*===========================================================================
  9. We assume that instructions are unsigned numbers with 4 bytes.
  10. All instructions have an opcode in the 8 bits. Moreover,
  11. an instruction can have 0, 1, or 2 arguments. There are 4 types of
  12. Instructions:
  13. type 0: no arguments
  14. type 1: 1 unsigned argument in the higher 24 bits (called `U')
  15. type 2: 1 signed argument in the higher 24 bits (`S')
  16. type 3: 1st unsigned argument in the higher 16 bits (`A')
  17. 2nd unsigned argument in the middle 8 bits (`B')
  18. The signed argument is represented in excess 2^23; that is, the number
  19. value is the usigned value minus 2^23.
  20. ===========================================================================*/
  21. #define SIZE_INSTRUCTION 32
  22. #define SIZE_OP 8
  23. #define SIZE_U (SIZE_INSTRUCTION-SIZE_OP)
  24. #define POS_U SIZE_OP
  25. #define SIZE_S (SIZE_INSTRUCTION-SIZE_OP)
  26. #define POS_S SIZE_OP
  27. #define SIZE_B 8
  28. #define POS_B SIZE_OP
  29. #define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B))
  30. #define POS_A (SIZE_OP+SIZE_B)
  31. #define EXCESS_S (1<<(SIZE_S-1)) /* == 2^23 */
  32. /* creates a mask with `n' 1 bits at position `p' */
  33. #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
  34. /* creates a mask with `n' 0 bits at position `p' */
  35. #define MASK0(n,p) (~MASK1(n,p))
  36. /*
  37. ** the following macros help to manipulate instructions
  38. */
  39. #define MAXARG_U ((1<<SIZE_U)-1)
  40. #define MAXARG_S ((1<<(SIZE_S-1))-1) /* `S' is signed */
  41. #define MAXARG_A ((1<<SIZE_A)-1)
  42. #define MAXARG_B ((1<<SIZE_B)-1)
  43. #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
  44. #define GETARG_U(i) ((int)((i)>>POS_U))
  45. #define GETARG_S(i) ((int)((i)>>POS_S)-EXCESS_S)
  46. #define GETARG_A(i) ((int)((i)>>POS_A))
  47. #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
  48. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
  49. #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
  50. ((Instruction)(u)<<POS_U)))
  51. #define SETARG_S(i,s) ((i) = (((i)&MASK0(SIZE_S,POS_S)) | \
  52. ((Instruction)((s)+EXCESS_S)<<POS_S)))
  53. #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
  54. ((Instruction)(a)<<POS_A)))
  55. #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
  56. ((Instruction)(b)<<POS_B)))
  57. #define CREATE_0(o) ((Instruction)(o))
  58. #define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U)
  59. #define CREATE_S(o,s) ((Instruction)(o) | ((Instruction)(s)+EXCESS_S)<<POS_S)
  60. #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
  61. | ((Instruction)(b)<<POS_B))
  62. /*
  63. ** K = U argument used as index to `kstr'
  64. ** J = S argument used as jump offset (relative to pc of next instruction)
  65. ** L = U argument used as index of local variable
  66. ** N = U argument used as index to `knum'
  67. */
  68. typedef enum {
  69. /*----------------------------------------------------------------------
  70. name args stack before stack after side effects
  71. ------------------------------------------------------------------------*/
  72. OP_END,/* - - (return) */
  73. OP_RETURN,/* U - (return) */
  74. OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
  75. OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
  76. OP_PUSHNIL,/* U - nil_1-nil_u */
  77. OP_POP,/* U a_u-a_1 - */
  78. OP_PUSHINT,/* S - (Number)s */
  79. OP_PUSHSTRING,/* K - KSTR[k] */
  80. OP_PUSHNUM,/* N - KNUM[u] */
  81. OP_PUSHNEGNUM,/* N - -KNUM[u] */
  82. OP_PUSHUPVALUE,/* U - Closure[u] */
  83. OP_PUSHLOCAL,/* L - LOC[u] */
  84. OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
  85. OP_GETTABLE,/* - i t t[i] */
  86. OP_GETDOTTED,/* K t t[KSTR[k]] */
  87. OP_PUSHSELF,/* K t t t[KSTR[k]] */
  88. OP_CREATETABLE,/* U - newarray(size = u) */
  89. OP_SETLOCAL,/* L x - LOC[u]=x */
  90. OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
  91. OP_SETTABLEPOP,/* - v i t - t[i]=v */
  92. OP_SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */
  93. OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */
  94. OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */
  95. OP_ADD,/* - y x x+y */
  96. OP_ADDI,/* S x x+s */
  97. OP_SUB,/* - y x x-y */
  98. OP_MULT,/* - y x x*y */
  99. OP_DIV,/* - y x x/y */
  100. OP_POW,/* - y x x^y */
  101. OP_CONC,/* U v_u-v_1 v1..-..v_u */
  102. OP_MINUS,/* - x -x */
  103. OP_NOT,/* - x (x==nil)? 1 : nil */
  104. OP_IFNEQJMP,/* J y x - (x~=y)? PC+=s */
  105. OP_IFEQJMP,/* J y x - (x==y)? PC+=s */
  106. OP_IFLTJMP,/* J y x - (x<y)? PC+=s */
  107. OP_IFLEJMP,/* J y x - (x<y)? PC+=s */
  108. OP_IFGTJMP,/* J y x - (x>y)? PC+=s */
  109. OP_IFGEJMP,/* J y x - (x>=y)? PC+=s */
  110. OP_IFTJMP,/* J x - (x!=nil)? PC+=s */
  111. OP_IFFJMP,/* J x - (x==nil)? PC+=s */
  112. OP_ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */
  113. OP_ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */
  114. OP_JMP,/* J - - PC+=s */
  115. OP_PUSHNILJMP,/* - - nil PC++; */
  116. OP_CLOSURE,/* A B v_b-v_1 closure(CNST[a], v_1-v_b) */
  117. OP_SETLINE/* U - - LINE=u */
  118. } OpCode;
  119. #define ISJUMP(o) (OP_IFNEQJMP <= (o) && (o) <= OP_JMP)
  120. #define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
  121. #define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */
  122. /*
  123. ** we use int to manipulte most arguments, so they must fit
  124. */
  125. #if MAXARG_U > MAX_INT
  126. #undef MAXARG_U
  127. #define MAXARG_U MAX_INT
  128. #endif
  129. #if MAXARG_S > MAX_INT
  130. #undef MAXARG_S
  131. #define MAXARG_S MAX_INT
  132. #endif
  133. #if MAXARG_A > MAX_INT
  134. #undef MAXARG_A
  135. #define MAXARG_A MAX_INT
  136. #endif
  137. #if MAXARG_B > MAX_INT
  138. #undef MAXARG_B
  139. #define MAXARG_B MAX_INT
  140. #endif
  141. #endif