source: branches/0.26.x/abcl/src/org/armedbear/lisp/jvm-instructions.lisp

Last change on this file was 13222, checked in by ehuelsmann, 14 years ago

Backport 'unsafe-p-removal' branch: this commit pushes back the
responsibility of maintaining stack consistency in generated (byte) code
to pass2, from a shared pass1/pass2 responsibility. The issue why it can't
happen in pass1 is because in pass1 the full structure of the lisp code
isn't known yet, due to lambda and local function inlining.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.7 KB
Line 
1;;; jvm-instructions.lisp
2;;;
3;;; Copyright (C) 2003-2006 Peter Graves
4;;; Copyright (C) 2010 Erik Huelsmann
5;;; $Id: jvm-instructions.lisp 13222 2011-02-15 22:29:22Z ehuelsmann $
6;;;
7;;; This program is free software; you can redistribute it and/or
8;;; modify it under the terms of the GNU General Public License
9;;; as published by the Free Software Foundation; either version 2
10;;; of the License, or (at your option) any later version.
11;;;
12;;; This program is distributed in the hope that it will be useful,
13;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with this program; if not, write to the Free Software
19;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20;;;
21;;; As a special exception, the copyright holders of this library give you
22;;; permission to link this library with independent modules to produce an
23;;; executable, regardless of the license terms of these independent
24;;; modules, and to copy and distribute the resulting executable under
25;;; terms of your choice, provided that you also meet, for each linked
26;;; independent module, the terms and conditions of the license of that
27;;; module.  An independent module is a module which is not derived from
28;;; or based on this library.  If you modify this library, you may extend
29;;; this exception to your version of the library, but you are not
30;;; obligated to do so.  If you do not wish to do so, delete this
31;;; exception statement from your version.
32
33(in-package #:jvm)
34
35
36;;    OPCODES
37
38(defconst *opcode-table* (make-array 256))
39
40(defconst *opcodes* (make-hash-table :test 'equalp))
41
42(defstruct jvm-opcode name number size stack-effect register-used)
43
44(defun %define-opcode (name number size stack-effect register)
45  (declare (type fixnum number size))
46  (let* ((name (string name))
47         (opcode (make-jvm-opcode :name name
48                                  :number number
49                                  :size size
50                                  :stack-effect stack-effect
51                                  :register-used register)))
52     (setf (svref *opcode-table* number) opcode)
53     (setf (gethash name *opcodes*) opcode)
54     (setf (gethash number *opcodes*) opcode)))
55
56(defmacro define-opcode (name number size stack-effect register)
57  `(%define-opcode ',name ,number ,size ,stack-effect ,register))
58
59;; name number size stack-effect register-used
60(define-opcode nop 0 1 0 nil)
61(define-opcode aconst_null 1 1 1 nil)
62(define-opcode iconst_m1 2 1 1 nil)
63(define-opcode iconst_0 3 1 1 nil)
64(define-opcode iconst_1 4 1 1 nil)
65(define-opcode iconst_2 5 1 1 nil)
66(define-opcode iconst_3 6 1 1 nil)
67(define-opcode iconst_4 7 1 1 nil)
68(define-opcode iconst_5 8 1 1 nil)
69(define-opcode lconst_0 9 1 2 nil)
70(define-opcode lconst_1 10 1 2 nil)
71(define-opcode fconst_0 11 1 1 nil)
72(define-opcode fconst_1 12 1 1 nil)
73(define-opcode fconst_2 13 1 1 nil)
74(define-opcode dconst_0 14 1 2 nil)
75(define-opcode dconst_1 15 1 2 nil)
76(define-opcode bipush 16 2 1 nil)
77(define-opcode sipush 17 3 1 nil)
78(define-opcode ldc 18 2 1 nil)
79(define-opcode ldc_w 19 3 1 nil)
80(define-opcode ldc2_w 20 3 2 nil)
81(define-opcode iload 21 2 1 t)
82(define-opcode lload 22 2 2 t)
83(define-opcode fload 23 2 nil t)
84(define-opcode dload 24 2 nil t)
85(define-opcode aload 25 2 1 t)
86(define-opcode iload_0 26 1 1 0)
87(define-opcode iload_1 27 1 1 1)
88(define-opcode iload_2 28 1 1 2)
89(define-opcode iload_3 29 1 1 3)
90(define-opcode lload_0 30 1 2 0)
91(define-opcode lload_1 31 1 2 1)
92(define-opcode lload_2 32 1 2 2)
93(define-opcode lload_3 33 1 2 3)
94(define-opcode fload_0 34 1 nil 0)
95(define-opcode fload_1 35 1 nil 1)
96(define-opcode fload_2 36 1 nil 2)
97(define-opcode fload_3 37 1 nil 3)
98(define-opcode dload_0 38 1 nil 0)
99(define-opcode dload_1 39 1 nil 1)
100(define-opcode dload_2 40 1 nil 2)
101(define-opcode dload_3 41 1 nil 3)
102(define-opcode aload_0 42 1 1 0)
103(define-opcode aload_1 43 1 1 1)
104(define-opcode aload_2 44 1 1 2)
105(define-opcode aload_3 45 1 1 3)
106(define-opcode iaload 46 1 -1 nil)
107(define-opcode laload 47 1 0 nil)
108(define-opcode faload 48 1 -1 nil)
109(define-opcode daload 49 1 0 nil)
110(define-opcode aaload 50 1 -1 nil)
111(define-opcode baload 51 1 nil nil)
112(define-opcode caload 52 1 nil nil)
113(define-opcode saload 53 1 nil nil)
114(define-opcode istore 54 2 -1 t)
115(define-opcode lstore 55 2 -2 t)
116(define-opcode fstore 56 2 nil t)
117(define-opcode dstore 57 2 nil t)
118(define-opcode astore 58 2 -1 t)
119(define-opcode istore_0 59 1 -1 0)
120(define-opcode istore_1 60 1 -1 1)
121(define-opcode istore_2 61 1 -1 2)
122(define-opcode istore_3 62 1 -1 3)
123(define-opcode lstore_0 63 1 -2 0)
124(define-opcode lstore_1 64 1 -2 1)
125(define-opcode lstore_2 65 1 -2 2)
126(define-opcode lstore_3 66 1 -2 3)
127(define-opcode fstore_0 67 1 nil 0)
128(define-opcode fstore_1 68 1 nil 1)
129(define-opcode fstore_2 69 1 nil 2)
130(define-opcode fstore_3 70 1 nil 3)
131(define-opcode dstore_0 71 1 nil 0)
132(define-opcode dstore_1 72 1 nil 1)
133(define-opcode dstore_2 73 1 nil 2)
134(define-opcode dstore_3 74 1 nil 3)
135(define-opcode astore_0 75 1 -1 0)
136(define-opcode astore_1 76 1 -1 1)
137(define-opcode astore_2 77 1 -1 2)
138(define-opcode astore_3 78 1 -1 3)
139(define-opcode iastore 79 1 -3 nil)
140(define-opcode lastore 80 1 -4 nil)
141(define-opcode fastore 81 1 -3 nil)
142(define-opcode dastore 82 1 -4 nil)
143(define-opcode aastore 83 1 -3 nil)
144(define-opcode bastore 84 1 nil nil)
145(define-opcode castore 85 1 nil nil)
146(define-opcode sastore 86 1 nil nil)
147(define-opcode pop 87 1 -1 nil)
148(define-opcode pop2 88 1 -2 nil)
149(define-opcode dup 89 1 1 nil)
150(define-opcode dup_x1 90 1 1 nil)
151(define-opcode dup_x2 91 1 1 nil)
152(define-opcode dup2 92 1 2 nil)
153(define-opcode dup2_x1 93 1 2 nil)
154(define-opcode dup2_x2 94 1 2 nil)
155(define-opcode swap 95 1 0 nil)
156(define-opcode iadd 96 1 -1 nil)
157(define-opcode ladd 97 1 -2 nil)
158(define-opcode fadd 98 1 -1 nil)
159(define-opcode dadd 99 1 -2 nil)
160(define-opcode isub 100 1 -1 nil)
161(define-opcode lsub 101 1 -2 nil)
162(define-opcode fsub 102 1 -1 nil)
163(define-opcode dsub 103 1 -2 nil)
164(define-opcode imul 104 1 -1 nil)
165(define-opcode lmul 105 1 -2 nil)
166(define-opcode fmul 106 1 -1 nil)
167(define-opcode dmul 107 1 -2 nil)
168(define-opcode idiv 108 1 nil nil)
169(define-opcode ldiv 109 1 nil nil)
170(define-opcode fdiv 110 1 nil nil)
171(define-opcode ddiv 111 1 nil nil)
172(define-opcode irem 112 1 nil nil)
173(define-opcode lrem 113 1 nil nil)
174(define-opcode frem 114 1 nil nil)
175(define-opcode drem 115 1 nil nil)
176(define-opcode ineg 116 1 0 nil)
177(define-opcode lneg 117 1 0 nil)
178(define-opcode fneg 118 1 0 nil)
179(define-opcode dneg 119 1 0 nil)
180(define-opcode ishl 120 1 -1 nil)
181(define-opcode lshl 121 1 -1 nil)
182(define-opcode ishr 122 1 -1 nil)
183(define-opcode lshr 123 1 -1 nil)
184(define-opcode iushr 124 1 nil nil)
185(define-opcode lushr 125 1 nil nil)
186(define-opcode iand 126 1 -1 nil)
187(define-opcode land 127 1 -2 nil)
188(define-opcode ior 128 1 -1 nil)
189(define-opcode lor 129 1 -2 nil)
190(define-opcode ixor 130 1 -1 nil)
191(define-opcode lxor 131 1 -2 nil)
192(define-opcode iinc 132 3 0 t)
193(define-opcode i2l 133 1 1 nil)
194(define-opcode i2f 134 1 0 nil)
195(define-opcode i2d 135 1 1 nil)
196(define-opcode l2i 136 1 -1 nil)
197(define-opcode l2f 137 1 -1 nil)
198(define-opcode l2d 138 1 0 nil)
199(define-opcode f2i 139 1 nil nil)
200(define-opcode f2l 140 1 nil nil)
201(define-opcode f2d 141 1 1 nil)
202(define-opcode d2i 142 1 nil nil)
203(define-opcode d2l 143 1 nil nil)
204(define-opcode d2f 144 1 -1 nil)
205(define-opcode i2b 145 1 nil nil)
206(define-opcode i2c 146 1 nil nil)
207(define-opcode i2s 147 1 nil nil)
208(define-opcode lcmp 148 1 -3 nil)
209(define-opcode fcmpl 149 1 -1 nil)
210(define-opcode fcmpg 150 1 -1 nil)
211(define-opcode dcmpl 151 1 -3 nil)
212(define-opcode dcmpg 152 1 -3 nil)
213(define-opcode ifeq 153 3 -1 nil)
214(define-opcode ifne 154 3 -1 nil)
215(define-opcode iflt 155 3 -1 nil)
216(define-opcode ifge 156 3 -1 nil)
217(define-opcode ifgt 157 3 -1 nil)
218(define-opcode ifle 158 3 -1 nil)
219(define-opcode if_icmpeq 159 3 -2 nil)
220(define-opcode if_icmpne 160 3 -2 nil)
221(define-opcode if_icmplt 161 3 -2 nil)
222(define-opcode if_icmpge 162 3 -2 nil)
223(define-opcode if_icmpgt 163 3 -2 nil)
224(define-opcode if_icmple 164 3 -2 nil)
225(define-opcode if_acmpeq 165 3 -2 nil)
226(define-opcode if_acmpne 166 3 -2 nil)
227(define-opcode goto 167 3 0 nil)
228;;(define-opcode jsr 168 3 1) Don't use these 2 opcodes: deprecated
229;;(define-opcode ret 169 2 0) their use results in JVM verifier errors
230(define-opcode tableswitch 170 0 nil nil)
231(define-opcode lookupswitch 171 0 nil nil)
232(define-opcode ireturn 172 1 nil nil)
233(define-opcode lreturn 173 1 nil nil)
234(define-opcode freturn 174 1 nil nil)
235(define-opcode dreturn 175 1 nil nil)
236(define-opcode areturn 176 1 -1 nil)
237(define-opcode return 177 1 0 nil)
238(define-opcode getstatic 178 3 1 nil)
239(define-opcode putstatic 179 3 -1 nil)
240(define-opcode getfield 180 3 0 nil)
241(define-opcode putfield 181 3 -2 nil)
242(define-opcode invokevirtual 182 3 nil nil)
243(define-opcode invokespecial 183 3 nil nil)
244(define-opcode invokestatic 184 3 nil nil)
245(define-opcode invokeinterface 185 5 nil nil)
246(define-opcode unused 186 0 nil nil)
247(define-opcode new 187 3 1 nil)
248(define-opcode newarray 188 2 nil nil)
249(define-opcode anewarray 189 3 0 nil)
250(define-opcode arraylength 190 1 0 nil)
251(define-opcode athrow 191 1 0 nil)
252(define-opcode checkcast 192 3 0 nil)
253(define-opcode instanceof 193 3 0 nil)
254(define-opcode monitorenter 194 1 -1 nil)
255(define-opcode monitorexit 195 1 -1 nil)
256(define-opcode wide 196 0 nil nil)
257(define-opcode multianewarray 197 4 nil nil)
258(define-opcode ifnull 198 3 -1 nil)
259(define-opcode ifnonnull 199 3 nil nil)
260(define-opcode goto_w 200 5 nil nil)
261;; (define-opcode jsr_w 201 5 nil) Don't use: deprecated
262(define-opcode label 202 0 0 nil)  ;; virtual: does not exist in the JVM
263;; (define-opcode push-value 203 nil 1)
264;; (define-opcode store-value 204 nil -1)
265(define-opcode clear-values 205 0 0 t)  ;; virtual: does not exist in the JVM
266;;(define-opcode var-ref 206 0 0)
267
268(defparameter *last-opcode* 206)
269
270(declaim (ftype (function (t) t) opcode-name))
271(defun opcode-name (opcode-number)
272  (let ((opcode (gethash opcode-number *opcodes*)))
273    (and opcode (jvm-opcode-name opcode))))
274
275(declaim (ftype (function (t) (integer 0 255)) opcode-number))
276(defun opcode-number (opcode-name)
277  (declare (optimize speed))
278  (let ((opcode (gethash (string opcode-name) *opcodes*)))
279    (if opcode
280        (jvm-opcode-number opcode)
281        (error "Unknown opcode ~S." opcode-name))))
282
283(declaim (ftype (function (t) fixnum) opcode-size))
284(defun opcode-size (opcode-number)
285  (declare (optimize speed (safety 0)))
286  (declare (type (integer 0 255) opcode-number))
287  (jvm-opcode-size (svref *opcode-table* opcode-number)))
288
289(declaim (ftype (function (t) t) opcode-stack-effect))
290(defun opcode-stack-effect (opcode-number)
291  (declare (optimize speed))
292  (jvm-opcode-stack-effect (svref *opcode-table* opcode-number)))
293
294
295
296
297;;   INSTRUCTION
298
299(defstruct (instruction (:constructor %make-instruction (opcode args)))
300  (opcode 0 :type (integer 0 255))
301  args
302  stack
303  depth
304  wide)
305
306(defun make-instruction (opcode args)
307  (let ((inst (apply #'%make-instruction
308                     (list opcode
309                           (remove :wide-prefix args)))))
310    (when (memq :wide-prefix args)
311      (setf (inst-wide inst) t))
312    inst))
313
314(defun print-instruction (instruction)
315  (sys::%format nil "~A ~A stack = ~S depth = ~S"
316          (opcode-name (instruction-opcode instruction))
317          (instruction-args instruction)
318          (instruction-stack instruction)
319          (instruction-depth instruction)))
320
321(declaim (ftype (function (t) t) instruction-label))
322(defun instruction-label (instruction)
323  (and instruction
324       (= (instruction-opcode (the instruction instruction)) 202)
325       (car (instruction-args instruction))))
326
327
328
329(defknown inst * t)
330(defun inst (instr &optional args)
331  (declare (optimize speed))
332  (let ((opcode (if (fixnump instr)
333                    instr
334                    (opcode-number instr))))
335    (unless (listp args)
336      (setf args (list args)))
337    (make-instruction opcode args)))
338
339
340;; Having %emit and %%emit output their code to *code*
341;; is currently an implementation detail exposed to all users.
342;; We need to have APIs to address this, but for now pass2 is
343;; our only user and we'll hard-code the use of *code*.
344(defvar *code* nil)
345
346(defknown %%emit * t)
347(defun %%emit (instr &rest args)
348  (declare (optimize speed))
349  (let ((instruction (make-instruction instr args)))
350    (push instruction *code*)
351    instruction))
352
353(defknown %emit * t)
354(defun %emit (instr &rest args)
355  (declare (optimize speed))
356  (let ((instruction (inst instr args)))
357    (push instruction *code*)
358    instruction))
359
360(defmacro emit (instr &rest args)
361  (when (and (consp instr)
362             (eq (car instr) 'QUOTE)
363             (symbolp (cadr instr)))
364    (setf instr (opcode-number (cadr instr))))
365  (if (fixnump instr)
366      `(%%emit ,instr ,@args)
367      `(%emit ,instr ,@args)))
368
369
370;;  Helper routines
371
372(defknown label (symbol) t)
373(defun label (symbol)
374  (declare (type symbol symbol))
375  (declare (optimize speed))
376  (emit 'label symbol)
377  (setf (symbol-value symbol) nil))
378
379(defknown aload (fixnum) t)
380(defun aload (index)
381  (case index
382    (0 (emit 'aload_0))
383    (1 (emit 'aload_1))
384    (2 (emit 'aload_2))
385    (3 (emit 'aload_3))
386    (t (emit 'aload index))))
387
388(defknown astore (fixnum) t)
389(defun astore (index)
390  (case index
391    (0 (emit 'astore_0))
392    (1 (emit 'astore_1))
393    (2 (emit 'astore_2))
394    (3 (emit 'astore_3))
395    (t (emit 'astore index))))
396
397(declaim (ftype (function (t) t) branch-p)
398         (inline branch-p))
399(defun branch-p (opcode)
400;;  (declare (optimize speed))
401;;  (declare (type '(integer 0 255) opcode))
402  (or (<= 153 opcode 167)
403      (<= 198 opcode 200))) ;; ifnull / ifnonnull / goto_w
404
405(declaim (ftype (function (t) t) unconditional-control-transfer-p)
406         (inline unconditional-control-transfer-p))
407(defun unconditional-control-transfer-p (opcode)
408  (or (= 167 opcode) ;; goto
409      (= 200 opcode) ;; goto_w
410      (<= 172 opcode 177) ;; ?return
411      (= 191 opcode) ;; athrow
412      ))
413
414(declaim (ftype (function (t) boolean) label-p)
415         (inline label-p))
416(defun label-p (instruction)
417  (and instruction
418       (= (the fixnum (instruction-opcode (the instruction instruction))) 202)))
419
420(defun print-code (code)
421  (dotimes (i (length code))
422    (let ((instruction (elt code i)))
423      (sys::%format t "~D ~A ~S ~S ~S~%"
424                    i
425                    (opcode-name (instruction-opcode instruction))
426                    (instruction-args instruction)
427                    (instruction-stack instruction)
428                    (instruction-depth instruction)))))
429
430(defun print-code2 (code)
431  (dotimes (i (length code))
432    (let ((instruction (elt code i)))
433      (case (instruction-opcode instruction)
434        (202 ; LABEL
435         (format t "~A:~%" (car (instruction-args instruction))))
436        (t
437         (format t "~8D:   ~A ~S~%"
438                 i
439                 (opcode-name (instruction-opcode instruction))
440                 (instruction-args instruction)))))))
441
442(defun expand-virtual-instructions (code)
443  (let* ((len (length code))
444         (vector (make-array (ash len 1) :fill-pointer 0 :adjustable t)))
445    (dotimes (index len vector)
446      (declare (type (unsigned-byte 16) index))
447      (let ((instruction (svref code index)))
448        (case (instruction-opcode instruction)
449          (205 ; CLEAR-VALUES
450           (dolist (instruction
451                     (list
452                      (inst 'aload (car (instruction-args instruction)))
453                      (inst 'aconst_null)
454                      (inst 'putfield (u2 (pool-field +lisp-thread+ "_values"
455                                                      +lisp-object-array+)))))
456             (vector-push-extend instruction vector)))
457          (t
458           (vector-push-extend instruction vector)))))))
459
460
461;;   RESOLVERS
462
463(defun unsupported-opcode (instruction)
464  (error "Unsupported opcode ~D." (instruction-opcode instruction)))
465
466(declaim (type hash-table +resolvers+))
467(defconst +resolvers+ (make-hash-table))
468
469(defun initialize-resolvers ()
470  (let ((ht +resolvers+))
471    (dotimes (n (1+ *last-opcode*))
472      (setf (gethash n ht) #'unsupported-opcode))
473    ;; The following opcodes resolve to themselves.
474    (dolist (n '(0 ; nop
475                 1 ; aconst_null
476                 2 ; iconst_m1
477                 3 ; iconst_0
478                 4 ; iconst_1
479                 5 ; iconst_2
480                 6 ; iconst_3
481                 7 ; iconst_4
482                 8 ; iconst_5
483                 9 ; lconst_0
484                 10 ; lconst_1
485                 11 ; fconst_0
486                 12 ; fconst_1
487                 13 ; fconst_2
488                 14 ; dconst_0
489                 15 ; dconst_1
490                 42 ; aload_0
491                 43 ; aload_1
492                 44 ; aload_2
493                 45 ; aload_3
494                 46 ; iaload
495                 47 ; laload
496                 48 ; faload
497                 49 ; daload
498                 50 ; aaload
499                 75 ; astore_0
500                 76 ; astore_1
501                 77 ; astore_2
502                 78 ; astore_3
503                 79 ; iastore
504                 80 ; lastore
505                 81 ; fastore
506                 82 ; dastore
507                 83 ; aastore
508                 87 ; pop
509                 88 ; pop2
510                 89 ; dup
511                 90 ; dup_x1
512                 91 ; dup_x2
513                 92 ; dup2
514                 93 ; dup2_x1
515                 94 ; dup2_x2
516                 95 ; swap
517                 96 ; iadd
518                 97 ; ladd
519                 98 ; fadd
520                 99 ; dadd
521                 100 ; isub
522                 101 ; lsub
523                 102 ; fsub
524                 103 ; dsub
525                 104 ; imul
526                 105 ; lmul
527                 106 ; fmul
528                 107 ; dmul
529                 116 ; ineg
530                 117 ; lneg
531                 118 ; fneg
532                 119 ; dneg
533                 120 ; ishl
534                 121 ; lshl
535                 122 ; ishr
536                 123 ; lshr
537                 126 ; iand
538                 127 ; land
539                 128 ; ior
540                 129 ; lor
541                 130 ; ixor
542                 131 ; lxor
543                 133 ; i2l
544                 134 ; i2f
545                 135 ; i2d
546                 136 ; l2i
547                 137 ; l2f
548                 138 ; l2d
549                 141 ; f2d
550                 144 ; d2f
551                 148 ; lcmp
552                 149 ; fcmpd
553                 150 ; fcmpg
554                 151 ; dcmpd
555                 152 ; dcmpg
556                 153 ; ifeq
557                 154 ; ifne
558                 155 ; ifge
559                 156 ; ifgt
560                 157 ; ifgt
561                 158 ; ifle
562                 159 ; if_icmpeq
563                 160 ; if_icmpne
564                 161 ; if_icmplt
565                 162 ; if_icmpge
566                 163 ; if_icmpgt
567                 164 ; if_icmple
568                 165 ; if_acmpeq
569                 166 ; if_acmpne
570                 167 ; goto
571                 176 ; areturn
572                 177 ; return
573                 178 ; getstatic
574                 179 ; putstatic
575                 180 ; getfield
576                 181 ; putfield
577                 182 ; invokevirtual
578                 183 ; invockespecial
579                 184 ; invokestatic
580                 187 ; new
581                 189 ; anewarray
582                 190 ; arraylength
583                 191 ; athrow
584                 192 ; checkcast
585                 193 ; instanceof
586                 194 ; monitorenter
587                 195 ; monitorexit
588                 198 ; ifnull
589                 202 ; label
590                 ))
591      (setf (gethash n ht) nil))))
592
593(initialize-resolvers)
594
595(defmacro define-resolver (opcodes args &body body)
596  (let ((name (gensym)))
597    `(progn
598       (defun ,name ,args ,@body)
599       (eval-when (:load-toplevel :execute)
600         ,(if (listp opcodes)
601              `(dolist (op ',opcodes)
602                 (setf (gethash op +resolvers+)
603                       (symbol-function ',name)))
604              `(setf (gethash ,opcodes +resolvers+)
605                     (symbol-function ',name)))))))
606
607(defun load/store-resolver (instruction inst-index inst-index2 error-text)
608 (let* ((args (instruction-args instruction))
609        (index (car args)))
610   (declare (type (unsigned-byte 16) index))
611   (cond ((<= 0 index 3)
612          (inst (+ index inst-index)))
613         ((<= 0 index 255)
614          (inst inst-index2 index))
615         (t
616          (error error-text)))))
617
618;; aload
619(define-resolver 25 (instruction)
620  (load/store-resolver instruction 42 25 "ALOAD unsupported case"))
621
622;; astore
623(define-resolver 58 (instruction)
624  (load/store-resolver instruction 75 58 "ASTORE unsupported case"))
625
626;; iload
627(define-resolver 21 (instruction)
628  (load/store-resolver instruction 26 21 "ILOAD unsupported case"))
629
630;; istore
631(define-resolver 54 (instruction)
632  (load/store-resolver instruction 59 54 "ISTORE unsupported case"))
633
634;; lload
635(define-resolver 22 (instruction)
636  (load/store-resolver instruction 30 22 "LLOAD unsupported case"))
637
638;; lstore
639(define-resolver 55 (instruction)
640  (load/store-resolver instruction 63 55 "LSTORE unsupported case"))
641
642;; bipush, sipush
643(define-resolver (16 17) (instruction)
644  (let* ((args (instruction-args instruction))
645         (n (first args)))
646    (declare (type fixnum n))
647    (cond ((<= 0 n 5)
648           (inst (+ n 3)))
649          ((<= -128 n 127)
650           (inst 16 (logand n #xff))) ; BIPUSH
651          (t ; SIPUSH
652           (inst 17 (s2 n))))))
653
654;; ldc
655(define-resolver 18 (instruction)
656  (let* ((args (instruction-args instruction)))
657    (unless (= (length args) 1)
658      (error "Wrong number of args for LDC."))
659    (if (> (car args) 255)
660        (inst 19 (u2 (car args))) ; LDC_W
661        (inst 18 args))))
662
663;; ldc2_w
664(define-resolver 20 (instruction)
665  (let* ((args (instruction-args instruction)))
666    (unless (= (length args) 1)
667      (error "Wrong number of args for LDC2_W."))
668    (inst 20 (u2 (car args)))))
669
670;; iinc
671(define-resolver 132 (instruction)
672  (let* ((args (instruction-args instruction))
673         (register (first args))
674         (n (second args)))
675    (when (not (<= -128 n 127))
676      (error "IINC argument ~A out of bounds." n))
677    (inst 132 (list register (s1 n)))))
678
679(defknown resolve-instruction (t) t)
680(defun resolve-instruction (instruction)
681  (declare (optimize speed))
682  (let ((resolver (gethash1 (instruction-opcode instruction) +resolvers+)))
683    (if resolver
684        (funcall resolver instruction)
685        instruction)))
686
687(defun resolve-instructions (code)
688  (let* ((len (length code))
689         (vector (make-array len :fill-pointer 0 :adjustable t)))
690    (dotimes (index len vector)
691      (declare (type (unsigned-byte 16) index))
692      (let ((instruction (aref code index)))
693        (vector-push-extend (resolve-instruction instruction) vector)))))
694
695
696
697;; BYTE CODE ANALYSIS AND OPTIMIZATION
698
699(declaim (ftype (function (t t t) t) analyze-stack-path))
700(defun analyze-stack-path (code start-index depth)
701  (declare (optimize speed))
702  (declare (type fixnum start-index depth))
703  (do* ((i start-index (1+ i))
704        (limit (length code)))
705       ((>= i limit))
706    (declare (type fixnum i limit))
707    (let* ((instruction (aref code i))
708           (instruction-depth (instruction-depth instruction))
709           (instruction-stack (instruction-stack instruction)))
710      (declare (type fixnum instruction-stack))
711      (when instruction-depth
712        (unless (= (the fixnum instruction-depth)
713                   (the fixnum (+ depth instruction-stack)))
714          (internal-compiler-error "Stack inconsistency detected ~
715                                    in ~A at index ~D: ~
716                                    found ~S, expected ~S."
717                                   (compiland-name *current-compiland*)
718                                   i instruction-depth
719                                   (+ depth instruction-stack)))
720        (return-from analyze-stack-path))
721      (let ((opcode (instruction-opcode instruction)))
722        (setf depth (+ depth instruction-stack))
723        (setf (instruction-depth instruction) depth)
724        (unless (<= 0 depth)
725          (internal-compiler-error "Stack inconsistency detected ~
726                                    in ~A at index ~D: ~
727                                    negative depth ~S."
728                                   (compiland-name *current-compiland*)
729                                   i depth))
730        (when (branch-p opcode)
731          (let ((label (car (instruction-args instruction))))
732            (declare (type symbol label))
733            (analyze-stack-path code (symbol-value label) depth)))
734        (when (unconditional-control-transfer-p opcode)
735          ;; Current path ends.
736          (return-from analyze-stack-path))))))
737
738(declaim (ftype (function (t) t) analyze-stack))
739(defun analyze-stack (code exception-entry-points)
740  (declare (optimize speed))
741  (let* ((code-length (length code)))
742    (declare (type vector code))
743    (dotimes (i code-length)
744      (let* ((instruction (aref code i))
745             (opcode (instruction-opcode instruction)))
746        (when (eql opcode 202) ; LABEL
747          (let ((label (car (instruction-args instruction))))
748            (set label i)))
749        (if (instruction-stack instruction)
750            (when (opcode-stack-effect opcode)
751              (unless (eql (instruction-stack instruction)
752                           (opcode-stack-effect opcode))
753                (sys::%format t "instruction-stack = ~S ~
754                                 opcode-stack-effect = ~S~%"
755                              (instruction-stack instruction)
756                              (opcode-stack-effect opcode))
757                (sys::%format t "index = ~D instruction = ~A~%" i
758                              (print-instruction instruction))))
759            (setf (instruction-stack instruction)
760                  (opcode-stack-effect opcode)))
761        (unless (instruction-stack instruction)
762          (sys::%format t "no stack information for instruction ~D~%"
763                        (instruction-opcode instruction))
764          (aver nil))))
765    (analyze-stack-path code 0 0)
766    (dolist (entry-point exception-entry-points)
767      ;; Stack depth is always 1 when handler is called.
768      (analyze-stack-path code (symbol-value entry-point) 1))
769    (let ((max-stack 0))
770      (declare (type fixnum max-stack))
771      (dotimes (i code-length)
772        (let* ((instruction (aref code i))
773               (instruction-depth (instruction-depth instruction)))
774          (when instruction-depth
775            (setf max-stack (max max-stack (the fixnum instruction-depth))))))
776      max-stack)))
777
778(defun analyze-locals (code)
779  (let ((code-length (length code))
780        (max-local 0))
781    (dotimes (i code-length max-local)
782      (let* ((instruction (aref code i))
783             (opcode (instruction-opcode instruction)))
784        (setf max-local
785              (max max-local
786                   (or (let ((opcode-register
787                                (jvm-opcode-register-used opcode)))
788                         (if (eq t opcode-register)
789                             (car (instruction-args instruction))
790                             opcode-register))
791                       0)))))))
792
793(defun delete-unused-labels (code handler-labels)
794  (declare (optimize speed))
795  (let ((code (coerce code 'vector))
796        (changed nil)
797        (marker (gensym)))
798    ;; Mark the labels that are actually branched to.
799    (dotimes (i (length code))
800      (let ((instruction (aref code i)))
801        (when (branch-p (instruction-opcode instruction))
802          (let ((label (car (instruction-args instruction))))
803            (set label marker)))))
804    ;; Add labels used for exception handlers.
805    (dolist (label handler-labels)
806      (set label marker))
807    ;; Remove labels that are not used as branch targets.
808    (dotimes (i (length code))
809      (let ((instruction (aref code i)))
810        (when (= (instruction-opcode instruction) 202) ; LABEL
811          (let ((label (car (instruction-args instruction))))
812            (declare (type symbol label))
813            (unless (eq (symbol-value label) marker)
814              (setf (aref code i) nil)
815              (setf changed t))))))
816    (values (if changed (delete nil code) code)
817            changed)))
818
819(defun delete-unreachable-code (code)
820  ;; Look for unreachable code after GOTO.
821  (declare (optimize speed))
822  (let* ((code (coerce code 'vector))
823         (changed nil)
824         (after-goto/areturn nil))
825    (dotimes (i (length code))
826      (declare (type (unsigned-byte 16) i))
827      (let* ((instruction (aref code i))
828             (opcode (instruction-opcode instruction)))
829        (cond (after-goto/areturn
830               (if (= opcode 202) ; LABEL
831                   (setf after-goto/areturn nil)
832                   ;; Unreachable.
833                   (progn
834                     (setf (aref code i) nil)
835                     (setf changed t))))
836              ((unconditional-control-transfer-p opcode)
837               (setf after-goto/areturn t)))))
838    (values (if changed (delete nil code) code)
839            changed)))
840
841
842(declaim (ftype (function (t) label-target-instructions) hash-labels))
843(defun label-target-instructions (code)
844  (let ((ht (make-hash-table :test 'eq))
845        (code (coerce code 'vector))
846        (pending-labels '()))
847    (dotimes (i (length code))
848      (let ((instruction (aref code i)))
849        (cond ((label-p instruction)
850               (push (instruction-label instruction) pending-labels))
851              (t
852               ;; Not a label.
853               (when pending-labels
854                 (dolist (label pending-labels)
855                   (setf (gethash label ht) instruction))
856                 (setf pending-labels nil))))))
857    ht))
858
859(defun optimize-jumps (code)
860  (declare (optimize speed))
861  (let* ((code (coerce code 'vector))
862         (ht (label-target-instructions code))
863         (changed nil))
864    (dotimes (i (length code))
865      (let* ((instruction (aref code i))
866             (opcode (and instruction (instruction-opcode instruction))))
867        (when (and opcode (branch-p opcode))
868          (let* ((target-label (car (instruction-args instruction)))
869                 (next-instruction (gethash1 target-label ht)))
870            (when next-instruction
871              (case (instruction-opcode next-instruction)
872                ((167 200)                  ;; GOTO
873                 (setf (instruction-args instruction)
874                       (instruction-args next-instruction)
875                       changed t))
876                (176 ; ARETURN
877                 (when (unconditional-control-transfer-p opcode)
878                   (setf (instruction-opcode instruction) 176
879                         (instruction-args instruction) nil
880                         changed t)))))))))
881    (values code changed)))
882
883
884(defun optimize-instruction-sequences (code)
885  (let* ((code (coerce code 'vector))
886         (changed nil))
887    (dotimes (i (1- (length code)))
888      (let* ((this-instruction (aref code i))
889             (this-opcode (and this-instruction
890                               (instruction-opcode this-instruction)))
891             (labels-skipped-p nil)
892             (next-instruction (do ((j (1+ i) (1+ j)))
893                                   ((or (>= j (length code))
894                                        (/= 202 ; LABEL
895                                            (instruction-opcode (aref code j))))
896                                    (when (< j (length code))
897                                      (aref code j)))
898                                 (setf labels-skipped-p t)))
899             (next-opcode (and next-instruction
900                               (instruction-opcode next-instruction))))
901        (case this-opcode
902          (205 ; CLEAR-VALUES
903           (when (eql next-opcode 205)       ; CLEAR-VALUES
904             (setf (aref code i) nil)
905             (setf changed t)))
906          (178 ; GETSTATIC
907           (when (and (eql next-opcode 87)   ; POP
908                      (not labels-skipped-p))
909             (setf (aref code i) nil)
910             (setf (aref code (1+ i)) nil)
911             (setf changed t)))
912          (176 ; ARETURN
913           (when (eql next-opcode 176)       ; ARETURN
914             (setf (aref code i) nil)
915             (setf changed t)))
916          ((200 167)                         ; GOTO GOTO_W
917           (when (and (or (eql next-opcode 202)  ; LABEL
918                          (eql next-opcode 200)  ; GOTO_W
919                          (eql next-opcode 167)) ; GOTO
920                      (eq (car (instruction-args this-instruction))
921                          (car (instruction-args next-instruction))))
922             (setf (aref code i) nil)
923             (setf changed t))))))
924    (values (if changed (delete nil code) code)
925            changed)))
926
927(defvar *enable-optimization* t)
928
929(defknown optimize-code (t t) t)
930(defun optimize-code (code handler-labels)
931  (unless *enable-optimization*
932    (format t "optimizations are disabled~%"))
933  (when *enable-optimization*
934    (when *compiler-debug*
935      (format t "----- before optimization -----~%")
936      (print-code code))
937    (loop
938       (let ((changed-p nil))
939         (multiple-value-setq
940             (code changed-p)
941           (delete-unused-labels code handler-labels))
942         (if changed-p
943             (setf code (optimize-instruction-sequences code))
944             (multiple-value-setq
945                 (code changed-p)
946               (optimize-instruction-sequences code)))
947         (if changed-p
948             (setf code (optimize-jumps code))
949             (multiple-value-setq
950                 (code changed-p)
951               (optimize-jumps code)))
952         (if changed-p
953             (setf code (delete-unreachable-code code))
954             (multiple-value-setq
955                 (code changed-p)
956               (delete-unreachable-code code)))
957         (unless changed-p
958           (return))))
959    (unless (vectorp code)
960      (setf code (coerce code 'vector)))
961    (when *compiler-debug*
962      (sys::%format t "----- after optimization -----~%")
963      (print-code code)))
964  code)
965
966
967
968
969(defun code-bytes (code)
970  (let ((length 0)
971        labels ;; alist
972        )
973    (declare (type (unsigned-byte 16) length))
974    ;; Pass 1: calculate label offsets and overall length.
975    (dotimes (i (length code))
976      (declare (type (unsigned-byte 16) i))
977      (let* ((instruction (aref code i))
978             (opcode (instruction-opcode instruction)))
979        (if (= opcode 202) ; LABEL
980            (let ((label (car (instruction-args instruction))))
981              (set label length)
982              (setf labels
983                    (acons label length labels)))
984            (incf length (opcode-size opcode)))))
985    ;; Pass 2: replace labels with calculated offsets.
986    (let ((index 0))
987      (declare (type (unsigned-byte 16) index))
988      (dotimes (i (length code))
989        (declare (type (unsigned-byte 16) i))
990        (let ((instruction (aref code i)))
991          (when (branch-p (instruction-opcode instruction))
992            (let* ((label (car (instruction-args instruction)))
993                   (offset (- (the (unsigned-byte 16)
994                                (symbol-value (the symbol label)))
995                              index)))
996              (setf (instruction-args instruction) (s2 offset))))
997          (unless (= (instruction-opcode instruction) 202) ; LABEL
998            (incf index (opcode-size (instruction-opcode instruction)))))))
999    ;; Expand instructions into bytes, skipping LABEL pseudo-instructions.
1000    (let ((bytes (make-array length))
1001          (index 0))
1002      (declare (type (unsigned-byte 16) index))
1003      (dotimes (i (length code))
1004        (declare (type (unsigned-byte 16) i))
1005        (let ((instruction (aref code i)))
1006          (unless (= (instruction-opcode instruction) 202) ; LABEL
1007            (setf (svref bytes index) (instruction-opcode instruction))
1008            (incf index)
1009            (dolist (byte (instruction-args instruction))
1010              (setf (svref bytes index) byte)
1011              (incf index)))))
1012      (values bytes labels))))
1013
1014(defun finalize-code (code handler-labels optimize)
1015  (setf code (coerce (nreverse code) 'vector))
1016  (when optimize
1017    (setf code (optimize-code code handler-labels)))
1018  (resolve-instructions (expand-virtual-instructions code)))
1019
1020(provide '#:opcodes)
Note: See TracBrowser for help on using the repository browser.