/*1** $Id: lopcodes.h $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 32-bit integers.14All instructions have an opcode in the first 7 bits.15Instructions can have the following formats:16173 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0181 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 019iABC C(8) | B(8) |k| A(8) | Op(7) |20iABx Bx(17) | A(8) | Op(7) |21iAsBx sBx (signed)(17) | A(8) | Op(7) |22iAx Ax(25) | Op(7) |23isJ sJ (signed)(25) | Op(7) |2425A signed argument is represented in excess K: the represented value is26the written unsigned value minus K, where K is half the maximum for the27corresponding unsigned argument.28===========================================================================*/293031enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */323334/*35** size and position of opcode arguments.36*/37#define SIZE_C 838#define SIZE_B 839#define SIZE_Bx (SIZE_C + SIZE_B + 1)40#define SIZE_A 841#define SIZE_Ax (SIZE_Bx + SIZE_A)42#define SIZE_sJ (SIZE_Bx + SIZE_A)4344#define SIZE_OP 74546#define POS_OP 04748#define POS_A (POS_OP + SIZE_OP)49#define POS_k (POS_A + SIZE_A)50#define POS_B (POS_k + 1)51#define POS_C (POS_B + SIZE_B)5253#define POS_Bx POS_k5455#define POS_Ax POS_A5657#define POS_sJ POS_A585960/*61** limits for opcode arguments.62** we use (signed) 'int' to manipulate most arguments,63** so they must fit in ints.64*/6566/* Check whether type 'int' has at least 'b' bits ('b' < 32) */67#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1)686970#if L_INTHASBITS(SIZE_Bx)71#define MAXARG_Bx ((1<<SIZE_Bx)-1)72#else73#define MAXARG_Bx MAX_INT74#endif7576#define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */777879#if L_INTHASBITS(SIZE_Ax)80#define MAXARG_Ax ((1<<SIZE_Ax)-1)81#else82#define MAXARG_Ax MAX_INT83#endif8485#if L_INTHASBITS(SIZE_sJ)86#define MAXARG_sJ ((1 << SIZE_sJ) - 1)87#else88#define MAXARG_sJ MAX_INT89#endif9091#define OFFSET_sJ (MAXARG_sJ >> 1)929394#define MAXARG_A ((1<<SIZE_A)-1)95#define MAXARG_B ((1<<SIZE_B)-1)96#define MAXARG_C ((1<<SIZE_C)-1)97#define OFFSET_sC (MAXARG_C >> 1)9899#define int2sC(i) ((i) + OFFSET_sC)100#define sC2int(i) ((i) - OFFSET_sC)101102103/* creates a mask with 'n' 1 bits at position 'p' */104#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))105106/* creates a mask with 'n' 0 bits at position 'p' */107#define MASK0(n,p) (~MASK1(n,p))108109/*110** the following macros help to manipulate instructions111*/112113#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))114#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \115((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))116117#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)118119120#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))121#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \122((cast(Instruction, v)<<pos)&MASK1(size,pos))))123124#define GETARG_A(i) getarg(i, POS_A, SIZE_A)125#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)126127#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))128#define GETARG_sB(i) sC2int(GETARG_B(i))129#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)130131#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))132#define GETARG_sC(i) sC2int(GETARG_C(i))133#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)134135#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))136#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))137#define SETARG_k(i,v) setarg(i, v, POS_k, 1)138139#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))140#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)141142#define GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))143#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)144145#define GETARG_sBx(i) \146check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)147#define SETARG_sBx(i,b) SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))148149#define GETARG_sJ(i) \150check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)151#define SETARG_sJ(i,j) \152setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)153154155#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \156| (cast(Instruction, a)<<POS_A) \157| (cast(Instruction, b)<<POS_B) \158| (cast(Instruction, c)<<POS_C) \159| (cast(Instruction, k)<<POS_k))160161#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \162| (cast(Instruction, a)<<POS_A) \163| (cast(Instruction, bc)<<POS_Bx))164165#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \166| (cast(Instruction, a)<<POS_Ax))167168#define CREATE_sJ(o,j,k) ((cast(Instruction, o) << POS_OP) \169| (cast(Instruction, j) << POS_sJ) \170| (cast(Instruction, k) << POS_k))171172173#if !defined(MAXINDEXRK) /* (for debugging only) */174#define MAXINDEXRK MAXARG_B175#endif176177178/*179** invalid register that fits in 8 bits180*/181#define NO_REG MAXARG_A182183184/*185** R[x] - register186** K[x] - constant (in constant table)187** RK(x) == if k(i) then K[x] else R[x]188*/189190191/*192** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)193** has extra descriptions in the notes after the enumeration.194*/195196typedef enum {197/*----------------------------------------------------------------------198name args description199------------------------------------------------------------------------*/200OP_MOVE,/* A B R[A] := R[B] */201OP_LOADI,/* A sBx R[A] := sBx */202OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */203OP_LOADK,/* A Bx R[A] := K[Bx] */204OP_LOADKX,/* A R[A] := K[extra arg] */205OP_LOADFALSE,/* A R[A] := false */206OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */207OP_LOADTRUE,/* A R[A] := true */208OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */209OP_GETUPVAL,/* A B R[A] := UpValue[B] */210OP_SETUPVAL,/* A B UpValue[B] := R[A] */211212OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */213OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */214OP_GETI,/* A B C R[A] := R[B][C] */215OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */216217OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */218OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */219OP_SETI,/* A B C R[A][B] := RK(C) */220OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */221222OP_NEWTABLE,/* A B C k R[A] := {} */223224OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */225226OP_ADDI,/* A B sC R[A] := R[B] + sC */227228OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */229OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */230OP_MULK,/* A B C R[A] := R[B] * K[C]:number */231OP_MODK,/* A B C R[A] := R[B] % K[C]:number */232OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */233OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */234OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */235236OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */237OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */238OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */239240OP_SHRI,/* A B sC R[A] := R[B] >> sC */241OP_SHLI,/* A B sC R[A] := sC << R[B] */242243OP_ADD,/* A B C R[A] := R[B] + R[C] */244OP_SUB,/* A B C R[A] := R[B] - R[C] */245OP_MUL,/* A B C R[A] := R[B] * R[C] */246OP_MOD,/* A B C R[A] := R[B] % R[C] */247OP_POW,/* A B C R[A] := R[B] ^ R[C] */248OP_DIV,/* A B C R[A] := R[B] / R[C] */249OP_IDIV,/* A B C R[A] := R[B] // R[C] */250251OP_BAND,/* A B C R[A] := R[B] & R[C] */252OP_BOR,/* A B C R[A] := R[B] | R[C] */253OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */254OP_SHL,/* A B C R[A] := R[B] << R[C] */255OP_SHR,/* A B C R[A] := R[B] >> R[C] */256257OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */258OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */259OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */260261OP_UNM,/* A B R[A] := -R[B] */262OP_BNOT,/* A B R[A] := ~R[B] */263OP_NOT,/* A B R[A] := not R[B] */264OP_LEN,/* A B R[A] := #R[B] (length operator) */265266OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */267268OP_CLOSE,/* A close all upvalues >= R[A] */269OP_TBC,/* A mark variable A "to be closed" */270OP_JMP,/* sJ pc += sJ */271OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */272OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */273OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */274275OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */276OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */277OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */278OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */279OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */280OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */281282OP_TEST,/* A k if (not R[A] == k) then pc++ */283OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */284285OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */286OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */287288OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */289OP_RETURN0,/* return */290OP_RETURN1,/* A return R[A] */291292OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */293OP_FORPREP,/* A Bx <check values and prepare counters>;294if not to run then pc+=Bx+1; */295296OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */297OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */298OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */299300OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */301302OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */303304OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */305306OP_VARARGPREP,/*A (adjust vararg parameters) */307308OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */309} OpCode;310311312#define NUM_OPCODES ((int)(OP_EXTRAARG) + 1)313314315316/*===========================================================================317Notes:318319(*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean320value, in a code equivalent to (not cond ? false : true). (It321produces false and skips the next instruction producing true.)322323(*) Opcodes OP_MMBIN and variants follow each arithmetic and324bitwise opcode. If the operation succeeds, it skips this next325opcode. Otherwise, this opcode calls the corresponding metamethod.326327(*) Opcode OP_TESTSET is used in short-circuit expressions that need328both to jump and to produce a value, such as (a = b or c).329330(*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then331'top' is set to last_result+1, so next open instruction (OP_CALL,332OP_RETURN*, OP_SETLIST) may use 'top'.333334(*) In OP_VARARG, if (C == 0) then use actual number of varargs and335set top (like in OP_CALL with C == 0).336337(*) In OP_RETURN, if (B == 0) then return up to 'top'.338339(*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always340OP_EXTRAARG.341342(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then343real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the344bits of C).345346(*) In OP_NEWTABLE, B is log2 of the hash size (which is always a347power of 2) plus 1, or zero for size zero. If not k, the array size348is C. Otherwise, the array size is EXTRAARG _ C.349350(*) For comparisons, k specifies what condition the test should accept351(true or false).352353(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped354(the constant is the first operand).355356(*) All 'skips' (pc++) assume that next instruction is a jump.357358(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the359function builds upvalues, which may need to be closed. C > 0 means360the function is vararg, so that its 'func' must be corrected before361returning; in this case, (C - 1) is its number of fixed parameters.362363(*) In comparisons with an immediate operand, C signals whether the364original operand was a float. (It must be corrected in case of365metamethods.)366367===========================================================================*/368369370/*371** masks for instruction properties. The format is:372** bits 0-2: op mode373** bit 3: instruction set register A374** bit 4: operator is a test (next instruction must be a jump)375** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)376** bit 6: instruction sets 'L->top' for next instruction (when C == 0)377** bit 7: instruction is an MM instruction (call a metamethod)378*/379380LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)381382#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))383#define testAMode(m) (luaP_opmodes[m] & (1 << 3))384#define testTMode(m) (luaP_opmodes[m] & (1 << 4))385#define testITMode(m) (luaP_opmodes[m] & (1 << 5))386#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))387#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))388389/* "out top" (set top for next instruction) */390#define isOT(i) \391((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \392GET_OPCODE(i) == OP_TAILCALL)393394/* "in top" (uses top from previous instruction) */395#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)396397#define opmode(mm,ot,it,t,a,m) \398(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))399400401/* number of list items to accumulate before a SETLIST instruction */402#define LFIELDS_PER_FLUSH 50403404#endif405406407