Imported from ../lua-4.0.1.tar.gz.
[lua.git] / src / lopcodes.h
blob2df72ce7e895a8cbe1898a48dc1d6c44faa93ec7
1 /*
2 ** $Id: lopcodes.h,v 1.68 2000/10/24 16:05:59 roberto Exp $
3 ** Opcodes for Lua virtual machine
4 ** See Copyright Notice in lua.h
5 */
7 #ifndef lopcodes_h
8 #define lopcodes_h
10 #include "llimits.h"
13 /*===========================================================================
14 We assume that instructions are unsigned numbers.
15 All instructions have an opcode in the first 6 bits. Moreover,
16 an instruction can have 0, 1, or 2 arguments. Instructions can
17 have the following types:
18 type 0: no arguments
19 type 1: 1 unsigned argument in the higher bits (called `U')
20 type 2: 1 signed argument in the higher bits (`S')
21 type 3: 1st unsigned argument in the higher bits (`A')
22 2nd unsigned argument in the middle bits (`B')
24 A signed argument is represented in excess K; that is, the number
25 value is the unsigned value minus K. K is exactly the maximum value
26 for that argument (so that -max is represented by 0, and +max is
27 represented by 2*max), which is half the maximum for the corresponding
28 unsigned argument.
30 The size of each argument is defined in `llimits.h'. The usual is an
31 instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
32 with 9 bits, and A arguments with 17 bits (32-6-9). For small
33 installations, the instruction size can be 16, so U has 10 bits,
34 and A and B have 5 bits each.
35 ===========================================================================*/
40 /* creates a mask with `n' 1 bits at position `p' */
41 #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
43 /* creates a mask with `n' 0 bits at position `p' */
44 #define MASK0(n,p) (~MASK1(n,p))
47 ** the following macros help to manipulate instructions
50 #define CREATE_0(o) ((Instruction)(o))
51 #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
52 #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
54 #define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
55 #define GETARG_U(i) ((int)((i)>>POS_U))
56 #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
57 ((Instruction)(u)<<POS_U)))
59 #define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
60 #define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
61 #define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
64 #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
65 | ((Instruction)(b)<<POS_B))
66 #define GETARG_A(i) ((int)((i)>>POS_A))
67 #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
68 ((Instruction)(a)<<POS_A)))
69 #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
70 #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
71 ((Instruction)(b)<<POS_B)))
75 ** K = U argument used as index to `kstr'
76 ** J = S argument used as jump offset (relative to pc of next instruction)
77 ** L = unsigned argument used as index of local variable
78 ** N = U argument used as index to `knum'
81 typedef enum {
82 /*----------------------------------------------------------------------
83 name args stack before stack after side effects
84 ------------------------------------------------------------------------*/
85 OP_END,/* - - (return) no results */
86 OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
88 OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
89 OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
91 OP_PUSHNIL,/* U - nil_1-nil_u */
92 OP_POP,/* U a_u-a_1 - */
94 OP_PUSHINT,/* S - (Number)s */
95 OP_PUSHSTRING,/* K - KSTR[k] */
96 OP_PUSHNUM,/* N - KNUM[n] */
97 OP_PUSHNEGNUM,/* N - -KNUM[n] */
99 OP_PUSHUPVALUE,/* U - Closure[u] */
101 OP_GETLOCAL,/* L - LOC[l] */
102 OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
104 OP_GETTABLE,/* - i t t[i] */
105 OP_GETDOTTED,/* K t t[KSTR[k]] */
106 OP_GETINDEXED,/* L t t[LOC[l]] */
107 OP_PUSHSELF,/* K t t t[KSTR[k]] */
109 OP_CREATETABLE,/* U - newarray(size = u) */
111 OP_SETLOCAL,/* L x - LOC[l]=x */
112 OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
113 OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
115 OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
116 OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
118 OP_ADD,/* - y x x+y */
119 OP_ADDI,/* S x x+s */
120 OP_SUB,/* - y x x-y */
121 OP_MULT,/* - y x x*y */
122 OP_DIV,/* - y x x/y */
123 OP_POW,/* - y x x^y */
124 OP_CONCAT,/* U v_u-v_1 v1..-..v_u */
125 OP_MINUS,/* - x -x */
126 OP_NOT,/* - x (x==nil)? 1 : nil */
128 OP_JMPNE,/* J y x - (x~=y)? PC+=s */
129 OP_JMPEQ,/* J y x - (x==y)? PC+=s */
130 OP_JMPLT,/* J y x - (x<y)? PC+=s */
131 OP_JMPLE,/* J y x - (x<y)? PC+=s */
132 OP_JMPGT,/* J y x - (x>y)? PC+=s */
133 OP_JMPGE,/* J y x - (x>=y)? PC+=s */
135 OP_JMPT,/* J x - (x~=nil)? PC+=s */
136 OP_JMPF,/* J x - (x==nil)? PC+=s */
137 OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
138 OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
139 OP_JMP,/* J - - PC+=s */
141 OP_PUSHNILJMP,/* - - nil PC++; */
143 OP_FORPREP,/* J */
144 OP_FORLOOP,/* J */
146 OP_LFORPREP,/* J */
147 OP_LFORLOOP,/* J */
149 OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
151 } OpCode;
153 #define NUM_OPCODES ((int)OP_CLOSURE+1)
156 #define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
160 /* special code to fit a LUA_MULTRET inside an argB */
161 #define MULT_RET 255 /* (<=MAXARG_B) */
162 #if MULT_RET>MAXARG_B
163 #undef MULT_RET
164 #define MULT_RET MAXARG_B
165 #endif
168 #endif