1 #ifndef __KTAP_BYTECODE_H__
2 #define __KTAP_BYTECODE_H__
5 /* opcode is copied from lua initially */
8 /*----------------------------------------------------------------------
9 * name args description
10 * ------------------------------------------------------------------------*/
11 OP_MOVE,/* A B R(A) := R(B) */
12 OP_LOADK,/* A Bx R(A) := Kst(Bx) */
13 OP_LOADKX,/* A R(A) := Kst(extra arg) */
14 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
15 OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
16 OP_GETUPVAL,/* A B R(A) := UpValue[B] */
18 OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
19 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
21 OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */
22 OP_SETTABUP_INCR,/* A B C UpValue[A][RK(B)] += RK(C) */
23 OP_SETTABUP_AGGR,/* A B C UpValue[A][RK(B)] <<< RK(C) */
24 OP_SETUPVAL,/* A B UpValue[B] := R(A) */
25 OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
26 OP_SETTABLE_INCR,/* A B C R(A)[RK(B)] += RK(C) */
27 OP_SETTABLE_AGGR,/* A B C R(A)[RK(B)] <<< RK(C) */
29 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
31 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
33 OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
34 OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
35 OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
36 OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
37 OP_MOD,/* A B C R(A) := RK(B) % RK(C) */
38 OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
39 OP_UNM,/* A B R(A) := -R(B) */
40 OP_NOT,/* A B R(A) := not R(B) */
41 OP_LEN,/* A B R(A) := length of R(B) */
43 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
45 OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
46 OP_EQ,/* A B C if ((RK(B) == RK(C)) != A) then pc++ */
47 OP_LT,/* A B C if ((RK(B) < RK(C)) != A) then pc++ */
48 OP_LE,/* A B C if ((RK(B) <= RK(C)) != A) then pc++ */
50 OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
51 OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
53 OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
54 OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
55 OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
57 OP_FORLOOP,/* A sBx R(A)+=R(A+2);
58 if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
59 OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
61 OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
62 OP_TFORLOOP,/* A sBx if R(A+1) != nil then { R(A)=R(A+1); pc += sBx }*/
64 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
66 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
68 OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
70 OP_EXTRAARG,/* Ax extra (larger) argument for previous opcode */
72 OP_EVENT,/* A B C R(A) := R(B)[C] */
74 OP_EVENTNAME, /* A R(A) = event_name() */
76 OP_EVENTARG,/* A B R(A) := event_arg(B)*/
78 OP_LOAD_GLOBAL,/* A B C R(A) := R(B)[C] */
85 #define NUM_OPCODES ((int)OP_LOAD_GLOBAL + 1)
88 enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
92 * ** size and position of opcode arguments.
96 #define SIZE_Bx (SIZE_C + SIZE_B)
98 #define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)
103 #define POS_A (POS_OP + SIZE_OP)
104 #define POS_C (POS_A + SIZE_A)
105 #define POS_B (POS_C + SIZE_C)
112 * ** limits for opcode arguments.
113 * ** we use (signed) int to manipulate most arguments,
114 * ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
116 #define MAXARG_Bx ((1<<SIZE_Bx)-1)
117 #define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
119 #define MAXARG_Ax ((1<<SIZE_Ax)-1)
121 #define MAXARG_A ((1<<SIZE_A)-1)
122 #define MAXARG_B ((1<<SIZE_B)-1)
123 #define MAXARG_C ((1<<SIZE_C)-1)
126 /* creates a mask with `n' 1 bits at position `p' */
127 #define MASK1(n,p) ((~((~(ktap_instruction)0)<<(n)))<<(p))
129 /* creates a mask with `n' 0 bits at position `p' */
130 #define MASK0(n,p) (~MASK1(n,p))
133 * ** the following macros help to manipulate instructions
136 #define GET_OPCODE(i) ((OpCode)((i)>>POS_OP) & MASK1(SIZE_OP,0))
137 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
138 ((((ktap_instruction)o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
140 #define getarg(i,pos,size) ((int)((i)>>pos) & MASK1(size,0))
141 #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
142 ((((ktap_instruction)v)<<pos)&MASK1(size,pos))))
144 #define GETARG_A(i) getarg(i, POS_A, SIZE_A)
145 #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
147 #define GETARG_B(i) getarg(i, POS_B, SIZE_B)
148 #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
150 #define GETARG_C(i) getarg(i, POS_C, SIZE_C)
151 #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
153 #define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx)
154 #define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
156 #define GETARG_Ax(i) getarg(i, POS_Ax, SIZE_Ax)
157 #define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
159 #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
160 #define SETARG_sBx(i,b) SETARG_Bx((i), (unsigned int)(b)+MAXARG_sBx)
162 #define CREATE_ABC(o,a,b,c) (((ktap_instruction)(o))<<POS_OP) \
163 | (((ktap_instruction)(a))<<POS_A) \
164 | (((ktap_instruction)(b))<<POS_B) \
165 | (((ktap_instruction)(c))<<POS_C)
167 #define CREATE_ABx(o,a,bc) (((ktap_instruction)(o))<<POS_OP) \
168 | (((ktap_instruction)(a))<<POS_A) \
169 | (((ktap_instruction)(bc))<<POS_Bx)
171 #define CREATE_Ax(o,a) (((ktap_instruction)(o))<<POS_OP) \
172 | (((ktap_instruction)(a))<<POS_Ax)
177 * ** Macros to operate RK indices
180 /* this bit 1 means constant (0 means register) */
181 #define BITRK (1 << (SIZE_B - 1))
183 /* test whether value is a constant */
184 #define ISK(x) ((x) & BITRK)
186 /* gets the index of the constant */
187 #define INDEXK(r) ((int)(r) & ~BITRK)
189 #define MAXINDEXRK (BITRK - 1)
191 /* code a constant index as a RK value */
192 #define RKASK(x) ((x) | BITRK)
196 * ** invalid register that fits in 8 bits
198 #define NO_REG MAXARG_A
203 * ** Kst(x) - constant (in constant table)
204 * ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
210 * ** masks for instruction properties. The format is:
211 * ** bits 0-1: op mode
212 * ** bits 2-3: C arg mode
213 * ** bits 4-5: B arg mode
214 * ** bit 6: instruction set register A
215 * ** bit 7: operator is a test (next instruction must be a jump)
219 OpArgN, /* argument is not used */
220 OpArgU, /* argument is used */
221 OpArgR, /* argument is a register or a jump offset */
222 OpArgK /* argument is a constant or register/constant */
225 extern const u8 ktap_opmodes[NUM_OPCODES];
227 #define getOpMode(m) ((enum OpMode)ktap_opmodes[m] & 3)
228 #define getBMode(m) ((enum OpArgMask)(ktap_opmodes[m] >> 4) & 3)
229 #define getCMode(m) ((enum OpArgMask)(ktap_opmodes[m] >> 2) & 3)
230 #define testAMode(m) (ktap_opmodes[m] & (1 << 6))
231 #define testTMode(m) (ktap_opmodes[m] & (1 << 7))
234 /* number of list items to accumulate before a SETLIST instruction */
235 #define LFIELDS_PER_FLUSH 50
237 extern const char *const ktap_opnames[NUM_OPCODES + 1];
239 #endif /* __KTAP_BYTECODE_H__ */