source: trunk/abcl/src/org/armedbear/lisp/jvm-instructions.lisp @ 13535

Last change on this file since 13535 was 13535, checked in by ehuelsmann, 12 years ago

Moving huge object serialization from <init>() to <clinit>()
broke the code generation for that special case -- there's no longer
a 'this' variable to be loaded. Replace with <this class>.class.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.9 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 13535 2011-08-23 20:35:32Z 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;; ldc_w
664(define-resolver 19 (instruction)
665  (let* ((args (instruction-args instruction)))
666    (unless (= (length args) 1)
667      (error "Wrong number of args for LDC_W."))
668    (inst 19 (u2 (car args)))))
669
670;; ldc2_w
671(define-resolver 20 (instruction)
672  (let* ((args (instruction-args instruction)))
673    (unless (= (length args) 1)
674      (error "Wrong number of args for LDC2_W."))
675    (inst 20 (u2 (car args)))))
676
677;; iinc
678(define-resolver 132 (instruction)
679  (let* ((args (instruction-args instruction))
680         (register (first args))
681         (n (second args)))
682    (when (not (<= -128 n 127))
683      (error "IINC argument ~A out of bounds." n))
684    (inst 132 (list register (s1 n)))))
685
686(defknown resolve-instruction (t) t)
687(defun resolve-instruction (instruction)
688  (declare (optimize speed))
689  (let ((resolver (gethash1 (instruction-opcode instruction) +resolvers+)))
690    (if resolver
691        (funcall resolver instruction)
692        instruction)))
693
694(defun resolve-instructions (code)
695  (let* ((len (length code))
696         (vector (make-array len :fill-pointer 0 :adjustable t)))
697    (dotimes (index len vector)
698      (declare (type (unsigned-byte 16) index))
699      (let ((instruction (aref code index)))
700        (vector-push-extend (resolve-instruction instruction) vector)))))
701
702
703
704;; BYTE CODE ANALYSIS AND OPTIMIZATION
705
706(declaim (ftype (function (t t t) t) analyze-stack-path))
707(defun analyze-stack-path (code start-index depth)
708  (declare (optimize speed))
709  (declare (type fixnum start-index depth))
710  (do* ((i start-index (1+ i))
711        (limit (length code)))
712       ((>= i limit))
713    (declare (type fixnum i limit))
714    (let* ((instruction (aref code i))
715           (instruction-depth (instruction-depth instruction))
716           (instruction-stack (instruction-stack instruction)))
717      (declare (type fixnum instruction-stack))
718      (when instruction-depth
719        (unless (= (the fixnum instruction-depth)
720                   (the fixnum (+ depth instruction-stack)))
721          (internal-compiler-error "Stack inconsistency detected ~
722                                    in ~A at index ~D: ~
723                                    found ~S, expected ~S."
724                                   (compiland-name *current-compiland*)
725                                   i instruction-depth
726                                   (+ depth instruction-stack)))
727        (return-from analyze-stack-path))
728      (let ((opcode (instruction-opcode instruction)))
729        (setf depth (+ depth instruction-stack))
730        (setf (instruction-depth instruction) depth)
731        (unless (<= 0 depth)
732          (internal-compiler-error "Stack inconsistency detected ~
733                                    in ~A at index ~D: ~
734                                    negative depth ~S."
735                                   (compiland-name *current-compiland*)
736                                   i depth))
737        (when (branch-p opcode)
738          (let ((label (car (instruction-args instruction))))
739            (declare (type symbol label))
740            (analyze-stack-path code (symbol-value label) depth)))
741        (when (unconditional-control-transfer-p opcode)
742          ;; Current path ends.
743          (return-from analyze-stack-path))))))
744
745(declaim (ftype (function (t) t) analyze-stack))
746(defun analyze-stack (code exception-entry-points)
747  (declare (optimize speed))
748  (let* ((code-length (length code)))
749    (declare (type vector code))
750    (dotimes (i code-length)
751      (let* ((instruction (aref code i))
752             (opcode (instruction-opcode instruction)))
753        (when (eql opcode 202) ; LABEL
754          (let ((label (car (instruction-args instruction))))
755            (set label i)))
756        (if (instruction-stack instruction)
757            (when (opcode-stack-effect opcode)
758              (unless (eql (instruction-stack instruction)
759                           (opcode-stack-effect opcode))
760                (sys::%format t "instruction-stack = ~S ~
761                                 opcode-stack-effect = ~S~%"
762                              (instruction-stack instruction)
763                              (opcode-stack-effect opcode))
764                (sys::%format t "index = ~D instruction = ~A~%" i
765                              (print-instruction instruction))))
766            (setf (instruction-stack instruction)
767                  (opcode-stack-effect opcode)))
768        (unless (instruction-stack instruction)
769          (sys::%format t "no stack information for instruction ~D~%"
770                        (instruction-opcode instruction))
771          (aver nil))))
772    (analyze-stack-path code 0 0)
773    (dolist (entry-point exception-entry-points)
774      ;; Stack depth is always 1 when handler is called.
775      (analyze-stack-path code (symbol-value entry-point) 1))
776    (let ((max-stack 0))
777      (declare (type fixnum max-stack))
778      (dotimes (i code-length)
779        (let* ((instruction (aref code i))
780               (instruction-depth (instruction-depth instruction)))
781          (when instruction-depth
782            (setf max-stack (max max-stack (the fixnum instruction-depth))))))
783      max-stack)))
784
785(defun analyze-locals (code)
786  (let ((code-length (length code))
787        (max-local 0))
788    (dotimes (i code-length max-local)
789      (let* ((instruction (aref code i))
790             (opcode (instruction-opcode instruction)))
791        (setf max-local
792              (max max-local
793                   (or (let ((opcode-register
794                                (jvm-opcode-register-used opcode)))
795                         (if (eq t opcode-register)
796                             (car (instruction-args instruction))
797                             opcode-register))
798                       0)))))))
799
800(defun delete-unused-labels (code handler-labels)
801  (declare (optimize speed))
802  (let ((code (coerce code 'vector))
803        (changed nil)
804        (marker (gensym)))
805    ;; Mark the labels that are actually branched to.
806    (dotimes (i (length code))
807      (let ((instruction (aref code i)))
808        (when (branch-p (instruction-opcode instruction))
809          (let ((label (car (instruction-args instruction))))
810            (set label marker)))))
811    ;; Add labels used for exception handlers.
812    (dolist (label handler-labels)
813      (set label marker))
814    ;; Remove labels that are not used as branch targets.
815    (dotimes (i (length code))
816      (let ((instruction (aref code i)))
817        (when (= (instruction-opcode instruction) 202) ; LABEL
818          (let ((label (car (instruction-args instruction))))
819            (declare (type symbol label))
820            (unless (eq (symbol-value label) marker)
821              (setf (aref code i) nil)
822              (setf changed t))))))
823    (values (if changed (delete nil code) code)
824            changed)))
825
826(defun delete-unreachable-code (code)
827  ;; Look for unreachable code after GOTO.
828  (declare (optimize speed))
829  (let* ((code (coerce code 'vector))
830         (changed nil)
831         (after-goto/areturn nil))
832    (dotimes (i (length code))
833      (declare (type (unsigned-byte 16) i))
834      (let* ((instruction (aref code i))
835             (opcode (instruction-opcode instruction)))
836        (cond (after-goto/areturn
837               (if (= opcode 202) ; LABEL
838                   (setf after-goto/areturn nil)
839                   ;; Unreachable.
840                   (progn
841                     (setf (aref code i) nil)
842                     (setf changed t))))
843              ((unconditional-control-transfer-p opcode)
844               (setf after-goto/areturn t)))))
845    (values (if changed (delete nil code) code)
846            changed)))
847
848
849(declaim (ftype (function (t) label-target-instructions) hash-labels))
850(defun label-target-instructions (code)
851  (let ((ht (make-hash-table :test 'eq))
852        (code (coerce code 'vector))
853        (pending-labels '()))
854    (dotimes (i (length code))
855      (let ((instruction (aref code i)))
856        (cond ((label-p instruction)
857               (push (instruction-label instruction) pending-labels))
858              (t
859               ;; Not a label.
860               (when pending-labels
861                 (dolist (label pending-labels)
862                   (setf (gethash label ht) instruction))
863                 (setf pending-labels nil))))))
864    ht))
865
866(defun optimize-jumps (code)
867  (declare (optimize speed))
868  (let* ((code (coerce code 'vector))
869         (ht (label-target-instructions code))
870         (changed nil))
871    (dotimes (i (length code))
872      (let* ((instruction (aref code i))
873             (opcode (and instruction (instruction-opcode instruction))))
874        (when (and opcode (branch-p opcode))
875          (let* ((target-label (car (instruction-args instruction)))
876                 (next-instruction (gethash1 target-label ht)))
877            (when next-instruction
878              (case (instruction-opcode next-instruction)
879                ((167 200)                  ;; GOTO
880                 (setf (instruction-args instruction)
881                       (instruction-args next-instruction)
882                       changed t))
883                (176 ; ARETURN
884                 (when (unconditional-control-transfer-p opcode)
885                   (setf (instruction-opcode instruction) 176
886                         (instruction-args instruction) nil
887                         changed t)))))))))
888    (values code changed)))
889
890
891(defun optimize-instruction-sequences (code)
892  (let* ((code (coerce code 'vector))
893         (changed nil))
894    (dotimes (i (1- (length code)))
895      (let* ((this-instruction (aref code i))
896             (this-opcode (and this-instruction
897                               (instruction-opcode this-instruction)))
898             (labels-skipped-p nil)
899             (next-instruction (do ((j (1+ i) (1+ j)))
900                                   ((or (>= j (length code))
901                                        (/= 202 ; LABEL
902                                            (instruction-opcode (aref code j))))
903                                    (when (< j (length code))
904                                      (aref code j)))
905                                 (setf labels-skipped-p t)))
906             (next-opcode (and next-instruction
907                               (instruction-opcode next-instruction))))
908        (case this-opcode
909          (205 ; CLEAR-VALUES
910           (when (eql next-opcode 205)       ; CLEAR-VALUES
911             (setf (aref code i) nil)
912             (setf changed t)))
913          (178 ; GETSTATIC
914           (when (and (eql next-opcode 87)   ; POP
915                      (not labels-skipped-p))
916             (setf (aref code i) nil)
917             (setf (aref code (1+ i)) nil)
918             (setf changed t)))
919          (176 ; ARETURN
920           (when (eql next-opcode 176)       ; ARETURN
921             (setf (aref code i) nil)
922             (setf changed t)))
923          ((200 167)                         ; GOTO GOTO_W
924           (when (and (or (eql next-opcode 202)  ; LABEL
925                          (eql next-opcode 200)  ; GOTO_W
926                          (eql next-opcode 167)) ; GOTO
927                      (eq (car (instruction-args this-instruction))
928                          (car (instruction-args next-instruction))))
929             (setf (aref code i) nil)
930             (setf changed t))))))
931    (values (if changed (delete nil code) code)
932            changed)))
933
934(defvar *enable-optimization* t)
935
936(defknown optimize-code (t t) t)
937(defun optimize-code (code handler-labels)
938  (unless *enable-optimization*
939    (format t "optimizations are disabled~%"))
940  (when *enable-optimization*
941    (when *compiler-debug*
942      (format t "----- before optimization -----~%")
943      (print-code code))
944    (loop
945       (let ((changed-p nil))
946         (multiple-value-setq
947             (code changed-p)
948           (delete-unused-labels code handler-labels))
949         (if changed-p
950             (setf code (optimize-instruction-sequences code))
951             (multiple-value-setq
952                 (code changed-p)
953               (optimize-instruction-sequences code)))
954         (if changed-p
955             (setf code (optimize-jumps code))
956             (multiple-value-setq
957                 (code changed-p)
958               (optimize-jumps code)))
959         (if changed-p
960             (setf code (delete-unreachable-code code))
961             (multiple-value-setq
962                 (code changed-p)
963               (delete-unreachable-code code)))
964         (unless changed-p
965           (return))))
966    (unless (vectorp code)
967      (setf code (coerce code 'vector)))
968    (when *compiler-debug*
969      (sys::%format t "----- after optimization -----~%")
970      (print-code code)))
971  code)
972
973
974
975
976(defun code-bytes (code)
977  (let ((length 0)
978        labels ;; alist
979        )
980    (declare (type (unsigned-byte 16) length))
981    ;; Pass 1: calculate label offsets and overall length.
982    (dotimes (i (length code))
983      (declare (type (unsigned-byte 16) i))
984      (let* ((instruction (aref code i))
985             (opcode (instruction-opcode instruction)))
986        (if (= opcode 202) ; LABEL
987            (let ((label (car (instruction-args instruction))))
988              (set label length)
989              (setf labels
990                    (acons label length labels)))
991            (incf length (opcode-size opcode)))))
992    ;; Pass 2: replace labels with calculated offsets.
993    (let ((index 0))
994      (declare (type (unsigned-byte 16) index))
995      (dotimes (i (length code))
996        (declare (type (unsigned-byte 16) i))
997        (let ((instruction (aref code i)))
998          (when (branch-p (instruction-opcode instruction))
999            (let* ((label (car (instruction-args instruction)))
1000                   (offset (- (the (unsigned-byte 16)
1001                                (symbol-value (the symbol label)))
1002                              index)))
1003              (setf (instruction-args instruction) (s2 offset))))
1004          (unless (= (instruction-opcode instruction) 202) ; LABEL
1005            (incf index (opcode-size (instruction-opcode instruction)))))))
1006    ;; Expand instructions into bytes, skipping LABEL pseudo-instructions.
1007    (let ((bytes (make-array length))
1008          (index 0))
1009      (declare (type (unsigned-byte 16) index))
1010      (dotimes (i (length code))
1011        (declare (type (unsigned-byte 16) i))
1012        (let ((instruction (aref code i)))
1013          (unless (= (instruction-opcode instruction) 202) ; LABEL
1014            (setf (svref bytes index) (instruction-opcode instruction))
1015            (incf index)
1016            (dolist (byte (instruction-args instruction))
1017              (setf (svref bytes index) byte)
1018              (incf index)))))
1019      (values bytes labels))))
1020
1021(defun finalize-code (code handler-labels optimize)
1022  (setf code (coerce (nreverse code) 'vector))
1023  (when optimize
1024    (setf code (optimize-code code handler-labels)))
1025  (resolve-instructions (expand-virtual-instructions code)))
1026
1027(provide '#:opcodes)
Note: See TracBrowser for help on using the repository browser.