/*1** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $2** Opcodes for Lua virtual machine3** See Copyright Notice in lua.h4*/56#ifndef lopcodes_h7#define lopcodes_h89#include "llimits.h"101112/*===========================================================================13We assume that instructions are unsigned numbers.14All instructions have an opcode in the first 6 bits.15Instructions can have the following fields:16`A' : 8 bits17`B' : 9 bits18`C' : 9 bits19`Bx' : 18 bits (`B' and `C' together)20`sBx' : signed Bx2122A signed argument is represented in excess K; that is, the number23value is the unsigned value minus K. K is exactly the maximum value24for that argument (so that -max is represented by 0, and +max is25represented by 2*max), which is half the maximum for the corresponding26unsigned argument.27===========================================================================*/282930enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */313233/*34** size and position of opcode arguments.35*/36#define SIZE_C 937#define SIZE_B 938#define SIZE_Bx (SIZE_C + SIZE_B)39#define SIZE_A 84041#define SIZE_OP 64243#define POS_OP 044#define POS_A (POS_OP + SIZE_OP)45#define POS_C (POS_A + SIZE_A)46#define POS_B (POS_C + SIZE_C)47#define POS_Bx POS_C484950/*51** limits for opcode arguments.52** we use (signed) int to manipulate most arguments,53** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)54*/55#if SIZE_Bx < LUAI_BITSINT-156#define MAXARG_Bx ((1<<SIZE_Bx)-1)57#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */58#else59#define MAXARG_Bx MAX_INT60#define MAXARG_sBx MAX_INT61#endif626364#define MAXARG_A ((1<<SIZE_A)-1)65#define MAXARG_B ((1<<SIZE_B)-1)66#define MAXARG_C ((1<<SIZE_C)-1)676869/* creates a mask with `n' 1 bits at position `p' */70#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)7172/* creates a mask with `n' 0 bits at position `p' */73#define MASK0(n,p) (~MASK1(n,p))7475/*76** the following macros help to manipulate instructions77*/7879#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))80#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \81((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))8283#define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))84#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \85((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))8687#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))88#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \89((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))9091#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))92#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \93((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))9495#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))96#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \97((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))9899#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)100#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))101102103#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \104| (cast(Instruction, a)<<POS_A) \105| (cast(Instruction, b)<<POS_B) \106| (cast(Instruction, c)<<POS_C))107108#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \109| (cast(Instruction, a)<<POS_A) \110| (cast(Instruction, bc)<<POS_Bx))111112113/*114** Macros to operate RK indices115*/116117/* this bit 1 means constant (0 means register) */118#define BITRK (1 << (SIZE_B - 1))119120/* test whether value is a constant */121#define ISK(x) ((x) & BITRK)122123/* gets the index of the constant */124#define INDEXK(r) ((int)(r) & ~BITRK)125126#define MAXINDEXRK (BITRK - 1)127128/* code a constant index as a RK value */129#define RKASK(x) ((x) | BITRK)130131132/*133** invalid register that fits in 8 bits134*/135#define NO_REG MAXARG_A136137138/*139** R(x) - register140** Kst(x) - constant (in constant table)141** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)142*/143144145/*146** grep "ORDER OP" if you change these enums147*/148149typedef enum {150/*----------------------------------------------------------------------151name args description152------------------------------------------------------------------------*/153OP_MOVE,/* A B R(A) := R(B) */154OP_LOADK,/* A Bx R(A) := Kst(Bx) */155OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */156OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */157OP_GETUPVAL,/* A B R(A) := UpValue[B] */158159OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */160OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */161162OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */163OP_SETUPVAL,/* A B UpValue[B] := R(A) */164OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */165166OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */167168OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */169170OP_ADD,/* A B C R(A) := RK(B) + RK(C) */171OP_SUB,/* A B C R(A) := RK(B) - RK(C) */172OP_MUL,/* A B C R(A) := RK(B) * RK(C) */173OP_DIV,/* A B C R(A) := RK(B) / RK(C) */174OP_MOD,/* A B C R(A) := RK(B) % RK(C) */175OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */176OP_UNM,/* A B R(A) := -R(B) */177OP_NOT,/* A B R(A) := not R(B) */178OP_LEN,/* A B R(A) := length of R(B) */179180OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */181182OP_JMP,/* sBx pc+=sBx */183184OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */185OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */186OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */187188OP_TEST,/* A C if not (R(A) <=> C) then pc++ */189OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */190191OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */192OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */193OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */194195OP_FORLOOP,/* A sBx R(A)+=R(A+2);196if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/197OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */198199OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));200if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */201OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */202203OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/204OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */205206OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */207} OpCode;208209210#define NUM_OPCODES (cast(int, OP_VARARG) + 1)211212213214/*===========================================================================215Notes:216(*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,217and can be 0: OP_CALL then sets `top' to last_result+1, so218next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.219220(*) In OP_VARARG, if (B == 0) then use actual number of varargs and221set top (like in OP_CALL with C == 0).222223(*) In OP_RETURN, if (B == 0) then return up to `top'224225(*) In OP_SETLIST, if (B == 0) then B = `top';226if (C == 0) then next `instruction' is real C227228(*) For comparisons, A specifies what condition the test should accept229(true or false).230231(*) All `skips' (pc++) assume that next instruction is a jump232===========================================================================*/233234235/*236** masks for instruction properties. The format is:237** bits 0-1: op mode238** bits 2-3: C arg mode239** bits 4-5: B arg mode240** bit 6: instruction set register A241** bit 7: operator is a test242*/243244enum OpArgMask {245OpArgN, /* argument is not used */246OpArgU, /* argument is used */247OpArgR, /* argument is a register or a jump offset */248OpArgK /* argument is a constant or register/constant */249};250251LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];252253#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))254#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))255#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))256#define testAMode(m) (luaP_opmodes[m] & (1 << 6))257#define testTMode(m) (luaP_opmodes[m] & (1 << 7))258259260LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */261262263/* number of list items to accumulate before a SETLIST instruction */264#define LFIELDS_PER_FLUSH 50265266267#endif268269270