lua/lopcodes.h

167 lines
5.1 KiB
C
Raw Normal View History

1997-09-16 12:25:59 -07:00
/*
** $Id: lopcodes.h,v 1.71 2001/03/07 13:22:55 roberto Exp roberto $
1997-09-16 12:25:59 -07:00
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
#ifndef lopcodes_h
#define lopcodes_h
2000-03-24 11:49:23 -08:00
#include "llimits.h"
1997-09-16 12:25:59 -07:00
2000-02-14 08:51:08 -08:00
/*===========================================================================
2000-03-24 09:26:08 -08:00
We assume that instructions are unsigned numbers.
All instructions have an opcode in the first 6 bits. Moreover,
2000-04-04 13:48:44 -07:00
an instruction can have 0, 1, or 2 arguments. Instructions can
have the following types:
2000-02-14 08:51:08 -08:00
type 0: no arguments
2000-03-24 09:26:08 -08:00
type 1: 1 unsigned argument in the higher bits (called `U')
type 2: 1 signed argument in the higher bits (`S')
type 3: 1st unsigned argument in the higher bits (`A')
2nd unsigned argument in the middle bits (`B')
2000-04-04 13:48:44 -07:00
A signed argument is represented in excess K; that is, the number
value is the unsigned value minus K. K is exactly the maximum value
for that argument (so that -max is represented by 0, and +max is
represented by 2*max), which is half the maximum for the corresponding
unsigned argument.
2000-03-24 09:26:08 -08:00
2000-03-24 11:49:23 -08:00
The size of each argument is defined in `llimits.h'. The usual is an
2000-04-04 13:48:44 -07:00
instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
with 9 bits, and A arguments with 17 bits (32-6-9). For small
2000-08-15 11:28:48 -07:00
installations, the instruction size can be 16, so U has 10 bits,
2000-03-24 09:26:08 -08:00
and A and B have 5 bits each.
2000-02-14 08:51:08 -08:00
===========================================================================*/
1999-03-05 13:16:07 -08:00
2000-03-08 16:19:22 -08:00
2000-04-04 13:48:44 -07:00
2000-03-08 16:19:22 -08:00
/* creates a mask with `n' 1 bits at position `p' */
#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
2000-03-08 16:19:22 -08:00
/* creates a mask with `n' 0 bits at position `p' */
#define MASK0(n,p) (~MASK1(n,p))
2000-03-03 06:58:26 -08:00
2000-02-14 08:51:08 -08:00
/*
** the following macros help to manipulate instructions
*/
1997-09-16 12:25:59 -07:00
2000-04-04 13:48:44 -07:00
#define CREATE_0(o) ((Instruction)(o))
2000-03-08 16:19:22 -08:00
#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
2000-04-04 13:48:44 -07:00
#define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
2000-04-04 13:48:44 -07:00
#define GETARG_U(i) ((int)((i)>>POS_U))
#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
((Instruction)(u)<<POS_U)))
2000-04-04 13:48:44 -07:00
#define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
#define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
#define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
#define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
| ((Instruction)(b)<<POS_B))
#define GETARG_A(i) ((int)((i)>>POS_A))
#define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
((Instruction)(a)<<POS_A)))
2000-04-04 13:48:44 -07:00
#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
((Instruction)(b)<<POS_B)))
2000-04-04 13:48:44 -07:00
2000-02-22 05:31:43 -08:00
/*
** K = U argument used as index to `kstr'
** J = S argument used as jump offset (relative to pc of next instruction)
2000-04-07 12:35:31 -07:00
** L = unsigned argument used as index of local variable
2000-03-02 04:32:53 -08:00
** N = U argument used as index to `knum'
2000-02-22 05:31:43 -08:00
*/
2000-02-14 08:51:08 -08:00
typedef enum {
2000-03-03 06:58:26 -08:00
/*----------------------------------------------------------------------
name args stack before stack after side effects
------------------------------------------------------------------------*/
2000-04-07 12:35:31 -07:00
OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
2000-03-10 10:37:44 -08:00
OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
2000-03-10 10:37:44 -08:00
OP_PUSHNIL,/* U - nil_1-nil_u */
OP_POP,/* U a_u-a_1 - */
OP_PUSHINT,/* S - (lua_Number)s */
2000-03-10 10:37:44 -08:00
OP_PUSHSTRING,/* K - KSTR[k] */
2000-04-07 12:35:31 -07:00
OP_PUSHNUM,/* N - KNUM[n] */
OP_PUSHNEGNUM,/* N - -KNUM[n] */
2000-03-10 10:37:44 -08:00
OP_PUSHUPVALUE,/* U - Closure[u] */
2000-04-07 12:35:31 -07:00
OP_GETLOCAL,/* L - LOC[l] */
2000-03-10 10:37:44 -08:00
OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
1997-09-16 12:25:59 -07:00
2000-03-10 10:37:44 -08:00
OP_GETTABLE,/* - i t t[i] */
OP_GETDOTTED,/* K t t[KSTR[k]] */
2000-04-07 12:35:31 -07:00
OP_GETINDEXED,/* L t t[LOC[l]] */
2000-03-10 10:37:44 -08:00
OP_PUSHSELF,/* K t t t[KSTR[k]] */
2000-03-10 10:37:44 -08:00
OP_CREATETABLE,/* U - newarray(size = u) */
OP_SETLOCAL,/* L x - LOC[l]=x */
2000-03-10 10:37:44 -08:00
OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
2000-04-07 12:35:31 -07:00
OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
OP_SETLIST,/* A B v_n-v_1 v_b v_b v_b[i+a*FPF]=v_i */
OP_SETMAP,/* U v_n k_n - v_1 k_1 v_u v_u v_u[k_i]=v_i */
1997-09-16 12:25:59 -07:00
2000-03-10 10:37:44 -08:00
OP_ADD,/* - y x x+y */
OP_ADDI,/* S x x+s */
OP_SUB,/* - y x x-y */
OP_MULT,/* - y x x*y */
OP_DIV,/* - y x x/y */
OP_POW,/* - y x x^y */
OP_CONCAT,/* U v_u-v_1 v1..-..v_u */
2000-03-10 10:37:44 -08:00
OP_MINUS,/* - x -x */
OP_NOT,/* - x (x==nil)? 1 : nil */
1997-10-06 07:51:11 -07:00
OP_JMPNE,/* J y x - (x~=y)? PC+=s */
2000-04-04 13:48:44 -07:00
OP_JMPEQ,/* J y x - (x==y)? PC+=s */
OP_JMPLT,/* J y x - (x<y)? PC+=s */
OP_JMPLE,/* J y x - (x<y)? PC+=s */
OP_JMPGT,/* J y x - (x>y)? PC+=s */
OP_JMPGE,/* J y x - (x>=y)? PC+=s */
2000-04-07 12:35:31 -07:00
OP_JMPT,/* J x - (x~=nil)? PC+=s */
2000-04-04 13:48:44 -07:00
OP_JMPF,/* J x - (x==nil)? PC+=s */
2000-04-07 12:35:31 -07:00
OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
2000-04-04 13:48:44 -07:00
OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
2000-03-10 10:37:44 -08:00
OP_JMP,/* J - - PC+=s */
2000-03-10 10:37:44 -08:00
OP_PUSHNILJMP,/* - - nil PC++; */
2000-04-12 11:57:19 -07:00
OP_FORPREP,/* J */
OP_FORLOOP,/* J */
OP_LFORPREP,/* J */
OP_LFORLOOP,/* J */
2000-06-26 12:28:31 -07:00
OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
1998-01-12 05:35:37 -08:00
1997-09-16 12:25:59 -07:00
} OpCode;
2000-06-26 12:28:31 -07:00
#define NUM_OPCODES ((int)OP_CLOSURE+1)
1997-09-16 12:25:59 -07:00
#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
2000-08-29 07:48:16 -07:00
/* special code to fit a LUA_MULTRET inside an argB */
#define MULT_RET 255 /* (<=MAXARG_B) */
#if MULT_RET>MAXARG_B
#undef MULT_RET
#define MULT_RET MAXARG_B
#endif
1997-09-16 12:25:59 -07:00
#endif