1 | ;;; opcodes.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2006 Peter Graves |
---|
4 | ;;; $Id: opcodes.lisp 12185 2009-10-10 13:12:09Z ehuelsmann $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package #:jvm) |
---|
33 | |
---|
34 | (defconst *opcode-table* (make-array 256)) |
---|
35 | |
---|
36 | (defconst *opcodes* (make-hash-table :test 'equalp)) |
---|
37 | |
---|
38 | (defstruct jvm-opcode name number size stack-effect) |
---|
39 | |
---|
40 | (defun %define-opcode (name number size stack-effect) |
---|
41 | (declare (type fixnum number size)) |
---|
42 | (let* ((name (string name)) |
---|
43 | (opcode (make-jvm-opcode :name name |
---|
44 | :number number |
---|
45 | :size size |
---|
46 | :stack-effect stack-effect))) |
---|
47 | (setf (svref *opcode-table* number) opcode) |
---|
48 | (setf (gethash name *opcodes*) opcode) |
---|
49 | (setf (gethash number *opcodes*) opcode))) |
---|
50 | |
---|
51 | (defmacro define-opcode (name number size stack-effect) |
---|
52 | `(%define-opcode ',name ,number ,size ,stack-effect)) |
---|
53 | |
---|
54 | ;; name number size stack-effect (nil if unknown) |
---|
55 | (define-opcode nop 0 1 0) |
---|
56 | (define-opcode aconst_null 1 1 1) |
---|
57 | (define-opcode iconst_m1 2 1 1) |
---|
58 | (define-opcode iconst_0 3 1 1) |
---|
59 | (define-opcode iconst_1 4 1 1) |
---|
60 | (define-opcode iconst_2 5 1 1) |
---|
61 | (define-opcode iconst_3 6 1 1) |
---|
62 | (define-opcode iconst_4 7 1 1) |
---|
63 | (define-opcode iconst_5 8 1 1) |
---|
64 | (define-opcode lconst_0 9 1 2) |
---|
65 | (define-opcode lconst_1 10 1 2) |
---|
66 | (define-opcode fconst_0 11 1 1) |
---|
67 | (define-opcode fconst_1 12 1 1) |
---|
68 | (define-opcode fconst_2 13 1 1) |
---|
69 | (define-opcode dconst_0 14 1 2) |
---|
70 | (define-opcode dconst_1 15 1 2) |
---|
71 | (define-opcode bipush 16 2 1) |
---|
72 | (define-opcode sipush 17 3 1) |
---|
73 | (define-opcode ldc 18 2 1) |
---|
74 | (define-opcode ldc_w 19 3 1) |
---|
75 | (define-opcode ldc2_w 20 3 2) |
---|
76 | (define-opcode iload 21 2 1) |
---|
77 | (define-opcode lload 22 2 2) |
---|
78 | (define-opcode fload 23 2 nil) |
---|
79 | (define-opcode dload 24 2 nil) |
---|
80 | (define-opcode aload 25 2 1) |
---|
81 | (define-opcode iload_0 26 1 1) |
---|
82 | (define-opcode iload_1 27 1 1) |
---|
83 | (define-opcode iload_2 28 1 1) |
---|
84 | (define-opcode iload_3 29 1 1) |
---|
85 | (define-opcode lload_0 30 1 2) |
---|
86 | (define-opcode lload_1 31 1 2) |
---|
87 | (define-opcode lload_2 32 1 2) |
---|
88 | (define-opcode lload_3 33 1 2) |
---|
89 | (define-opcode fload_0 34 1 nil) |
---|
90 | (define-opcode fload_1 35 1 nil) |
---|
91 | (define-opcode fload_2 36 1 nil) |
---|
92 | (define-opcode fload_3 37 1 nil) |
---|
93 | (define-opcode dload_0 38 1 nil) |
---|
94 | (define-opcode dload_1 39 1 nil) |
---|
95 | (define-opcode dload_2 40 1 nil) |
---|
96 | (define-opcode dload_3 41 1 nil) |
---|
97 | (define-opcode aload_0 42 1 1) |
---|
98 | (define-opcode aload_1 43 1 1) |
---|
99 | (define-opcode aload_2 44 1 1) |
---|
100 | (define-opcode aload_3 45 1 1) |
---|
101 | (define-opcode iaload 46 1 -1) |
---|
102 | (define-opcode laload 47 1 0) |
---|
103 | (define-opcode faload 48 1 -1) |
---|
104 | (define-opcode daload 49 1 0) |
---|
105 | (define-opcode aaload 50 1 -1) |
---|
106 | (define-opcode baload 51 1 nil) |
---|
107 | (define-opcode caload 52 1 nil) |
---|
108 | (define-opcode saload 53 1 nil) |
---|
109 | (define-opcode istore 54 2 -1) |
---|
110 | (define-opcode lstore 55 2 -2) |
---|
111 | (define-opcode fstore 56 2 nil) |
---|
112 | (define-opcode dstore 57 2 nil) |
---|
113 | (define-opcode astore 58 2 -1) |
---|
114 | (define-opcode istore_0 59 1 -1) |
---|
115 | (define-opcode istore_1 60 1 -1) |
---|
116 | (define-opcode istore_2 61 1 -1) |
---|
117 | (define-opcode istore_3 62 1 -1) |
---|
118 | (define-opcode lstore_0 63 1 -2) |
---|
119 | (define-opcode lstore_1 64 1 -2) |
---|
120 | (define-opcode lstore_2 65 1 -2) |
---|
121 | (define-opcode lstore_3 66 1 -2) |
---|
122 | (define-opcode fstore_0 67 1 nil) |
---|
123 | (define-opcode fstore_1 68 1 nil) |
---|
124 | (define-opcode fstore_2 69 1 nil) |
---|
125 | (define-opcode fstore_3 70 1 nil) |
---|
126 | (define-opcode dstore_0 71 1 nil) |
---|
127 | (define-opcode dstore_1 72 1 nil) |
---|
128 | (define-opcode dstore_2 73 1 nil) |
---|
129 | (define-opcode dstore_3 74 1 nil) |
---|
130 | (define-opcode astore_0 75 1 -1) |
---|
131 | (define-opcode astore_1 76 1 -1) |
---|
132 | (define-opcode astore_2 77 1 -1) |
---|
133 | (define-opcode astore_3 78 1 -1) |
---|
134 | (define-opcode iastore 79 1 -3) |
---|
135 | (define-opcode lastore 80 1 -4) |
---|
136 | (define-opcode fastore 81 1 -3) |
---|
137 | (define-opcode dastore 82 1 -4) |
---|
138 | (define-opcode aastore 83 1 -3) |
---|
139 | (define-opcode bastore 84 1 nil) |
---|
140 | (define-opcode castore 85 1 nil) |
---|
141 | (define-opcode sastore 86 1 nil) |
---|
142 | (define-opcode pop 87 1 -1) |
---|
143 | (define-opcode pop2 88 1 -2) |
---|
144 | (define-opcode dup 89 1 1) |
---|
145 | (define-opcode dup_x1 90 1 1) |
---|
146 | (define-opcode dup_x2 91 1 1) |
---|
147 | (define-opcode dup2 92 1 2) |
---|
148 | (define-opcode dup2_x1 93 1 2) |
---|
149 | (define-opcode dup2_x2 94 1 2) |
---|
150 | (define-opcode swap 95 1 0) |
---|
151 | (define-opcode iadd 96 1 -1) |
---|
152 | (define-opcode ladd 97 1 -2) |
---|
153 | (define-opcode fadd 98 1 -1) |
---|
154 | (define-opcode dadd 99 1 -2) |
---|
155 | (define-opcode isub 100 1 -1) |
---|
156 | (define-opcode lsub 101 1 -2) |
---|
157 | (define-opcode fsub 102 1 -1) |
---|
158 | (define-opcode dsub 103 1 -2) |
---|
159 | (define-opcode imul 104 1 -1) |
---|
160 | (define-opcode lmul 105 1 -2) |
---|
161 | (define-opcode fmul 106 1 -1) |
---|
162 | (define-opcode dmul 107 1 -2) |
---|
163 | (define-opcode idiv 108 1 nil) |
---|
164 | (define-opcode ldiv 109 1 nil) |
---|
165 | (define-opcode fdiv 110 1 nil) |
---|
166 | (define-opcode ddiv 111 1 nil) |
---|
167 | (define-opcode irem 112 1 nil) |
---|
168 | (define-opcode lrem 113 1 nil) |
---|
169 | (define-opcode frem 114 1 nil) |
---|
170 | (define-opcode drem 115 1 nil) |
---|
171 | (define-opcode ineg 116 1 0) |
---|
172 | (define-opcode lneg 117 1 0) |
---|
173 | (define-opcode fneg 118 1 0) |
---|
174 | (define-opcode dneg 119 1 0) |
---|
175 | (define-opcode ishl 120 1 -1) |
---|
176 | (define-opcode lshl 121 1 -1) |
---|
177 | (define-opcode ishr 122 1 -1) |
---|
178 | (define-opcode lshr 123 1 -1) |
---|
179 | (define-opcode iushr 124 1 nil) |
---|
180 | (define-opcode lushr 125 1 nil) |
---|
181 | (define-opcode iand 126 1 -1) |
---|
182 | (define-opcode land 127 1 -2) |
---|
183 | (define-opcode ior 128 1 -1) |
---|
184 | (define-opcode lor 129 1 -2) |
---|
185 | (define-opcode ixor 130 1 -1) |
---|
186 | (define-opcode lxor 131 1 -2) |
---|
187 | (define-opcode iinc 132 3 0) |
---|
188 | (define-opcode i2l 133 1 1) |
---|
189 | (define-opcode i2f 134 1 0) |
---|
190 | (define-opcode i2d 135 1 1) |
---|
191 | (define-opcode l2i 136 1 -1) |
---|
192 | (define-opcode l2f 137 1 -1) |
---|
193 | (define-opcode l2d 138 1 0) |
---|
194 | (define-opcode f2i 139 1 nil) |
---|
195 | (define-opcode f2l 140 1 nil) |
---|
196 | (define-opcode f2d 141 1 1) |
---|
197 | (define-opcode d2i 142 1 nil) |
---|
198 | (define-opcode d2l 143 1 nil) |
---|
199 | (define-opcode d2f 144 1 -1) |
---|
200 | (define-opcode i2b 145 1 nil) |
---|
201 | (define-opcode i2c 146 1 nil) |
---|
202 | (define-opcode i2s 147 1 nil) |
---|
203 | (define-opcode lcmp 148 1 -3) |
---|
204 | (define-opcode fcmpl 149 1 -1) |
---|
205 | (define-opcode fcmpg 150 1 -1) |
---|
206 | (define-opcode dcmpl 151 1 -3) |
---|
207 | (define-opcode dcmpg 152 1 -3) |
---|
208 | (define-opcode ifeq 153 3 -1) |
---|
209 | (define-opcode ifne 154 3 -1) |
---|
210 | (define-opcode iflt 155 3 -1) |
---|
211 | (define-opcode ifge 156 3 -1) |
---|
212 | (define-opcode ifgt 157 3 -1) |
---|
213 | (define-opcode ifle 158 3 -1) |
---|
214 | (define-opcode if_icmpeq 159 3 -2) |
---|
215 | (define-opcode if_icmpne 160 3 -2) |
---|
216 | (define-opcode if_icmplt 161 3 -2) |
---|
217 | (define-opcode if_icmpge 162 3 -2) |
---|
218 | (define-opcode if_icmpgt 163 3 -2) |
---|
219 | (define-opcode if_icmple 164 3 -2) |
---|
220 | (define-opcode if_acmpeq 165 3 -2) |
---|
221 | (define-opcode if_acmpne 166 3 -2) |
---|
222 | (define-opcode goto 167 3 0) |
---|
223 | ;;(define-opcode jsr 168 3 1) Don't use these 2 opcodes: deprecated |
---|
224 | ;;(define-opcode ret 169 2 0) their use results in JVM verifier errors |
---|
225 | (define-opcode tableswitch 170 0 nil) |
---|
226 | (define-opcode lookupswitch 171 0 nil) |
---|
227 | (define-opcode ireturn 172 1 nil) |
---|
228 | (define-opcode lreturn 173 1 nil) |
---|
229 | (define-opcode freturn 174 1 nil) |
---|
230 | (define-opcode dreturn 175 1 nil) |
---|
231 | (define-opcode areturn 176 1 -1) |
---|
232 | (define-opcode return 177 1 0) |
---|
233 | (define-opcode getstatic 178 3 1) |
---|
234 | (define-opcode putstatic 179 3 -1) |
---|
235 | (define-opcode getfield 180 3 0) |
---|
236 | (define-opcode putfield 181 3 -2) |
---|
237 | (define-opcode invokevirtual 182 3 nil) |
---|
238 | (define-opcode invokespecial 183 3 nil) |
---|
239 | (define-opcode invokestatic 184 3 nil) |
---|
240 | (define-opcode invokeinterface 185 5 nil) |
---|
241 | (define-opcode unused 186 0 nil) |
---|
242 | (define-opcode new 187 3 1) |
---|
243 | (define-opcode newarray 188 2 nil) |
---|
244 | (define-opcode anewarray 189 3 0) |
---|
245 | (define-opcode arraylength 190 1 0) |
---|
246 | (define-opcode athrow 191 1 0) |
---|
247 | (define-opcode checkcast 192 3 0) |
---|
248 | (define-opcode instanceof 193 3 0) |
---|
249 | (define-opcode monitorenter 194 1 -1) |
---|
250 | (define-opcode monitorexit 195 1 -1) |
---|
251 | (define-opcode wide 196 0 nil) |
---|
252 | (define-opcode multianewarray 197 4 nil) |
---|
253 | (define-opcode ifnull 198 3 -1) |
---|
254 | (define-opcode ifnonnull 199 3 nil) |
---|
255 | (define-opcode goto_w 200 5 nil) |
---|
256 | (define-opcode jsr_w 201 5 nil) |
---|
257 | (define-opcode label 202 0 0) |
---|
258 | ;; (define-opcode push-value 203 nil 1) |
---|
259 | ;; (define-opcode store-value 204 nil -1) |
---|
260 | (define-opcode clear-values 205 0 0) |
---|
261 | ;;(define-opcode var-ref 206 0 0) |
---|
262 | |
---|
263 | (defparameter *last-opcode* 206) |
---|
264 | |
---|
265 | (declaim (ftype (function (t) t) opcode-name)) |
---|
266 | (defun opcode-name (opcode-number) |
---|
267 | (let ((opcode (gethash opcode-number *opcodes*))) |
---|
268 | (and opcode (jvm-opcode-name opcode)))) |
---|
269 | |
---|
270 | (declaim (ftype (function (t) (integer 0 255)) opcode-number)) |
---|
271 | (defun opcode-number (opcode-name) |
---|
272 | (declare (optimize speed)) |
---|
273 | (let ((opcode (gethash (string opcode-name) *opcodes*))) |
---|
274 | (if opcode |
---|
275 | (jvm-opcode-number opcode) |
---|
276 | (error "Unknown opcode ~S." opcode-name)))) |
---|
277 | |
---|
278 | (declaim (ftype (function (t) fixnum) opcode-size)) |
---|
279 | (defun opcode-size (opcode-number) |
---|
280 | (declare (optimize speed (safety 0))) |
---|
281 | (declare (type (integer 0 255) opcode-number)) |
---|
282 | (jvm-opcode-size (svref *opcode-table* opcode-number))) |
---|
283 | |
---|
284 | (declaim (ftype (function (t) t) opcode-stack-effect)) |
---|
285 | (defun opcode-stack-effect (opcode-number) |
---|
286 | (declare (optimize speed)) |
---|
287 | (jvm-opcode-stack-effect (svref *opcode-table* opcode-number))) |
---|
288 | |
---|
289 | (provide '#:opcodes) |
---|