diff options
Diffstat (limited to 'lopcodes.h')
-rw-r--r-- | lopcodes.h | 77 |
1 files changed, 44 insertions, 33 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lopcodes.h,v 1.52 2000/03/24 19:49:23 roberto Exp roberto $ | 2 | ** $Id: lopcodes.h,v 1.53 2000/03/27 14:31:12 roberto Exp roberto $ |
3 | ** Opcodes for Lua virtual machine | 3 | ** Opcodes for Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -13,26 +13,30 @@ | |||
13 | /*=========================================================================== | 13 | /*=========================================================================== |
14 | We assume that instructions are unsigned numbers. | 14 | We assume that instructions are unsigned numbers. |
15 | All instructions have an opcode in the first 6 bits. Moreover, | 15 | All instructions have an opcode in the first 6 bits. Moreover, |
16 | an instruction can have 0, 1, or 2 arguments. There are 4 types of | 16 | an instruction can have 0, 1, or 2 arguments. Instructions can |
17 | Instructions: | 17 | have the following types: |
18 | type 0: no arguments | 18 | type 0: no arguments |
19 | type 1: 1 unsigned argument in the higher bits (called `U') | 19 | type 1: 1 unsigned argument in the higher bits (called `U') |
20 | type 2: 1 signed argument in the higher bits (`S') | 20 | type 2: 1 signed argument in the higher bits (`S') |
21 | type 3: 1st unsigned argument in the higher bits (`A') | 21 | type 3: 1st unsigned argument in the higher bits (`A') |
22 | 2nd unsigned argument in the middle bits (`B') | 22 | 2nd unsigned argument in the middle bits (`B') |
23 | 23 | ||
24 | The signed argument is represented in excess 2^K; that is, the number | 24 | A signed argument is represented in excess K; that is, the number |
25 | value is the usigned value minus 2^K. | 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. | ||
26 | 29 | ||
27 | The size of each argument is defined in `llimits.h'. The usual is an | 30 | The size of each argument is defined in `llimits.h'. The usual is an |
28 | instruction with 32 bits, U and S arguments with 26 bits (32-6), B | 31 | instruction with 32 bits, U arguments with 26 bits (32-6), B arguments |
29 | argument with 9 bits, and A argument with 17 bits (32-6-9). For small | 32 | with 9 bits, and A arguments with 17 bits (32-6-9). For small |
30 | instalations, the instruction size can be 16, so U and S have 10 bits, | 33 | instalations, the instruction size can be 16, so U has 10 bits, |
31 | and A and B have 5 bits each. | 34 | and A and B have 5 bits each. |
32 | ===========================================================================*/ | 35 | ===========================================================================*/ |
33 | 36 | ||
34 | 37 | ||
35 | #define EXCESS_S (1<<(SIZE_S-1)) /* == 2^K */ | 38 | |
39 | #define MAXARG_sA (MAXARG_A>>1) /* max value for a signed A */ | ||
36 | 40 | ||
37 | 41 | ||
38 | /* creates a mask with `n' 1 bits at position `p' */ | 42 | /* creates a mask with `n' 1 bits at position `p' */ |
@@ -45,27 +49,33 @@ | |||
45 | ** the following macros help to manipulate instructions | 49 | ** the following macros help to manipulate instructions |
46 | */ | 50 | */ |
47 | 51 | ||
52 | #define CREATE_0(o) ((Instruction)(o)) | ||
48 | #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0))) | 53 | #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0))) |
49 | #define GETARG_U(i) ((int)((i)>>POS_U)) | ||
50 | #define GETARG_S(i) ((int)((i)>>POS_S)-EXCESS_S) | ||
51 | #define GETARG_A(i) ((int)((i)>>POS_A)) | ||
52 | #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0))) | ||
53 | |||
54 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))) | 54 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))) |
55 | |||
56 | #define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U) | ||
57 | #define GETARG_U(i) ((int)((i)>>POS_U)) | ||
55 | #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \ | 58 | #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \ |
56 | ((Instruction)(u)<<POS_U))) | 59 | ((Instruction)(u)<<POS_U))) |
57 | #define SETARG_S(i,s) ((i) = (((i)&MASK0(SIZE_S,POS_S)) | \ | 60 | |
58 | ((Instruction)((s)+EXCESS_S)<<POS_S))) | 61 | #define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S) |
62 | #define GETARG_S(i) (GETARG_U(i)-MAXARG_S) | ||
63 | #define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S) | ||
64 | |||
65 | |||
66 | #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \ | ||
67 | | ((Instruction)(b)<<POS_B)) | ||
68 | #define GETARG_A(i) ((int)((i)>>POS_A)) | ||
59 | #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ | 69 | #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ |
60 | ((Instruction)(a)<<POS_A))) | 70 | ((Instruction)(a)<<POS_A))) |
71 | #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0))) | ||
61 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ | 72 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ |
62 | ((Instruction)(b)<<POS_B))) | 73 | ((Instruction)(b)<<POS_B))) |
63 | 74 | ||
64 | #define CREATE_0(o) ((Instruction)(o)) | 75 | |
65 | #define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U) | 76 | #define CREATE_sAB(o,a,b) (CREATE_AB((o),(a)+MAXARG_sA,(b))) |
66 | #define CREATE_S(o,s) ((Instruction)(o) | ((Instruction)(s)+EXCESS_S)<<POS_S) | 77 | #define GETARG_sA(i) (GETARG_A(i)-MAXARG_sA) |
67 | #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \ | 78 | |
68 | | ((Instruction)(b)<<POS_B)) | ||
69 | 79 | ||
70 | 80 | ||
71 | /* | 81 | /* |
@@ -112,6 +122,7 @@ OP_SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */ | |||
112 | OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */ | 122 | OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */ |
113 | OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */ | 123 | OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */ |
114 | 124 | ||
125 | OP_INCLOCAL,/* sA B - - LOC[B]+=sA */ | ||
115 | OP_ADD,/* - y x x+y */ | 126 | OP_ADD,/* - y x x+y */ |
116 | OP_ADDI,/* S x x+s */ | 127 | OP_ADDI,/* S x x+s */ |
117 | OP_SUB,/* - y x x-y */ | 128 | OP_SUB,/* - y x x-y */ |
@@ -122,17 +133,17 @@ OP_CONC,/* U v_u-v_1 v1..-..v_u */ | |||
122 | OP_MINUS,/* - x -x */ | 133 | OP_MINUS,/* - x -x */ |
123 | OP_NOT,/* - x (x==nil)? 1 : nil */ | 134 | OP_NOT,/* - x (x==nil)? 1 : nil */ |
124 | 135 | ||
125 | OP_IFNEQJMP,/* J y x - (x~=y)? PC+=s */ | 136 | OP_JMPNEQ,/* J y x - (x~=y)? PC+=s */ |
126 | OP_IFEQJMP,/* J y x - (x==y)? PC+=s */ | 137 | OP_JMPEQ,/* J y x - (x==y)? PC+=s */ |
127 | OP_IFLTJMP,/* J y x - (x<y)? PC+=s */ | 138 | OP_JMPLT,/* J y x - (x<y)? PC+=s */ |
128 | OP_IFLEJMP,/* J y x - (x<y)? PC+=s */ | 139 | OP_JMPLE,/* J y x - (x<y)? PC+=s */ |
129 | OP_IFGTJMP,/* J y x - (x>y)? PC+=s */ | 140 | OP_JMPGT,/* J y x - (x>y)? PC+=s */ |
130 | OP_IFGEJMP,/* J y x - (x>=y)? PC+=s */ | 141 | OP_JMPGE,/* J y x - (x>=y)? PC+=s */ |
131 | 142 | ||
132 | OP_IFTJMP,/* J x - (x!=nil)? PC+=s */ | 143 | OP_JMPT,/* J x - (x!=nil)? PC+=s */ |
133 | OP_IFFJMP,/* J x - (x==nil)? PC+=s */ | 144 | OP_JMPF,/* J x - (x==nil)? PC+=s */ |
134 | OP_ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */ | 145 | OP_JMPONT,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */ |
135 | OP_ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */ | 146 | OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */ |
136 | OP_JMP,/* J - - PC+=s */ | 147 | OP_JMP,/* J - - PC+=s */ |
137 | 148 | ||
138 | OP_PUSHNILJMP,/* - - nil PC++; */ | 149 | OP_PUSHNILJMP,/* - - nil PC++; */ |
@@ -145,7 +156,7 @@ OP_SETLINE/* U - - LINE=u */ | |||
145 | 156 | ||
146 | 157 | ||
147 | 158 | ||
148 | #define ISJUMP(o) (OP_IFNEQJMP <= (o) && (o) <= OP_JMP) | 159 | #define ISJUMP(o) (OP_JMPNEQ <= (o) && (o) <= OP_JMP) |
149 | 160 | ||
150 | 161 | ||
151 | #endif | 162 | #endif |