lopcodes.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ** $Id: lopcodes.h,v 1.50 2000/03/16 18:03:09 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 "llims.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. There are 4 types of
  13. Instructions:
  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. The signed argument is represented in excess 2^K; that is, the number
  20. value is the usigned value minus 2^K.
  21. The size of each argument is defined in `llims.h'. The usual is an
  22. instruction with 32 bits, U and S arguments with 26 bits (32-6), B
  23. argument with 9 bits, and A argument with 17 bits (32-6-9). For small
  24. instalations, the instruction size can be 16, so U and S have 10 bits,
  25. and A and B have 5 bits each.
  26. ===========================================================================*/
  27. #define EXCESS_S (1<<(SIZE_S-1)) /* == 2^K */
  28. /* creates a mask with `n' 1 bits at position `p' */
  29. #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
  30. /* creates a mask with `n' 0 bits at position `p' */
  31. #define MASK0(n,p) (~MASK1(n,p))
  32. /*
  33. ** the following macros help to manipulate instructions
  34. */
  35. #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
  36. #define GETARG_U(i) ((int)((i)>>POS_U))
  37. #define GETARG_S(i) ((int)((i)>>POS_S)-EXCESS_S)
  38. #define GETARG_A(i) ((int)((i)>>POS_A))
  39. #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
  40. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
  41. #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
  42. ((Instruction)(u)<<POS_U)))
  43. #define SETARG_S(i,s) ((i) = (((i)&MASK0(SIZE_S,POS_S)) | \
  44. ((Instruction)((s)+EXCESS_S)<<POS_S)))
  45. #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
  46. ((Instruction)(a)<<POS_A)))
  47. #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
  48. ((Instruction)(b)<<POS_B)))
  49. #define CREATE_0(o) ((Instruction)(o))
  50. #define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U)
  51. #define CREATE_S(o,s) ((Instruction)(o) | ((Instruction)(s)+EXCESS_S)<<POS_S)
  52. #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
  53. | ((Instruction)(b)<<POS_B))
  54. /*
  55. ** K = U argument used as index to `kstr'
  56. ** J = S argument used as jump offset (relative to pc of next instruction)
  57. ** L = U argument used as index of local variable
  58. ** N = U argument used as index to `knum'
  59. */
  60. typedef enum {
  61. /*----------------------------------------------------------------------
  62. name args stack before stack after side effects
  63. ------------------------------------------------------------------------*/
  64. OP_END,/* - - (return) */
  65. OP_RETURN,/* U - (return) */
  66. OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
  67. OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
  68. OP_PUSHNIL,/* U - nil_1-nil_u */
  69. OP_POP,/* U a_u-a_1 - */
  70. OP_PUSHINT,/* S - (Number)s */
  71. OP_PUSHSTRING,/* K - KSTR[k] */
  72. OP_PUSHNUM,/* N - KNUM[u] */
  73. OP_PUSHNEGNUM,/* N - -KNUM[u] */
  74. OP_PUSHUPVALUE,/* U - Closure[u] */
  75. OP_PUSHLOCAL,/* L - LOC[u] */
  76. OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
  77. OP_GETTABLE,/* - i t t[i] */
  78. OP_GETDOTTED,/* K t t[KSTR[k]] */
  79. OP_PUSHSELF,/* K t t t[KSTR[k]] */
  80. OP_CREATETABLE,/* U - newarray(size = u) */
  81. OP_SETLOCAL,/* L x - LOC[u]=x */
  82. OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
  83. OP_SETTABLEPOP,/* - v i t - t[i]=v */
  84. OP_SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */
  85. OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */
  86. OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */
  87. OP_ADD,/* - y x x+y */
  88. OP_ADDI,/* S x x+s */
  89. OP_SUB,/* - y x x-y */
  90. OP_MULT,/* - y x x*y */
  91. OP_DIV,/* - y x x/y */
  92. OP_POW,/* - y x x^y */
  93. OP_CONC,/* U v_u-v_1 v1..-..v_u */
  94. OP_MINUS,/* - x -x */
  95. OP_NOT,/* - x (x==nil)? 1 : nil */
  96. OP_IFNEQJMP,/* J y x - (x~=y)? PC+=s */
  97. OP_IFEQJMP,/* J y x - (x==y)? PC+=s */
  98. OP_IFLTJMP,/* J y x - (x<y)? PC+=s */
  99. OP_IFLEJMP,/* J y x - (x<y)? PC+=s */
  100. OP_IFGTJMP,/* J y x - (x>y)? PC+=s */
  101. OP_IFGEJMP,/* J y x - (x>=y)? PC+=s */
  102. OP_IFTJMP,/* J x - (x!=nil)? PC+=s */
  103. OP_IFFJMP,/* J x - (x==nil)? PC+=s */
  104. OP_ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */
  105. OP_ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */
  106. OP_JMP,/* J - - PC+=s */
  107. OP_PUSHNILJMP,/* - - nil PC++; */
  108. OP_CLOSURE,/* A B v_b-v_1 closure(CNST[a], v_1-v_b) */
  109. OP_SETLINE/* U - - LINE=u */
  110. } OpCode;
  111. #define ISJUMP(o) (OP_IFNEQJMP <= (o) && (o) <= OP_JMP)
  112. #endif