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