Mercurial > vba-linux
view src/lua/lopcodes.h @ 44:a80a707cc402
add readme.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 26 Feb 2013 10:13:11 +0000 |
parents | 27763b933818 |
children |
line wrap: on
line source
1 /*2 ** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $3 ** Opcodes for Lua virtual machine4 ** See Copyright Notice in lua.h5 */7 #ifndef lopcodes_h8 #define lopcodes_h10 #include "llimits.h"13 /*===========================================================================14 We assume that instructions are unsigned numbers.15 All instructions have an opcode in the first 6 bits.16 Instructions can have the following fields:17 `A' : 8 bits18 `B' : 9 bits19 `C' : 9 bits20 `Bx' : 18 bits (`B' and `C' together)21 `sBx' : signed Bx23 A signed argument is represented in excess K; that is, the number24 value is the unsigned value minus K. K is exactly the maximum value25 for that argument (so that -max is represented by 0, and +max is26 represented by 2*max), which is half the maximum for the corresponding27 unsigned argument.28 ===========================================================================*/31 enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */34 /*35 ** size and position of opcode arguments.36 */37 #define SIZE_C 938 #define SIZE_B 939 #define SIZE_Bx (SIZE_C + SIZE_B)40 #define SIZE_A 842 #define SIZE_OP 644 #define POS_OP 045 #define POS_A (POS_OP + SIZE_OP)46 #define POS_C (POS_A + SIZE_A)47 #define POS_B (POS_C + SIZE_C)48 #define POS_Bx POS_C51 /*52 ** limits for opcode arguments.53 ** we use (signed) int to manipulate most arguments,54 ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)55 */56 #if SIZE_Bx < LUAI_BITSINT-157 #define MAXARG_Bx ((1<<SIZE_Bx)-1)58 #define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */59 #else60 #define MAXARG_Bx MAX_INT61 #define MAXARG_sBx MAX_INT62 #endif65 #define MAXARG_A ((1<<SIZE_A)-1)66 #define MAXARG_B ((1<<SIZE_B)-1)67 #define MAXARG_C ((1<<SIZE_C)-1)70 /* creates a mask with `n' 1 bits at position `p' */71 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)73 /* creates a mask with `n' 0 bits at position `p' */74 #define MASK0(n,p) (~MASK1(n,p))76 /*77 ** the following macros help to manipulate instructions78 */80 #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))81 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \82 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))84 #define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))85 #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \86 ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))88 #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))89 #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \90 ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))92 #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))93 #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \94 ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))96 #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))97 #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \98 ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))100 #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)101 #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))104 #define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \105 | (cast(Instruction, a)<<POS_A) \106 | (cast(Instruction, b)<<POS_B) \107 | (cast(Instruction, c)<<POS_C))109 #define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \110 | (cast(Instruction, a)<<POS_A) \111 | (cast(Instruction, bc)<<POS_Bx))114 /*115 ** Macros to operate RK indices116 */118 /* this bit 1 means constant (0 means register) */119 #define BITRK (1 << (SIZE_B - 1))121 /* test whether value is a constant */122 #define ISK(x) ((x) & BITRK)124 /* gets the index of the constant */125 #define INDEXK(r) ((int)(r) & ~BITRK)127 #define MAXINDEXRK (BITRK - 1)129 /* code a constant index as a RK value */130 #define RKASK(x) ((x) | BITRK)133 /*134 ** invalid register that fits in 8 bits135 */136 #define NO_REG MAXARG_A139 /*140 ** R(x) - register141 ** Kst(x) - constant (in constant table)142 ** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)143 */146 /*147 ** grep "ORDER OP" if you change these enums148 */150 typedef enum {151 /*----------------------------------------------------------------------152 name args description153 ------------------------------------------------------------------------*/154 OP_MOVE,/* A B R(A) := R(B) */155 OP_LOADK,/* A Bx R(A) := Kst(Bx) */156 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */157 OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */158 OP_GETUPVAL,/* A B R(A) := UpValue[B] */160 OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */161 OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */163 OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */164 OP_SETUPVAL,/* A B UpValue[B] := R(A) */165 OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */167 OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */169 OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */171 OP_ADD,/* A B C R(A) := RK(B) + RK(C) */172 OP_SUB,/* A B C R(A) := RK(B) - RK(C) */173 OP_MUL,/* A B C R(A) := RK(B) * RK(C) */174 OP_DIV,/* A B C R(A) := RK(B) / RK(C) */175 OP_MOD,/* A B C R(A) := RK(B) % RK(C) */176 OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */177 OP_UNM,/* A B R(A) := -R(B) */178 OP_NOT,/* A B R(A) := not R(B) */179 OP_LEN,/* A B R(A) := length of R(B) */181 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */183 OP_JMP,/* sBx pc+=sBx */185 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */186 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */187 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */189 OP_TEST,/* A C if not (R(A) <=> C) then pc++ */190 OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */192 OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */193 OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */194 OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */196 OP_FORLOOP,/* A sBx R(A)+=R(A+2);197 if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/198 OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */200 OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));201 if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */202 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */204 OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/205 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */207 OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */208 } OpCode;211 #define NUM_OPCODES (cast(int, OP_VARARG) + 1)215 /*===========================================================================216 Notes:217 (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,218 and can be 0: OP_CALL then sets `top' to last_result+1, so219 next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.221 (*) In OP_VARARG, if (B == 0) then use actual number of varargs and222 set top (like in OP_CALL with C == 0).224 (*) In OP_RETURN, if (B == 0) then return up to `top'226 (*) In OP_SETLIST, if (B == 0) then B = `top';227 if (C == 0) then next `instruction' is real C229 (*) For comparisons, A specifies what condition the test should accept230 (true or false).232 (*) All `skips' (pc++) assume that next instruction is a jump233 ===========================================================================*/236 /*237 ** masks for instruction properties. The format is:238 ** bits 0-1: op mode239 ** bits 2-3: C arg mode240 ** bits 4-5: B arg mode241 ** bit 6: instruction set register A242 ** bit 7: operator is a test243 */245 enum OpArgMask {246 OpArgN, /* argument is not used */247 OpArgU, /* argument is used */248 OpArgR, /* argument is a register or a jump offset */249 OpArgK /* argument is a constant or register/constant */250 };252 LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];254 #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))255 #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))256 #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))257 #define testAMode(m) (luaP_opmodes[m] & (1 << 6))258 #define testTMode(m) (luaP_opmodes[m] & (1 << 7))261 LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */264 /* number of list items to accumulate before a SETLIST instruction */265 #define LFIELDS_PER_FLUSH 50268 #endif