L0001 /* L0002 ** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ L0003 ** Opcodes for Lua virtual machine L0004 ** See Copyright Notice in lua.h L0005 */ L0006 L0007 #ifndef lopcodes_h L0008 #define lopcodes_h L0009 L0010 #include "llimits.h" L0011 L0012 L0013 /*=========================================================================== L0014 We assume that instructions are unsigned numbers. L0015 All instructions have an opcode in the first 6 bits. L0016 Instructions can have the following fields: L0017 `A' : 8 bits L0018 `B' : 9 bits L0019 `C' : 9 bits L0020 `Bx' : 18 bits (`B' and `C' together) L0021 `sBx' : signed Bx L0022 L0023 A signed argument is represented in excess K; that is, the number L0024 value is the unsigned value minus K. K is exactly the maximum value L0025 for that argument (so that -max is represented by 0, and +max is L0026 represented by 2*max), which is half the maximum for the corresponding L0027 unsigned argument. L0028 ===========================================================================*/ L0029 L0030 L0031 enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ L0032 L0033 L0034 /* L0035 ** size and position of opcode arguments. L0036 */ L0037 #define SIZE_C 9 L0038 #define SIZE_B 9 L0039 #define SIZE_Bx (SIZE_C + SIZE_B) L0040 #define SIZE_A 8 L0041 L0042 #define SIZE_OP 6 L0043 L0044 #define POS_OP 0 L0045 #define POS_A (POS_OP + SIZE_OP) L0046 #define POS_C (POS_A + SIZE_A) L0047 #define POS_B (POS_C + SIZE_C) L0048 #define POS_Bx POS_C L0049 L0050 L0051 /* L0052 ** limits for opcode arguments. L0053 ** we use (signed) int to manipulate most arguments, L0054 ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) L0055 */ L0056 #if SIZE_Bx < LUAI_BITSINT-1 L0057 #define MAXARG_Bx ((1<<SIZE_Bx)-1) L0058 #define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */ L0059 #else L0060 #define MAXARG_Bx MAX_INT L0061 #define MAXARG_sBx MAX_INT L0062 #endif L0063 L0064 L0065 #define MAXARG_A ((1<<SIZE_A)-1) L0066 #define MAXARG_B ((1<<SIZE_B)-1) L0067 #define MAXARG_C ((1<<SIZE_C)-1) L0068 L0069 L0070 /* creates a mask with `n' 1 bits at position `p' */ L0071 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p) L0072 L0073 /* creates a mask with `n' 0 bits at position `p' */ L0074 #define MASK0(n,p) (~MASK1(n,p)) L0075 L0076 /* L0077 ** the following macros help to manipulate instructions L0078 */ L0079 L0080 #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) L0081 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ L0082 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) L0083 L0084 #define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))Gets integer A fields bits of Instruction i.L0085 #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ L0086 ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))Sets (in-place) A field bits of Instruction i to integer v.L0087 L0088 #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) L0089 #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ L0090 ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B)))) L0091 L0092 #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) L0093 #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ L0094 ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C)))) L0095 L0096 #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) L0097 #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ L0098 ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx)))) L0099 L0100 #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) L0101 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) L0102 L0103 L0104 #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \ L0105 | (cast(Instruction, a)<<POS_A) \ L0106 | (cast(Instruction, b)<<POS_B) \ L0107 | (cast(Instruction, c)<<POS_C))Returns Instruction formed from opcode (OpCode) o and given integer A, B, C fields.L0108 L0109 #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \ L0110 | (cast(Instruction, a)<<POS_A) \ L0111 | (cast(Instruction, bc)<<POS_Bx)) L0112 L0113 L0114 /* L0115 ** Macros to operate RK indices L0116 */ L0117 L0118 /* this bit 1 means constant (0 means register) */ L0119 #define BITRK (1 << (SIZE_B - 1)) L0120 L0121 /* test whether value is a constant */ L0122 #define ISK(x) ((x) & BITRK) L0123 L0124 /* gets the index of the constant */ L0125 #define INDEXK(r) ((int)(r) & ~BITRK) L0126 L0127 #define MAXINDEXRK (BITRK - 1) L0128 L0129 /* code a constant index as a RK value */ L0130 #define RKASK(x) ((x) | BITRK) L0131 L0132 L0133 /* L0134 ** invalid register that fits in 8 bits L0135 */ L0136 #define NO_REG MAXARG_A L0137 L0138 L0139 /* L0140 ** R(x) - register L0141 ** Kst(x) - constant (in constant table) L0142 ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x) L0143 */ L0144 L0145 L0146 /* L0147 ** grep "ORDER OP" if you change these enums L0148 */ L0149 L0150 typedef enum { L0151 /*---------------------------------------------------------------------- L0152 name args description L0153 ------------------------------------------------------------------------*/ L0154 OP_MOVE,/* A B R(A) := R(B) */ L0155 OP_LOADK,/* A Bx R(A) := Kst(Bx) */ L0156 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ L0157 OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ L0158 OP_GETUPVAL,/* A B R(A) := UpValue[B] */ L0159 L0160 OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ L0161 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ L0162 L0163 OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ L0164 OP_SETUPVAL,/* A B UpValue[B] := R(A) */ L0165 OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ L0166 L0167 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ L0168 L0169 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ L0170 L0171 OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ L0172 OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ L0173 OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ L0174 OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ L0175 OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ L0176 OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ L0177 OP_UNM,/* A B R(A) := -R(B) */ L0178 OP_NOT,/* A B R(A) := not R(B) */ L0179 OP_LEN,/* A B R(A) := length of R(B) */ L0180 L0181 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ L0182 L0183 OP_JMP,/* sBx pc+=sBx */ L0184 L0185 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ L0186 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ L0187 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ L0188 L0189 OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ L0190 OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ L0191 L0192 OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ L0193 OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ L0194 OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ L0195 L0196 OP_FORLOOP,/* A sBx R(A)+=R(A+2); L0197 if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ L0198 OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ L0199 L0200 OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); L0201 if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ L0202 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ L0203 L0204 OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ L0205 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ L0206 L0207 OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ L0208 } OpCode; L0209 L0210 L0211 #define NUM_OPCODES (cast(int, OP_VARARG) + 1) L0212 L0213 L0214 L0215 /*=========================================================================== L0216 Notes: L0217 (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, L0218 and can be 0: OP_CALL then sets `top' to last_result+1, so L0219 next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. L0220 L0221 (*) In OP_VARARG, if (B == 0) then use actual number of varargs and L0222 set top (like in OP_CALL with C == 0). L0223 L0224 (*) In OP_RETURN, if (B == 0) then return up to `top' L0225 L0226 (*) In OP_SETLIST, if (B == 0) then B = `top'; L0227 if (C == 0) then next `instruction' is real C L0228 L0229 (*) For comparisons, A specifies what condition the test should accept L0230 (true or false). L0231 L0232 (*) All `skips' (pc++) assume that next instruction is a jump L0233 ===========================================================================*/ L0234 L0235 L0236 /* L0237 ** masks for instruction properties. The format is: L0238 ** bits 0-1: op mode L0239 ** bits 2-3: C arg mode L0240 ** bits 4-5: B arg mode L0241 ** bit 6: instruction set register A L0242 ** bit 7: operator is a test L0243 */ L0244 L0245 enum OpArgMask { L0246 OpArgN, /* argument is not used */ L0247 OpArgU, /* argument is used */ L0248 OpArgR, /* argument is a register or a jump offset */ L0249 OpArgK /* argument is a constant or register/constant */ L0250 }; L0251 L0252 LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; L0253 L0254 #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) L0255 #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) L0256 #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) L0257 #define testAMode(m) (luaP_opmodes[m] & (1 << 6)) L0258 #define testTMode(m) (luaP_opmodes[m] & (1 << 7)) L0259 L0260 L0261 LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ L0262 L0263 L0264 /* number of list items to accumulate before a SETLIST instruction */ L0265 #define LFIELDS_PER_FLUSH 50 L0266 L0267 L0268 #endif