lopcodes.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. ** $Id: lopcodes.h,v 1.44 2000/03/03 18:53:17 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 real value
  19. 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. ENDCODE,/* - - (return) */
  73. RETCODE,/* U - (return) */
  74. CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
  75. TAILCALL,/* A B v_a-v_1 f (return) f(v1,...,v_a) */
  76. PUSHNIL,/* U - nil_1-nil_u */
  77. POP,/* U a_u-a_1 - */
  78. PUSHINT,/* S - (real)s */
  79. PUSHSTRING,/* K - KSTR[k] */
  80. PUSHNUM,/* N - KNUM[u] */
  81. PUSHNEGNUM,/* N - -KNUM[u] */
  82. PUSHUPVALUE,/* U - Closure[u] */
  83. PUSHLOCAL,/* L - LOC[u] */
  84. GETGLOBAL,/* K - VAR[KSTR[k]] */
  85. GETTABLE,/* - i t t[i] */
  86. GETDOTTED,/* K t t[KSTR[k]] */
  87. PUSHSELF,/* K t t t[KSTR[k]] */
  88. CREATETABLE,/* U - newarray(size = u) */
  89. SETLOCAL,/* L x - LOC[u]=x */
  90. SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
  91. SETTABLEPOP,/* - v i t - t[i]=v */
  92. SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */
  93. SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */
  94. SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */
  95. ADDOP,/* - y x x+y */
  96. ADDI,/* S x x+s */
  97. SUBOP,/* - y x x-y */
  98. MULTOP,/* - y x x*y */
  99. DIVOP,/* - y x x/y */
  100. POWOP,/* - y x x^y */
  101. CONCOP,/* U v_u-v_1 v1..-..v_u */
  102. MINUSOP,/* - x -x */
  103. NOTOP,/* - x (x==nil)? 1 : nil */
  104. IFNEQJMP,/* J y x - (x~=y)? PC+=s */
  105. IFEQJMP,/* J y x - (x==y)? PC+=s */
  106. IFLTJMP,/* J y x - (x<y)? PC+=s */
  107. IFLEJMP,/* J y x - (x<y)? PC+=s */
  108. IFGTJMP,/* J y x - (x>y)? PC+=s */
  109. IFGEJMP,/* J y x - (x>=y)? PC+=s */
  110. IFTJMP,/* J x - (x!=nil)? PC+=s */
  111. IFFJMP,/* J x - (x==nil)? PC+=s */
  112. ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */
  113. ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */
  114. JMP,/* J - - PC+=s */
  115. PUSHNILJMP,/* - - nil PC++; */
  116. CLOSURE,/* A B v_b-v_1 closure(CNST[a], v_1-v_b) */
  117. SETLINE/* U - - LINE=u */
  118. } OpCode;
  119. #define ISJUMP(o) (IFNEQJMP <= (o) && (o) <= JMP)
  120. #define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
  121. #define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */
  122. #endif