1 | ;;; jvm.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2008 Peter Graves |
---|
4 | ;;; $Id: jvm.lisp 11851 2009-05-09 20:05:25Z 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 | (export '(compile-defun *catch-errors* jvm-compile jvm-compile-package |
---|
35 | derive-compiler-type)) |
---|
36 | |
---|
37 | (eval-when (:compile-toplevel :load-toplevel :execute) |
---|
38 | (require "LOOP") |
---|
39 | (require "FORMAT") |
---|
40 | (require "CLOS") |
---|
41 | (require "PRINT-OBJECT") |
---|
42 | (require "COMPILER-TYPES") |
---|
43 | (require "KNOWN-FUNCTIONS") |
---|
44 | (require "KNOWN-SYMBOLS") |
---|
45 | (require "DUMP-FORM") |
---|
46 | (require "OPCODES") |
---|
47 | (require "JAVA") |
---|
48 | (require "COMPILER-PASS1") |
---|
49 | (require "COMPILER-PASS2")) |
---|
50 | |
---|
51 | (defvar *closure-variables* nil) |
---|
52 | |
---|
53 | (defvar *enable-dformat* nil) |
---|
54 | |
---|
55 | #+nil |
---|
56 | (defun dformat (destination control-string &rest args) |
---|
57 | (when *enable-dformat* |
---|
58 | (apply #'sys::%format destination control-string args))) |
---|
59 | |
---|
60 | (defmacro dformat (&rest ignored) |
---|
61 | (declare (ignore ignored))) |
---|
62 | |
---|
63 | |
---|
64 | (defmacro with-saved-compiler-policy (&body body) |
---|
65 | "Saves compiler policy variables, restoring them after evaluating `body'." |
---|
66 | `(let ((*speed* *speed*) |
---|
67 | (*space* *space*) |
---|
68 | (*safety* *safety*) |
---|
69 | (*debug* *debug*) |
---|
70 | (*explain* *explain*) |
---|
71 | (*inline-declarations* *inline-declarations*)) |
---|
72 | ,@body)) |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | (defvar *compiler-debug* nil) |
---|
77 | |
---|
78 | (defvar *pool* nil) |
---|
79 | (defvar *pool-count* 1) |
---|
80 | (defvar *pool-entries* nil) |
---|
81 | (defvar *fields* ()) |
---|
82 | (defvar *static-code* ()) |
---|
83 | |
---|
84 | (defvar *declared-symbols* nil) |
---|
85 | (defvar *declared-functions* nil) |
---|
86 | (defvar *declared-strings* nil) |
---|
87 | (defvar *declared-integers* nil) |
---|
88 | (defvar *declared-floats* nil) |
---|
89 | (defvar *declared-doubles* nil) |
---|
90 | |
---|
91 | (defstruct (class-file (:constructor %make-class-file)) |
---|
92 | pathname ; pathname of output file |
---|
93 | lambda-name |
---|
94 | class |
---|
95 | superclass |
---|
96 | lambda-list ; as advertised |
---|
97 | pool |
---|
98 | (pool-count 1) |
---|
99 | (pool-entries (make-hash-table :test #'equal)) |
---|
100 | fields |
---|
101 | methods |
---|
102 | static-code |
---|
103 | (symbols (make-hash-table :test 'eq)) |
---|
104 | (functions (make-hash-table :test 'equal)) |
---|
105 | (strings (make-hash-table :test 'eq)) |
---|
106 | (integers (make-hash-table :test 'eql)) |
---|
107 | (floats (make-hash-table :test 'eql)) |
---|
108 | (doubles (make-hash-table :test 'eql))) |
---|
109 | |
---|
110 | (defun class-name-from-filespec (filespec) |
---|
111 | (let* ((name (pathname-name filespec))) |
---|
112 | (declare (type string name)) |
---|
113 | (dotimes (i (length name)) |
---|
114 | (declare (type fixnum i)) |
---|
115 | (when (char= (char name i) #\-) |
---|
116 | (setf (char name i) #\_))) |
---|
117 | (concatenate 'string "org/armedbear/lisp/" name))) |
---|
118 | |
---|
119 | (defun make-class-file (&key pathname lambda-name lambda-list) |
---|
120 | (aver (not (null pathname))) |
---|
121 | (let ((class-file (%make-class-file :pathname pathname |
---|
122 | :lambda-name lambda-name |
---|
123 | :lambda-list lambda-list))) |
---|
124 | (setf (class-file-class class-file) (class-name-from-filespec pathname)) |
---|
125 | class-file)) |
---|
126 | |
---|
127 | (defmacro with-class-file (class-file &body body) |
---|
128 | (let ((var (gensym))) |
---|
129 | `(let* ((,var ,class-file) |
---|
130 | (*pool* (class-file-pool ,var)) |
---|
131 | (*pool-count* (class-file-pool-count ,var)) |
---|
132 | (*pool-entries* (class-file-pool-entries ,var)) |
---|
133 | (*fields* (class-file-fields ,var)) |
---|
134 | (*static-code* (class-file-static-code ,var)) |
---|
135 | (*declared-symbols* (class-file-symbols ,var)) |
---|
136 | (*declared-functions* (class-file-functions ,var)) |
---|
137 | (*declared-strings* (class-file-strings ,var)) |
---|
138 | (*declared-integers* (class-file-integers ,var)) |
---|
139 | (*declared-floats* (class-file-floats ,var)) |
---|
140 | (*declared-doubles* (class-file-doubles ,var))) |
---|
141 | (progn ,@body) |
---|
142 | (setf (class-file-pool ,var) *pool* |
---|
143 | (class-file-pool-count ,var) *pool-count* |
---|
144 | (class-file-pool-entries ,var) *pool-entries* |
---|
145 | (class-file-fields ,var) *fields* |
---|
146 | (class-file-static-code ,var) *static-code* |
---|
147 | (class-file-symbols ,var) *declared-symbols* |
---|
148 | (class-file-functions ,var) *declared-functions* |
---|
149 | (class-file-strings ,var) *declared-strings* |
---|
150 | (class-file-integers ,var) *declared-integers* |
---|
151 | (class-file-floats ,var) *declared-floats* |
---|
152 | (class-file-doubles ,var) *declared-doubles*)))) |
---|
153 | |
---|
154 | (defstruct compiland |
---|
155 | name |
---|
156 | (kind :external) ; :INTERNAL or :EXTERNAL |
---|
157 | lambda-expression |
---|
158 | arg-vars |
---|
159 | free-specials |
---|
160 | arity ; NIL if the number of args can vary. |
---|
161 | p1-result |
---|
162 | parent |
---|
163 | (children 0 :type fixnum) ; Number of local functions defined with FLET or LABELS. |
---|
164 | argument-register |
---|
165 | closure-register |
---|
166 | environment-register |
---|
167 | class-file ; class-file object |
---|
168 | (%single-valued-p t)) |
---|
169 | |
---|
170 | (defknown compiland-single-valued-p (t) t) |
---|
171 | (defun compiland-single-valued-p (compiland) |
---|
172 | (unless (compiland-parent compiland) |
---|
173 | (let ((name (compiland-name compiland))) |
---|
174 | (when name |
---|
175 | (let ((result-type |
---|
176 | (or (function-result-type name) |
---|
177 | (and (proclaimed-ftype name) |
---|
178 | (ftype-result-type (proclaimed-ftype name)))))) |
---|
179 | (when result-type |
---|
180 | (return-from compiland-single-valued-p |
---|
181 | (cond ((eq result-type '*) |
---|
182 | nil) |
---|
183 | ((atom result-type) |
---|
184 | t) |
---|
185 | ((eq (%car result-type) 'VALUES) |
---|
186 | (= (length result-type) 2)) |
---|
187 | (t |
---|
188 | t)))))))) |
---|
189 | ;; Otherwise... |
---|
190 | (compiland-%single-valued-p compiland)) |
---|
191 | |
---|
192 | (defvar *current-compiland* nil) |
---|
193 | |
---|
194 | (defvar *this-class* nil) |
---|
195 | |
---|
196 | (defvar *code* ()) |
---|
197 | |
---|
198 | ;; All tags visible at the current point of compilation, some of which may not |
---|
199 | ;; be in the current compiland. |
---|
200 | (defvar *visible-tags* ()) |
---|
201 | |
---|
202 | ;; The next available register. |
---|
203 | (defvar *register* 0) |
---|
204 | |
---|
205 | ;; Total number of registers allocated. |
---|
206 | (defvar *registers-allocated* 0) |
---|
207 | |
---|
208 | (defvar *handlers* ()) |
---|
209 | |
---|
210 | (defstruct handler |
---|
211 | from |
---|
212 | to |
---|
213 | code |
---|
214 | catch-type) |
---|
215 | |
---|
216 | ;; Variables visible at the current point of compilation. |
---|
217 | (defvar *visible-variables* nil) |
---|
218 | |
---|
219 | ;; All variables seen so far. |
---|
220 | (defvar *all-variables* nil) |
---|
221 | |
---|
222 | ;; Undefined variables that we've already warned about. |
---|
223 | (defvar *undefined-variables* nil) |
---|
224 | |
---|
225 | (defvar *dump-variables* nil) |
---|
226 | |
---|
227 | (defun dump-1-variable (variable) |
---|
228 | (sys::%format t " ~S special-p = ~S register = ~S index = ~S declared-type = ~S~%" |
---|
229 | (variable-name variable) |
---|
230 | (variable-special-p variable) |
---|
231 | (variable-register variable) |
---|
232 | (variable-index variable) |
---|
233 | (variable-declared-type variable))) |
---|
234 | |
---|
235 | (defun dump-variables (list caption &optional (force nil)) |
---|
236 | (when (or force *dump-variables*) |
---|
237 | (write-string caption) |
---|
238 | (if list |
---|
239 | (dolist (variable list) |
---|
240 | (dump-1-variable variable)) |
---|
241 | (sys::%format t " None.~%")))) |
---|
242 | |
---|
243 | (defstruct (variable-info (:conc-name variable-) |
---|
244 | (:constructor make-variable) |
---|
245 | (:predicate variable-p)) |
---|
246 | name |
---|
247 | initform |
---|
248 | temp-register |
---|
249 | (declared-type :none) |
---|
250 | (derived-type :none) |
---|
251 | ignore-p |
---|
252 | ignorable-p |
---|
253 | representation |
---|
254 | special-p ; indicates whether a variable is special |
---|
255 | register ; register number for a local variable |
---|
256 | index ; index number for a variable in the argument array |
---|
257 | closure-index ; index number for a variable in the closure context array |
---|
258 | ;; a variable can be either special-p *or* have a register *or* |
---|
259 | ;; have an index *or a closure-index |
---|
260 | reserved-register |
---|
261 | (reads 0 :type fixnum) |
---|
262 | (writes 0 :type fixnum) |
---|
263 | references |
---|
264 | used-non-locally-p |
---|
265 | (compiland *current-compiland*)) |
---|
266 | |
---|
267 | (defstruct (var-ref (:constructor make-var-ref (variable))) |
---|
268 | ;; The variable this reference refers to. Will be NIL if the VAR-REF has been |
---|
269 | ;; rewritten to reference a constant value. |
---|
270 | variable |
---|
271 | ;; True if the VAR-REF has been rewritten to reference a constant value. |
---|
272 | constant-p |
---|
273 | ;; The constant value of this VAR-REF. |
---|
274 | constant-value) |
---|
275 | |
---|
276 | ;; obj can be a symbol or variable |
---|
277 | ;; returns variable or nil |
---|
278 | (declaim (ftype (function (t) t) unboxed-fixnum-variable)) |
---|
279 | (defun unboxed-fixnum-variable (obj) |
---|
280 | (cond ((symbolp obj) |
---|
281 | (let ((variable (find-visible-variable obj))) |
---|
282 | (if (and variable |
---|
283 | (eq (variable-representation variable) :int)) |
---|
284 | variable |
---|
285 | nil))) |
---|
286 | ((variable-p obj) |
---|
287 | (if (eq (variable-representation obj) :int) |
---|
288 | obj |
---|
289 | nil)) |
---|
290 | (t |
---|
291 | nil))) |
---|
292 | |
---|
293 | (defvar *child-p* nil |
---|
294 | "True for local functions created by FLET, LABELS and (NAMED-)LAMBDA") |
---|
295 | |
---|
296 | (defknown find-variable (symbol list) t) |
---|
297 | (defun find-variable (name variables) |
---|
298 | (dolist (variable variables) |
---|
299 | (when (eq name (variable-name variable)) |
---|
300 | (return variable)))) |
---|
301 | |
---|
302 | (defknown find-visible-variable (t) t) |
---|
303 | (defun find-visible-variable (name) |
---|
304 | (dolist (variable *visible-variables*) |
---|
305 | (when (eq name (variable-name variable)) |
---|
306 | (return variable)))) |
---|
307 | |
---|
308 | (defknown allocate-register () (integer 0 65535)) |
---|
309 | (defun allocate-register () |
---|
310 | (let* ((register *register*) |
---|
311 | (next-register (1+ register))) |
---|
312 | (declare (type (unsigned-byte 16) register next-register)) |
---|
313 | (setf *register* next-register) |
---|
314 | (when (< *registers-allocated* next-register) |
---|
315 | (setf *registers-allocated* next-register)) |
---|
316 | register)) |
---|
317 | |
---|
318 | (defknown allocate-register-pair () (integer 0 65535)) |
---|
319 | (defun allocate-register-pair () |
---|
320 | (let* ((register *register*) |
---|
321 | (next-register (+ register 2))) |
---|
322 | (declare (type (unsigned-byte 16) register next-register)) |
---|
323 | (setf *register* next-register) |
---|
324 | (when (< *registers-allocated* next-register) |
---|
325 | (setf *registers-allocated* next-register)) |
---|
326 | register)) |
---|
327 | |
---|
328 | (defstruct local-function |
---|
329 | name |
---|
330 | compiland |
---|
331 | inline-expansion |
---|
332 | function |
---|
333 | class-file |
---|
334 | variable) |
---|
335 | |
---|
336 | (defvar *local-functions* ()) |
---|
337 | |
---|
338 | (defknown find-local-function (t) t) |
---|
339 | (defun find-local-function (name) |
---|
340 | (dolist (local-function *local-functions* nil) |
---|
341 | (when (equal name (local-function-name local-function)) |
---|
342 | (return local-function)))) |
---|
343 | |
---|
344 | (defvar *using-arg-array* nil) |
---|
345 | (defvar *hairy-arglist-p* nil) |
---|
346 | |
---|
347 | (defstruct node |
---|
348 | ;; Block name or (TAGBODY) or (LET) or (MULTIPLE-VALUE-BIND). |
---|
349 | name |
---|
350 | form |
---|
351 | (compiland *current-compiland*)) |
---|
352 | |
---|
353 | ;; Used to wrap TAGBODYs, UNWIND-PROTECTs and LET/LET*/M-V-B forms as well as |
---|
354 | ;; BLOCKs per se. |
---|
355 | (defstruct (block-node (:conc-name block-) (:include node) (:constructor make-block-node (name))) |
---|
356 | (exit (gensym)) |
---|
357 | target |
---|
358 | catch-tag |
---|
359 | ;; True if there is any RETURN from this block. |
---|
360 | return-p |
---|
361 | ;; True if there is a non-local RETURN from this block. |
---|
362 | non-local-return-p |
---|
363 | ;; True if a tag in this tagbody is the target of a non-local GO. |
---|
364 | non-local-go-p |
---|
365 | ;; If non-nil, the TAGBODY contains local blocks which "contaminate" the |
---|
366 | ;; environment, with GO forms in them which target tags in this TAGBODY |
---|
367 | ;; Non-nil if and only if the block doesn't modify the environment |
---|
368 | needs-environment-restoration |
---|
369 | ;; If non-nil, register containing saved dynamic environment for this block. |
---|
370 | environment-register |
---|
371 | ;; Only used in LET/LET*/M-V-B nodes. |
---|
372 | vars |
---|
373 | free-specials |
---|
374 | ;; Only used in TAGBODY |
---|
375 | tags |
---|
376 | ) |
---|
377 | |
---|
378 | (defvar *blocks* ()) |
---|
379 | |
---|
380 | (defun find-block (name) |
---|
381 | (dolist (block *blocks*) |
---|
382 | (when (eq name (block-name block)) |
---|
383 | (return block)))) |
---|
384 | |
---|
385 | (defknown node-constant-p (t) boolean) |
---|
386 | (defun node-constant-p (object) |
---|
387 | (cond ((block-node-p object) |
---|
388 | nil) |
---|
389 | ((var-ref-p object) |
---|
390 | nil) |
---|
391 | ((constantp object) |
---|
392 | t) |
---|
393 | (t |
---|
394 | nil))) |
---|
395 | |
---|
396 | (defknown block-requires-non-local-exit-p (t) boolean) |
---|
397 | (defun block-requires-non-local-exit-p (object) |
---|
398 | "A block which *always* requires a 'non-local-exit' is a block which |
---|
399 | requires a transfer control exception to be thrown: e.g. Go and Return. |
---|
400 | |
---|
401 | Non-local exits are required by blocks which do more in their cleanup |
---|
402 | than just restore the lastSpecialBinding (= dynamic environment). |
---|
403 | " |
---|
404 | (let ((name (block-name object))) |
---|
405 | (or (equal name '(CATCH)) |
---|
406 | (equal name '(UNWIND-PROTECT))))) |
---|
407 | |
---|
408 | |
---|
409 | (defknown enclosed-by-protected-block-p (&optional t) boolean) |
---|
410 | (defun enclosed-by-protected-block-p (&optional outermost-block) |
---|
411 | "Indicates whether the code being compiled/analyzed is enclosed in |
---|
412 | a block which requires a non-local transfer of control exception to |
---|
413 | be generated. |
---|
414 | " |
---|
415 | (dolist (enclosing-block *blocks*) |
---|
416 | (when (eq enclosing-block outermost-block) |
---|
417 | (return-from enclosed-by-protected-block-p nil)) |
---|
418 | (when (block-requires-non-local-exit-p enclosing-block) |
---|
419 | (return-from enclosed-by-protected-block-p t)))) |
---|
420 | |
---|
421 | (defknown enclosed-by-environment-setting-block-p (&optional t) boolean) |
---|
422 | (defun enclosed-by-environment-setting-block-p (&optional outermost-block) |
---|
423 | (dolist (enclosing-block *blocks*) |
---|
424 | (when (eq enclosing-block outermost-block) |
---|
425 | (return nil)) |
---|
426 | (when (and (block-environment-register enclosing-block) |
---|
427 | (not (block-needs-environment-restoration enclosing-block))) |
---|
428 | (return t)))) |
---|
429 | |
---|
430 | (defknown environment-register-to-restore (&optional t) t) |
---|
431 | (defun environment-register-to-restore (&optional outermost-block) |
---|
432 | "Returns the environment register which contains the |
---|
433 | saved environment from the outermost enclosing block: |
---|
434 | |
---|
435 | That's the one which contains the environment used in the outermost block." |
---|
436 | (flet ((outermost-register (last-register block) |
---|
437 | (when (eq block outermost-block) |
---|
438 | (return-from environment-register-to-restore last-register)) |
---|
439 | (or (block-environment-register block) |
---|
440 | last-register))) |
---|
441 | (reduce #'outermost-register *blocks* |
---|
442 | :initial-value nil))) |
---|
443 | |
---|
444 | (defstruct tag |
---|
445 | ;; The symbol (or integer) naming the tag |
---|
446 | name |
---|
447 | ;; The symbol which is the jump target in JVM byte code |
---|
448 | label |
---|
449 | ;; The associated TAGBODY |
---|
450 | block |
---|
451 | (compiland *current-compiland*) |
---|
452 | used) |
---|
453 | |
---|
454 | (defknown find-tag (t) t) |
---|
455 | (defun find-tag (name) |
---|
456 | (dolist (tag *visible-tags*) |
---|
457 | (when (eql name (tag-name tag)) |
---|
458 | (return tag)))) |
---|
459 | |
---|
460 | (defun process-ignore/ignorable (declaration names variables) |
---|
461 | (when (memq declaration '(IGNORE IGNORABLE)) |
---|
462 | (let ((what (if (eq declaration 'IGNORE) "ignored" "ignorable"))) |
---|
463 | (dolist (name names) |
---|
464 | (let ((variable (find-variable name variables))) |
---|
465 | (cond ((null variable) |
---|
466 | (compiler-style-warn "Declaring unknown variable ~S to be ~A." |
---|
467 | name what)) |
---|
468 | ((variable-special-p variable) |
---|
469 | (compiler-style-warn "Declaring special variable ~S to be ~A." |
---|
470 | name what)) |
---|
471 | ((eq declaration 'IGNORE) |
---|
472 | (setf (variable-ignore-p variable) t)) |
---|
473 | (t |
---|
474 | (setf (variable-ignorable-p variable) t)))))))) |
---|
475 | |
---|
476 | (defvar *file-compilation* nil) |
---|
477 | (defvar *pathnames-generator* #'make-temp-file) |
---|
478 | |
---|
479 | (defun compile (name &optional definition) |
---|
480 | (let ((*file-compilation* nil) |
---|
481 | (*pathnames-generator* #'make-temp-file) |
---|
482 | (sys::*fasl-anonymous-package* (sys::%make-package))) |
---|
483 | (jvm-compile name definition))) |
---|
484 | |
---|
485 | (defmacro with-file-compilation (&body body) |
---|
486 | `(let ((*file-compilation* t) |
---|
487 | (*pathnames-generator* #'sys::next-classfile-name)) |
---|
488 | ,@body)) |
---|
489 | |
---|
490 | (defun finalize-generic-functions () |
---|
491 | (dolist (sym '(make-instance |
---|
492 | initialize-instance |
---|
493 | shared-initialize)) |
---|
494 | (let ((gf (and (fboundp sym) (fdefinition sym)))) |
---|
495 | (when (typep gf 'generic-function) |
---|
496 | (unless (compiled-function-p gf) |
---|
497 | (mop::finalize-generic-function gf)))))) |
---|
498 | |
---|
499 | (finalize-generic-functions) |
---|
500 | |
---|
501 | (provide 'jvm) |
---|