Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ktap / include / ktap_opcodes.h
1 #ifndef __KTAP_BYTECODE_H__
2 #define __KTAP_BYTECODE_H__
3
4
5 /* opcode is copied from lua initially */
6
7 typedef enum {
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]                              */
17
18 OP_GETTABUP,/*  A B C   R(A) := UpValue[B][RK(C)]                       */
19 OP_GETTABLE,/*  A B C   R(A) := R(B)[RK(C)]                             */
20
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)                      */
28
29 OP_NEWTABLE,/*  A B C   R(A) := {} (size = B,C)                         */
30
31 OP_SELF,/*      A B C   R(A+1) := R(B); R(A) := R(B)[RK(C)]             */
32
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)                          */
42
43 OP_CONCAT,/*    A B C   R(A) := R(B).. ... ..R(C)                       */
44
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++            */
49
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++     */
52
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)      */
56
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                           */
60
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 }*/
63
64 OP_SETLIST,/*   A B C   R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B        */
65
66 OP_CLOSURE,/*   A Bx    R(A) := closure(KPROTO[Bx])                     */
67
68 OP_VARARG,/*    A B     R(A), R(A+1), ..., R(A+B-2) = vararg            */
69
70 OP_EXTRAARG,/*   Ax      extra (larger) argument for previous opcode     */
71
72 OP_EVENT,/*  A B C   R(A) := R(B)[C]                             */
73
74 OP_EVENTNAME, /* A      R(A) = event_name() */
75
76 OP_EVENTARG,/* A B      R(A) := event_arg(B)*/
77
78 OP_LOAD_GLOBAL,/*  A B C   R(A) := R(B)[C]                             */
79
80 OP_EXIT,
81
82 } OpCode;
83
84
85 #define NUM_OPCODES     ((int)OP_LOAD_GLOBAL + 1)
86
87
88 enum OpMode {iABC, iABx, iAsBx, iAx};  /* basic instruction format */
89
90
91 /*
92  * ** size and position of opcode arguments.
93  * */
94 #define SIZE_C          9
95 #define SIZE_B          9
96 #define SIZE_Bx         (SIZE_C + SIZE_B)
97 #define SIZE_A          8
98 #define SIZE_Ax         (SIZE_C + SIZE_B + SIZE_A)
99
100 #define SIZE_OP         6
101
102 #define POS_OP          0
103 #define POS_A           (POS_OP + SIZE_OP)
104 #define POS_C           (POS_A + SIZE_A)
105 #define POS_B           (POS_C + SIZE_C)
106 #define POS_Bx          POS_C
107 #define POS_Ax          POS_A
108
109
110
111 /*
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)
115  * */
116 #define MAXARG_Bx        ((1<<SIZE_Bx)-1)
117 #define MAXARG_sBx        (MAXARG_Bx>>1)         /* `sBx' is signed */
118
119 #define MAXARG_Ax       ((1<<SIZE_Ax)-1)
120
121 #define MAXARG_A        ((1<<SIZE_A)-1)
122 #define MAXARG_B        ((1<<SIZE_B)-1)
123 #define MAXARG_C        ((1<<SIZE_C)-1)
124
125
126 /* creates a mask with `n' 1 bits at position `p' */
127 #define MASK1(n,p)      ((~((~(ktap_instruction)0)<<(n)))<<(p))
128
129 /* creates a mask with `n' 0 bits at position `p' */
130 #define MASK0(n,p)      (~MASK1(n,p))
131
132 /*
133  * ** the following macros help to manipulate instructions
134  * */
135
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))))
139
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))))
143
144 #define GETARG_A(i)     getarg(i, POS_A, SIZE_A)
145 #define SETARG_A(i,v)   setarg(i, v, POS_A, SIZE_A)
146
147 #define GETARG_B(i)     getarg(i, POS_B, SIZE_B)
148 #define SETARG_B(i,v)   setarg(i, v, POS_B, SIZE_B)
149
150 #define GETARG_C(i)     getarg(i, POS_C, SIZE_C)
151 #define SETARG_C(i,v)   setarg(i, v, POS_C, SIZE_C)
152
153 #define GETARG_Bx(i)    getarg(i, POS_Bx, SIZE_Bx)
154 #define SETARG_Bx(i,v)  setarg(i, v, POS_Bx, SIZE_Bx)
155
156 #define GETARG_Ax(i)    getarg(i, POS_Ax, SIZE_Ax)
157 #define SETARG_Ax(i,v)  setarg(i, v, POS_Ax, SIZE_Ax)
158
159 #define GETARG_sBx(i)   (GETARG_Bx(i)-MAXARG_sBx)
160 #define SETARG_sBx(i,b) SETARG_Bx((i), (unsigned int)(b)+MAXARG_sBx)
161
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)
166
167 #define CREATE_ABx(o,a,bc)      (((ktap_instruction)(o))<<POS_OP) \
168                         | (((ktap_instruction)(a))<<POS_A) \
169                         | (((ktap_instruction)(bc))<<POS_Bx)
170
171 #define CREATE_Ax(o,a)          (((ktap_instruction)(o))<<POS_OP) \
172                         | (((ktap_instruction)(a))<<POS_Ax)
173
174
175
176 /*
177  * ** Macros to operate RK indices
178  * */
179
180 /* this bit 1 means constant (0 means register) */
181 #define BITRK           (1 << (SIZE_B - 1))
182
183 /* test whether value is a constant */
184 #define ISK(x)          ((x) & BITRK)
185
186 /* gets the index of the constant */
187 #define INDEXK(r)       ((int)(r) & ~BITRK)
188
189 #define MAXINDEXRK      (BITRK - 1)
190
191 /* code a constant index as a RK value */
192 #define RKASK(x)        ((x) | BITRK)
193
194
195 /*
196  * ** invalid register that fits in 8 bits
197  * */
198 #define NO_REG          MAXARG_A
199
200
201 /*
202  * ** R(x) - register
203  * ** Kst(x) - constant (in constant table)
204  * ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
205  * */
206
207
208
209 /*
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)
216  * */
217
218 enum OpArgMask {
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 */
223 };
224
225 extern const u8 ktap_opmodes[NUM_OPCODES];
226
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))
232
233
234 /* number of list items to accumulate before a SETLIST instruction */
235 #define LFIELDS_PER_FLUSH       50
236
237 extern const char *const ktap_opnames[NUM_OPCODES + 1];
238
239 #endif /* __KTAP_BYTECODE_H__ */