Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.76 KB | None | 0 0
  1. #ifndef lopcodes_h
  2. #define lopcodes_h
  3.  
  4. #include "llimits.h"
  5.  
  6.  
  7. /*===========================================================================
  8.   We assume that instructions are unsigned numbers.
  9.   All instructions have an opcode in the first 6 bits.
  10.   Instructions can have the following fields:
  11.         `A' : 8 bits
  12.         `B' : 9 bits
  13.         `C' : 9 bits
  14.         'Ax' : 26 bits ('A', 'B', and 'C' together)
  15.         `Bx' : 18 bits (`B' and `C' together)
  16.         `sBx' : signed Bx
  17.  
  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.  
  26. enum OpMode {iABC, iABx, iAsBx, iAx};  /* basic instruction format */
  27.  
  28.  
  29. /*
  30. ** size and position of opcode arguments.
  31. */
  32. #define SIZE_C          9
  33. #define SIZE_B          9
  34. #define SIZE_Bx         (SIZE_C + SIZE_B)
  35. #define SIZE_A          8
  36. #define SIZE_Ax         (SIZE_C + SIZE_B + SIZE_A)
  37.  
  38. #define SIZE_OP         6
  39.  
  40. #define POS_OP          0
  41. #define POS_A           (POS_OP + SIZE_OP)
  42. #define POS_C           (POS_A + SIZE_A)
  43. #define POS_B           (POS_C + SIZE_C)
  44. #define POS_Bx          POS_C
  45. #define POS_Ax          POS_A
  46.  
  47.  
  48. /*
  49. ** limits for opcode arguments.
  50. ** we use (signed) int to manipulate most arguments,
  51. ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
  52. */
  53. #if SIZE_Bx < LUAI_BITSINT-1
  54. #define MAXARG_Bx        ((1<<SIZE_Bx)-1)
  55. #define MAXARG_sBx        (MAXARG_Bx>>1)         /* `sBx' is signed */
  56. #else
  57. #define MAXARG_Bx        MAX_INT
  58. #define MAXARG_sBx        MAX_INT
  59. #endif
  60.  
  61. #if SIZE_Ax < LUAI_BITSINT-1
  62. #define MAXARG_Ax       ((1<<SIZE_Ax)-1)
  63. #else
  64. #define MAXARG_Ax       MAX_INT
  65. #endif
  66.  
  67.  
  68. #define MAXARG_A        ((1<<SIZE_A)-1)
  69. #define MAXARG_B        ((1<<SIZE_B)-1)
  70. #define MAXARG_C        ((1<<SIZE_C)-1)
  71.  
  72.  
  73. /* creates a mask with `n' 1 bits at position `p' */
  74. #define MASK1(n,p)      ((~((~(Instruction)0)<<(n)))<<(p))
  75.  
  76. /* creates a mask with `n' 0 bits at position `p' */
  77. #define MASK0(n,p)      (~MASK1(n,p))
  78.  
  79. /*
  80. ** the following macros help to manipulate instructions
  81. */
  82.  
  83. #define GET_OPCODE(i)   (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
  84. #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
  85.                 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
  86.  
  87. #define getarg(i,pos,size)      (cast(int, ((i)>>pos) & MASK1(size,0)))
  88. #define setarg(i,v,pos,size)    ((i) = (((i)&MASK0(size,pos)) | \
  89.                 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
  90.  
  91. #define GETARG_A(i)     getarg(i, POS_A, SIZE_A)
  92. #define SETARG_A(i,v)   setarg(i, v, POS_A, SIZE_A)
  93.  
  94. #define GETARG_B(i)     getarg(i, POS_B, SIZE_B)
  95. #define SETARG_B(i,v)   setarg(i, v, POS_B, SIZE_B)
  96.  
  97. #define GETARG_C(i)     getarg(i, POS_C, SIZE_C)
  98. #define SETARG_C(i,v)   setarg(i, v, POS_C, SIZE_C)
  99.  
  100. #define GETARG_Bx(i)    getarg(i, POS_Bx, SIZE_Bx)
  101. #define SETARG_Bx(i,v)  setarg(i, v, POS_Bx, SIZE_Bx)
  102.  
  103. #define GETARG_Ax(i)    getarg(i, POS_Ax, SIZE_Ax)
  104. #define SETARG_Ax(i,v)  setarg(i, v, POS_Ax, SIZE_Ax)
  105.  
  106. #define GETARG_sBx(i)   (GETARG_Bx(i)-MAXARG_sBx)
  107. #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
  108.  
  109.  
  110. #define CREATE_ABC(o,a,b,c)     ((cast(Instruction, o)<<POS_OP) \
  111.                         | (cast(Instruction, a)<<POS_A) \
  112.                         | (cast(Instruction, b)<<POS_B) \
  113.                         | (cast(Instruction, c)<<POS_C))
  114.  
  115. #define CREATE_ABx(o,a,bc)      ((cast(Instruction, o)<<POS_OP) \
  116.                         | (cast(Instruction, a)<<POS_A) \
  117.                         | (cast(Instruction, bc)<<POS_Bx))
  118.  
  119. #define CREATE_Ax(o,a)          ((cast(Instruction, o)<<POS_OP) \
  120.                         | (cast(Instruction, a)<<POS_Ax))
  121.  
  122.  
  123. /*
  124. ** Macros to operate RK indices
  125. */
  126.  
  127. /* this bit 1 means constant (0 means register) */
  128. #define BITRK           (1 << (SIZE_B - 1))
  129.  
  130. /* test whether value is a constant */
  131. #define ISK(x)          ((x) & BITRK)
  132.  
  133. /* gets the index of the constant */
  134. #define INDEXK(r)       ((int)(r) & ~BITRK)
  135.  
  136. #define MAXINDEXRK      (BITRK - 1)
  137.  
  138. /* code a constant index as a RK value */
  139. #define RKASK(x)        ((x) | BITRK)
  140.  
  141.  
  142. /*
  143. ** invalid register that fits in 8 bits
  144. */
  145. #define NO_REG          MAXARG_A
  146.  
  147.  
  148. /*
  149. ** R(x) - register
  150. ** Kst(x) - constant (in constant table)
  151. ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
  152. */
  153.  
  154.  
  155. /*
  156. ** grep "ORDER OP" if you change these enums
  157. */
  158.  
  159. typedef enum {
  160. /*----------------------------------------------------------------------
  161. name            args    description
  162. ------------------------------------------------------------------------*/
  163. OP_MOVE,/*      A B     R(A) := R(B)                                    */
  164. OP_LOADK,/*     A Bx    R(A) := Kst(Bx)                                 */
  165. OP_LOADKX,/*    A       R(A) := Kst(extra arg)                          */
  166. OP_LOADBOOL,/*  A B C   R(A) := (Bool)B; if (C) pc++                    */
  167. OP_LOADNIL,/*   A B     R(A), R(A+1), ..., R(A+B) := nil                */
  168. OP_GETUPVAL,/*  A B     R(A) := UpValue[B]                              */
  169.  
  170. OP_GETTABUP,/*  A B C   R(A) := UpValue[B][RK(C)]                       */
  171. OP_GETTABLE,/*  A B C   R(A) := R(B)[RK(C)]                             */
  172.  
  173. OP_SETTABUP,/*  A B C   UpValue[A][RK(B)] := RK(C)                      */
  174. OP_SETUPVAL,/*  A B     UpValue[B] := R(A)                              */
  175. OP_SETTABLE,/*  A B C   R(A)[RK(B)] := RK(C)                            */
  176.  
  177. OP_NEWTABLE,/*  A B C   R(A) := {} (size = B,C)                         */
  178.  
  179. OP_SELF,/*      A B C   R(A+1) := R(B); R(A) := R(B)[RK(C)]             */
  180.  
  181. OP_ADD,/*       A B C   R(A) := RK(B) + RK(C)                           */
  182. OP_SUB,/*       A B C   R(A) := RK(B) - RK(C)                           */
  183. OP_MUL,/*       A B C   R(A) := RK(B) * RK(C)                           */
  184. OP_DIV,/*       A B C   R(A) := RK(B) / RK(C)                           */
  185. OP_MOD,/*       A B C   R(A) := RK(B) % RK(C)                           */
  186. OP_POW,/*       A B C   R(A) := RK(B) ^ RK(C)                           */
  187. OP_UNM,/*       A B     R(A) := -R(B)                                   */
  188. OP_NOT,/*       A B     R(A) := not R(B)                                */
  189. OP_LEN,/*       A B     R(A) := length of R(B)                          */
  190.  
  191. OP_CONCAT,/*    A B C   R(A) := R(B).. ... ..R(C)                       */
  192.  
  193. OP_JMP,/*       A sBx   pc+=sBx; if (A) close all upvalues >= R(A - 1)  */
  194. OP_EQ,/*        A B C   if ((RK(B) == RK(C)) ~= A) then pc++            */
  195. OP_LT,/*        A B C   if ((RK(B) <  RK(C)) ~= A) then pc++            */
  196. OP_LE,/*        A B C   if ((RK(B) <= RK(C)) ~= A) then pc++            */
  197.  
  198. OP_TEST,/*      A C     if not (R(A) <=> C) then pc++                   */
  199. OP_TESTSET,/*   A B C   if (R(B) <=> C) then R(A) := R(B) else pc++     */
  200.  
  201. OP_CALL,/*      A B C   R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
  202. OP_TAILCALL,/*  A B C   return R(A)(R(A+1), ... ,R(A+B-1))              */
  203. OP_RETURN,/*    A B     return R(A), ... ,R(A+B-2)      (see note)      */
  204.  
  205. OP_FORLOOP,/*   A sBx   R(A)+=R(A+2);
  206.                         if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
  207. OP_FORPREP,/*   A sBx   R(A)-=R(A+2); pc+=sBx                           */
  208.  
  209. OP_TFORCALL,/*  A C     R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));  */
  210. OP_TFORLOOP,/*  A sBx   if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
  211.  
  212. OP_SETLIST,/*   A B C   R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B        */
  213.  
  214. OP_CLOSURE,/*   A Bx    R(A) := closure(KPROTO[Bx])                     */
  215.  
  216. OP_VARARG,/*    A B     R(A), R(A+1), ..., R(A+B-2) = vararg            */
  217.  
  218. OP_EXTRAARG/*   Ax      extra (larger) argument for previous opcode     */
  219. } OpCode;
  220.  
  221.  
  222. #define NUM_OPCODES     (cast(int, OP_EXTRAARG) + 1)
  223.  
  224.  
  225.  
  226. /*===========================================================================
  227.   Notes:
  228.   (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then `top' is
  229.   set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
  230.   OP_SETLIST) may use `top'.
  231.  
  232.   (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
  233.   set top (like in OP_CALL with C == 0).
  234.  
  235.   (*) In OP_RETURN, if (B == 0) then return up to `top'.
  236.  
  237.   (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
  238.   'instruction' is EXTRAARG(real C).
  239.  
  240.   (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
  241.  
  242.   (*) For comparisons, A specifies what condition the test should accept
  243.   (true or false).
  244.  
  245.   (*) All `skips' (pc++) assume that next instruction is a jump.
  246.  
  247. ===========================================================================*/
  248.  
  249.  
  250. /*
  251. ** masks for instruction properties. The format is:
  252. ** bits 0-1: op mode
  253. ** bits 2-3: C arg mode
  254. ** bits 4-5: B arg mode
  255. ** bit 6: instruction set register A
  256. ** bit 7: operator is a test (next instruction must be a jump)
  257. */
  258.  
  259. enum OpArgMask {
  260.   OpArgN,  /* argument is not used */
  261.   OpArgU,  /* argument is used */
  262.   OpArgR,  /* argument is a register or a jump offset */
  263.   OpArgK   /* argument is a constant or register/constant */
  264. };
  265.  
  266. LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
  267.  
  268. #define getOpMode(m)    (cast(enum OpMode, luaP_opmodes[m] & 3))
  269. #define getBMode(m)     (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
  270. #define getCMode(m)     (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
  271. #define testAMode(m)    (luaP_opmodes[m] & (1 << 6))
  272. #define testTMode(m)    (luaP_opmodes[m] & (1 << 7))
  273.  
  274.  
  275. LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */
  276.  
  277.  
  278. /* number of list items to accumulate before a SETLIST instruction */
  279. #define LFIELDS_PER_FLUSH       50
  280.  
  281.  
  282. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement