summaryrefslogtreecommitdiff
path: root/src/lopcodes.h
diff options
context:
space:
mode:
authorLua Team <team@lua.org>2000-11-06 12:00:00 +0000
committerrepogen <>2000-11-06 12:00:00 +0000
commit8cb71cb5548e3138e5d4e4744f52c79d9fafb116 (patch)
tree25859eb162c67eafc46866e0ec3a9a7ebf93157a /src/lopcodes.h
parentb7610da5fed99f59ac73ae452da8839a0f2c1bda (diff)
downloadlua-github-4.0.tar.gz
Lua 4.04.0
Diffstat (limited to 'src/lopcodes.h')
-rw-r--r--src/lopcodes.h210
1 files changed, 120 insertions, 90 deletions
diff --git a/src/lopcodes.h b/src/lopcodes.h
index 6a59b39e..2df72ce7 100644
--- a/src/lopcodes.h
+++ b/src/lopcodes.h
@@ -1,5 +1,5 @@
/*
-** $Id: lopcodes.h,v 1.33 1999/06/17 17:04:03 roberto Exp $
+** $Id: lopcodes.h,v 1.68 2000/10/24 16:05:59 roberto Exp $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -7,132 +7,162 @@
#ifndef lopcodes_h
#define lopcodes_h
+#include "llimits.h"
-/*
-** NOTICE: variants of the same opcode must be consecutive: First, those
-** with word parameter, then with byte parameter.
-*/
+/*===========================================================================
+ We assume that instructions are unsigned numbers.
+ All instructions have an opcode in the first 6 bits. Moreover,
+ an instruction can have 0, 1, or 2 arguments. Instructions can
+ have the following types:
+ type 0: no arguments
+ 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')
-typedef enum {
-/* name parm before after side effect
------------------------------------------------------------------------------*/
-ENDCODE,/* - - (return) */
-RETCODE,/* b - (return) */
+ 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.
-CALL,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
+ The size of each argument is defined in `llimits.h'. The usual is an
+ 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
+ installations, the instruction size can be 16, so U has 10 bits,
+ and A and B have 5 bits each.
+===========================================================================*/
-TAILCALL,/* b c v_c...v_1 f (return) f(v1,...,v_c) */
-PUSHNIL,/* b - nil_0...nil_b */
-POP,/* b a_b...a_1 - */
-PUSHNUMBERW,/* w - (float)w */
-PUSHNUMBER,/* b - (float)b */
-PUSHNUMBERNEGW,/* w - (float)-w */
-PUSHNUMBERNEG,/* b - (float)-b */
+/* creates a mask with `n' 1 bits at position `p' */
+#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
-PUSHCONSTANTW,/*w - CNST[w] */
-PUSHCONSTANT,/* b - CNST[b] */
+/* creates a mask with `n' 0 bits at position `p' */
+#define MASK0(n,p) (~MASK1(n,p))
-PUSHUPVALUE,/* b - Closure[b] */
+/*
+** the following macros help to manipulate instructions
+*/
-PUSHLOCAL,/* b - LOC[b] */
+#define CREATE_0(o) ((Instruction)(o))
+#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
+#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
-GETGLOBALW,/* w - VAR[CNST[w]] */
-GETGLOBAL,/* b - VAR[CNST[b]] */
+#define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
+#define GETARG_U(i) ((int)((i)>>POS_U))
+#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
+ ((Instruction)(u)<<POS_U)))
-GETTABLE,/* - i t t[i] */
+#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)
-GETDOTTEDW,/* w t t[CNST[w]] */
-GETDOTTED,/* b t t[CNST[b]] */
-PUSHSELFW,/* w t t t[CNST[w]] */
-PUSHSELF,/* b t t t[CNST[b]] */
+#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)))
+#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)))
-CREATEARRAYW,/* w - newarray(size = w) */
-CREATEARRAY,/* b - newarray(size = b) */
-SETLOCAL,/* b x - LOC[b]=x */
+/*
+** K = U argument used as index to `kstr'
+** J = S argument used as jump offset (relative to pc of next instruction)
+** L = unsigned argument used as index of local variable
+** N = U argument used as index to `knum'
+*/
-SETGLOBALW,/* w x - VAR[CNST[w]]=x */
-SETGLOBAL,/* b x - VAR[CNST[b]]=x */
+typedef enum {
+/*----------------------------------------------------------------------
+name args stack before stack after side effects
+------------------------------------------------------------------------*/
+OP_END,/* - - (return) no results */
+OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
-SETTABLEPOP,/* - v i t - t[i]=v */
+OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
+OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
-SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
+OP_PUSHNIL,/* U - nil_1-nil_u */
+OP_POP,/* U a_u-a_1 - */
-SETLISTW,/* w c v_c...v_1 t t t[i+w*FPF]=v_i */
-SETLIST,/* b c v_c...v_1 t t t[i+b*FPF]=v_i */
+OP_PUSHINT,/* S - (Number)s */
+OP_PUSHSTRING,/* K - KSTR[k] */
+OP_PUSHNUM,/* N - KNUM[n] */
+OP_PUSHNEGNUM,/* N - -KNUM[n] */
-SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
+OP_PUSHUPVALUE,/* U - Closure[u] */
-NEQOP,/* - y x (x~=y)? 1 : nil */
-EQOP,/* - y x (x==y)? 1 : nil */
-LTOP,/* - y x (x<y)? 1 : nil */
-LEOP,/* - y x (x<y)? 1 : nil */
-GTOP,/* - y x (x>y)? 1 : nil */
-GEOP,/* - y x (x>=y)? 1 : nil */
-ADDOP,/* - y x x+y */
-SUBOP,/* - y x x-y */
-MULTOP,/* - y x x*y */
-DIVOP,/* - y x x/y */
-POWOP,/* - y x x^y */
-CONCOP,/* - y x x..y */
-MINUSOP,/* - x -x */
-NOTOP,/* - x (x==nil)? 1 : nil */
+OP_GETLOCAL,/* L - LOC[l] */
+OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
-ONTJMPW,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
-ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
-ONFJMPW,/* w x (x==nil)? x : - (x==nil)? PC+=w */
-ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
-JMPW,/* w - - PC+=w */
-JMP,/* b - - PC+=b */
-IFFJMPW,/* w x - (x==nil)? PC+=w */
-IFFJMP,/* b x - (x==nil)? PC+=b */
-IFTUPJMPW,/* w x - (x!=nil)? PC-=w */
-IFTUPJMP,/* b x - (x!=nil)? PC-=b */
-IFFUPJMPW,/* w x - (x==nil)? PC-=w */
-IFFUPJMP,/* b x - (x==nil)? PC-=b */
+OP_GETTABLE,/* - i t t[i] */
+OP_GETDOTTED,/* K t t[KSTR[k]] */
+OP_GETINDEXED,/* L t t[LOC[l]] */
+OP_PUSHSELF,/* K t t t[KSTR[k]] */
-CLOSUREW,/* w c v_c...v_1 closure(CNST[w], v_c...v_1) */
-CLOSURE,/* b c v_c...v_1 closure(CNST[b], v_c...v_1) */
+OP_CREATETABLE,/* U - newarray(size = u) */
-SETLINEW,/* w - - LINE=w */
-SETLINE,/* b - - LINE=b */
+OP_SETLOCAL,/* L x - LOC[l]=x */
+OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
+OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
-LONGARGW,/* w (add w*(1<<16) to arg of next instruction) */
-LONGARG,/* b (add b*(1<<16) to arg of next instruction) */
+OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
+OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
-CHECKSTACK /* b (assert #temporaries == b; only for internal debuging!) */
+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 */
+OP_MINUS,/* - x -x */
+OP_NOT,/* - x (x==nil)? 1 : nil */
-} OpCode;
+OP_JMPNE,/* J y x - (x~=y)? PC+=s */
+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 */
+OP_JMPT,/* J x - (x~=nil)? PC+=s */
+OP_JMPF,/* J x - (x==nil)? PC+=s */
+OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
+OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
+OP_JMP,/* J - - PC+=s */
-#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
-#define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) */
+OP_PUSHNILJMP,/* - - nil PC++; */
-#define ZEROVARARG 128
+OP_FORPREP,/* J */
+OP_FORLOOP,/* J */
+OP_LFORPREP,/* J */
+OP_LFORLOOP,/* J */
-/* maximum value of an arg of 3 bytes; must fit in an "int" */
-#if MAX_INT < (1<<24)
-#define MAX_ARG MAX_INT
-#else
-#define MAX_ARG ((1<<24)-1)
-#endif
+OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
-/* maximum value of a word of 2 bytes; cannot be bigger than MAX_ARG */
-#if MAX_ARG < (1<<16)
-#define MAX_WORD MAX_ARG
-#else
-#define MAX_WORD ((1<<16)-1)
-#endif
+} OpCode;
+
+#define NUM_OPCODES ((int)OP_CLOSURE+1)
-/* maximum value of a byte */
-#define MAX_BYTE ((1<<8)-1)
+#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
+
+
+
+/* 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
#endif