annotate src/lua/src/lopcodes.h @ 1:f9f4f1b99eed

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