1 | ;;; jvm.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2008 Peter Graves |
---|
4 | ;;; $Id: jvm.lisp 13488 2011-08-13 20:26:01Z 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-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 "COMPILER-ERROR") |
---|
44 | (require "KNOWN-FUNCTIONS") |
---|
45 | (require "DUMP-FORM") |
---|
46 | (require "JVM-INSTRUCTIONS") |
---|
47 | (require "JVM-CLASS-FILE") |
---|
48 | (require "KNOWN-SYMBOLS") |
---|
49 | (require "JAVA") |
---|
50 | (require "COMPILER-PASS1") |
---|
51 | (require "COMPILER-PASS2")) |
---|
52 | |
---|
53 | (defvar *closure-variables* nil) |
---|
54 | |
---|
55 | (defvar *enable-dformat* nil) |
---|
56 | (defvar *callbacks* nil |
---|
57 | "A list of functions to be called by the compiler and code generator |
---|
58 | in order to generate 'compilation events'.") |
---|
59 | |
---|
60 | (declaim (inline invoke-callbacks)) |
---|
61 | (defun invoke-callbacks (&rest args) |
---|
62 | (dolist (cb *callbacks*) |
---|
63 | (apply cb args))) |
---|
64 | |
---|
65 | #+nil |
---|
66 | (defun dformat (destination control-string &rest args) |
---|
67 | (when *enable-dformat* |
---|
68 | (apply #'sys::%format destination control-string args))) |
---|
69 | |
---|
70 | (defmacro dformat (&rest ignored) |
---|
71 | (declare (ignore ignored))) |
---|
72 | |
---|
73 | (declaim (inline u2 s1 s2)) |
---|
74 | |
---|
75 | (defknown u2 (fixnum) cons) |
---|
76 | (defun u2 (n) |
---|
77 | (declare (optimize speed)) |
---|
78 | (declare (type (unsigned-byte 16) n)) |
---|
79 | (when (not (<= 0 n 65535)) |
---|
80 | (error "u2 argument ~A out of 65k range." n)) |
---|
81 | (list (logand (ash n -8) #xff) |
---|
82 | (logand n #xff))) |
---|
83 | |
---|
84 | (defknown s1 (fixnum) fixnum) |
---|
85 | (defun s1 (n) |
---|
86 | (declare (optimize speed)) |
---|
87 | (declare (type (signed-byte 8) n)) |
---|
88 | (when (not (<= -128 n 127)) |
---|
89 | (error "s1 argument ~A out of 8-bit signed range." n)) |
---|
90 | (if (< n 0) |
---|
91 | (1+ (logxor (- n) #xFF)) |
---|
92 | n)) |
---|
93 | |
---|
94 | |
---|
95 | (defknown s2 (fixnum) cons) |
---|
96 | (defun s2 (n) |
---|
97 | (declare (optimize speed)) |
---|
98 | (declare (type (signed-byte 16) n)) |
---|
99 | (when (not (<= -32768 n 32767)) |
---|
100 | (error "s2 argument ~A out of 16-bit signed range." n)) |
---|
101 | (u2 (if (< n 0) (1+ (logxor (- n) #xFFFF)) |
---|
102 | n))) |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | (defmacro with-saved-compiler-policy (&body body) |
---|
109 | "Saves compiler policy variables, restoring them after evaluating `body'." |
---|
110 | `(let ((*speed* *speed*) |
---|
111 | (*space* *space*) |
---|
112 | (*safety* *safety*) |
---|
113 | (*debug* *debug*) |
---|
114 | (*explain* *explain*) |
---|
115 | (*inline-declarations* *inline-declarations*)) |
---|
116 | ,@body)) |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | (defvar *compiler-debug* nil) |
---|
121 | |
---|
122 | (defvar *pool* nil) |
---|
123 | (defvar *static-code* ()) |
---|
124 | (defvar *class-file* nil) |
---|
125 | |
---|
126 | (defvar *externalized-objects* nil) |
---|
127 | (defvar *declared-functions* nil) |
---|
128 | |
---|
129 | (defstruct (abcl-class-file (:include class-file) |
---|
130 | (:constructor %make-abcl-class-file)) |
---|
131 | pathname ; pathname of output file |
---|
132 | class-name |
---|
133 | static-initializer |
---|
134 | constructor |
---|
135 | objects ;; an alist of externalized objects and their field names |
---|
136 | (functions (make-hash-table :test 'equal)) ;; because of (SETF ...) functions |
---|
137 | ) |
---|
138 | |
---|
139 | (defun class-name-from-filespec (filespec) |
---|
140 | (let* ((name (pathname-name filespec))) |
---|
141 | (declare (type string name)) |
---|
142 | (dotimes (i (length name)) |
---|
143 | (declare (type fixnum i)) |
---|
144 | (when (or (char= (char name i) #\-) |
---|
145 | (char= (char name i) #\Space)) |
---|
146 | (setf (char name i) #\_))) |
---|
147 | (make-jvm-class-name |
---|
148 | (concatenate 'string "org.armedbear.lisp." name)))) |
---|
149 | |
---|
150 | (defun make-unique-class-name () |
---|
151 | "Creates a random class name for use with a `class-file' structure's |
---|
152 | `class' slot." |
---|
153 | (make-jvm-class-name |
---|
154 | (concatenate 'string "abcl_" |
---|
155 | (substitute #\_ #\- |
---|
156 | (java:jcall (java:jmethod "java.util.UUID" |
---|
157 | "toString") |
---|
158 | (java:jstatic "randomUUID" |
---|
159 | "java.util.UUID")))))) |
---|
160 | |
---|
161 | (defun make-abcl-class-file (&key pathname) |
---|
162 | "Creates a `class-file' structure. If `pathname' is non-NIL, it's |
---|
163 | used to derive a class name. If it is NIL, a random one created |
---|
164 | using `make-unique-class-name'." |
---|
165 | (let* ((class-name (if pathname |
---|
166 | (class-name-from-filespec pathname) |
---|
167 | (make-unique-class-name))) |
---|
168 | (class-file (%make-abcl-class-file :pathname pathname |
---|
169 | :class class-name ; to be finalized |
---|
170 | :class-name class-name |
---|
171 | :access-flags '(:public :final)))) |
---|
172 | (when *file-compilation* |
---|
173 | (let ((source-attribute |
---|
174 | (make-source-file-attribute |
---|
175 | :filename (file-namestring *compile-file-truename*)))) |
---|
176 | (class-add-attribute class-file source-attribute))) |
---|
177 | class-file)) |
---|
178 | |
---|
179 | (defmacro with-class-file (class-file &body body) |
---|
180 | (let ((var (gensym))) |
---|
181 | `(let* ((,var ,class-file) |
---|
182 | (*class-file* ,var) |
---|
183 | (*pool* (abcl-class-file-constants ,var)) |
---|
184 | (*externalized-objects* (abcl-class-file-objects ,var)) |
---|
185 | (*declared-functions* (abcl-class-file-functions ,var))) |
---|
186 | (progn ,@body) |
---|
187 | (setf (abcl-class-file-objects ,var) *externalized-objects* |
---|
188 | (abcl-class-file-functions ,var) *declared-functions*)))) |
---|
189 | |
---|
190 | (defstruct compiland |
---|
191 | name |
---|
192 | lambda-expression |
---|
193 | arg-vars ; variables for lambda arguments |
---|
194 | free-specials ; |
---|
195 | arity ; number of args, or NIL if the number of args can vary. |
---|
196 | p1-result ; the parse tree as created in pass 1 |
---|
197 | parent ; the parent for compilands which defined within another |
---|
198 | children ; List of local functions |
---|
199 | ; defined with FLET, LABELS or LAMBDA |
---|
200 | blocks ; TAGBODY, PROGV, BLOCK, etc. blocks |
---|
201 | argument-register |
---|
202 | closure-register |
---|
203 | environment-register |
---|
204 | class-file ; class-file object |
---|
205 | (%single-valued-p t)) |
---|
206 | |
---|
207 | (defknown compiland-single-valued-p (t) t) |
---|
208 | (defun compiland-single-valued-p (compiland) |
---|
209 | (unless (compiland-parent compiland) |
---|
210 | (let ((name (compiland-name compiland))) |
---|
211 | (when name |
---|
212 | (let ((result-type |
---|
213 | (or (function-result-type name) |
---|
214 | (and (proclaimed-ftype name) |
---|
215 | (ftype-result-type (proclaimed-ftype name)))))) |
---|
216 | (when result-type |
---|
217 | (return-from compiland-single-valued-p |
---|
218 | (cond ((eq result-type '*) |
---|
219 | nil) |
---|
220 | ((atom result-type) |
---|
221 | t) |
---|
222 | ((eq (%car result-type) 'VALUES) |
---|
223 | (= (length result-type) 2)) |
---|
224 | (t |
---|
225 | t)))))))) |
---|
226 | ;; Otherwise... |
---|
227 | (compiland-%single-valued-p compiland)) |
---|
228 | |
---|
229 | (defvar *current-compiland* nil) |
---|
230 | |
---|
231 | (defvar *this-class* nil) |
---|
232 | |
---|
233 | ;; All tags visible at the current point of compilation, some of which may not |
---|
234 | ;; be in the current compiland. |
---|
235 | (defvar *visible-tags* ()) |
---|
236 | |
---|
237 | ;; The next available register. |
---|
238 | (defvar *register* 0) |
---|
239 | |
---|
240 | ;; Total number of registers allocated. |
---|
241 | (defvar *registers-allocated* 0) |
---|
242 | |
---|
243 | ;; Variables visible at the current point of compilation. |
---|
244 | (defvar *visible-variables* nil |
---|
245 | "All variables visible to the form currently being |
---|
246 | processed, including free specials.") |
---|
247 | |
---|
248 | ;; All variables seen so far. |
---|
249 | (defvar *all-variables* nil |
---|
250 | "All variables in the lexical scope (thus excluding free specials) |
---|
251 | of the compilands being processed (p1: so far; p2: in total).") |
---|
252 | |
---|
253 | ;; Undefined variables that we've already warned about. |
---|
254 | (defvar *undefined-variables* nil) |
---|
255 | |
---|
256 | (defvar *dump-variables* nil) |
---|
257 | |
---|
258 | (defun dump-1-variable (variable) |
---|
259 | (sys::%format t " ~S special-p = ~S register = ~S binding-reg = ~S index = ~S declared-type = ~S~%" |
---|
260 | (variable-name variable) |
---|
261 | (variable-special-p variable) |
---|
262 | (variable-register variable) |
---|
263 | (variable-binding-register variable) |
---|
264 | (variable-index variable) |
---|
265 | (variable-declared-type variable))) |
---|
266 | |
---|
267 | (defun dump-variables (list caption &optional (force nil)) |
---|
268 | (when (or force *dump-variables*) |
---|
269 | (write-string caption) |
---|
270 | (if list |
---|
271 | (dolist (variable list) |
---|
272 | (dump-1-variable variable)) |
---|
273 | (sys::%format t " None.~%")))) |
---|
274 | |
---|
275 | (defstruct (variable-info (:conc-name variable-) |
---|
276 | (:constructor make-variable) |
---|
277 | (:predicate variable-p)) |
---|
278 | name |
---|
279 | initform |
---|
280 | (declared-type :none) |
---|
281 | (derived-type :none) |
---|
282 | ignore-p |
---|
283 | ignorable-p |
---|
284 | representation |
---|
285 | special-p ; indicates whether a variable is special |
---|
286 | |
---|
287 | ;; A variable can be stored in a number of locations. |
---|
288 | ;; 1. if it's passed as a normal argument, it'll be in a register (max 8) |
---|
289 | ;; the same is true if the variable is a local variable (at any index) |
---|
290 | ;; 2. if it's passed in the argument array, it'll be in the array in |
---|
291 | ;; register 1 (register 0 contains the function object) |
---|
292 | ;; 3. if the variable is part of a closure, it'll be in the closure array |
---|
293 | ;; 4. if the variable is part of the outer scope of a function with a |
---|
294 | ;; non-null lexical environment, the variable is to be looked up |
---|
295 | ;; from a lexical environment object |
---|
296 | ;; 5. the variable is a special variable and its binding has been looked |
---|
297 | ;; up and cached in a local register (binding-register) |
---|
298 | |
---|
299 | ;; a variable can be either special-p *or* have a register *or* |
---|
300 | ;; have an index *or* a closure-index *or* an environment |
---|
301 | |
---|
302 | register ; register number for a local variable |
---|
303 | binding-register ; register number containing the binding reference |
---|
304 | index ; index number for a variable in the argument array |
---|
305 | closure-index ; index number for a variable in the closure context array |
---|
306 | environment ; the environment for the variable, if we're compiling in |
---|
307 | ; a non-null lexical environment with variables |
---|
308 | |
---|
309 | (reads 0 :type fixnum) |
---|
310 | (writes 0 :type fixnum) |
---|
311 | references |
---|
312 | (references-allowed-p t) ; NIL if this is a symbol macro in the enclosing |
---|
313 | ; lexical environment |
---|
314 | used-non-locally-p |
---|
315 | (compiland *current-compiland*) |
---|
316 | block) |
---|
317 | |
---|
318 | (defstruct (var-ref (:constructor make-var-ref (variable))) |
---|
319 | ;; The variable this reference refers to. Will be NIL if the VAR-REF has been |
---|
320 | ;; rewritten to reference a constant value. |
---|
321 | variable |
---|
322 | ;; True if the VAR-REF has been rewritten to reference a constant value. |
---|
323 | constant-p |
---|
324 | ;; The constant value of this VAR-REF. |
---|
325 | constant-value) |
---|
326 | |
---|
327 | ;; obj can be a symbol or variable |
---|
328 | ;; returns variable or nil |
---|
329 | (declaim (ftype (function (t) t) unboxed-fixnum-variable)) |
---|
330 | (defun unboxed-fixnum-variable (obj) |
---|
331 | (cond ((symbolp obj) |
---|
332 | (let ((variable (find-visible-variable obj))) |
---|
333 | (if (and variable |
---|
334 | (eq (variable-representation variable) :int)) |
---|
335 | variable |
---|
336 | nil))) |
---|
337 | ((variable-p obj) |
---|
338 | (if (eq (variable-representation obj) :int) |
---|
339 | obj |
---|
340 | nil)) |
---|
341 | (t |
---|
342 | nil))) |
---|
343 | |
---|
344 | (defvar *child-p* nil |
---|
345 | "True for local functions created by FLET, LABELS and (NAMED-)LAMBDA") |
---|
346 | |
---|
347 | (defknown find-variable (symbol list) t) |
---|
348 | (defun find-variable (name variables) |
---|
349 | (dolist (variable variables) |
---|
350 | (when (eq name (variable-name variable)) |
---|
351 | (return variable)))) |
---|
352 | |
---|
353 | (defknown find-visible-variable (t) t) |
---|
354 | (defun find-visible-variable (name) |
---|
355 | (dolist (variable *visible-variables*) |
---|
356 | (when (eq name (variable-name variable)) |
---|
357 | (return variable)))) |
---|
358 | |
---|
359 | (defknown representation-size (t) (integer 0 65535)) |
---|
360 | (defun representation-size (representation) |
---|
361 | (ecase representation |
---|
362 | ((NIL :int :boolean :float :char) 1) |
---|
363 | ((:long :double) 2))) |
---|
364 | |
---|
365 | (defknown allocate-register (t) (integer 0 65535)) |
---|
366 | (defun allocate-register (representation) |
---|
367 | (let ((register *register*)) |
---|
368 | (incf *register* (representation-size representation)) |
---|
369 | (setf *registers-allocated* |
---|
370 | (max *registers-allocated* *register*)) |
---|
371 | register)) |
---|
372 | |
---|
373 | |
---|
374 | (defstruct local-function |
---|
375 | name |
---|
376 | definition |
---|
377 | compiland |
---|
378 | field |
---|
379 | inline-expansion |
---|
380 | environment ;; the environment in which the function is stored in |
---|
381 | ;; case of a function from an enclosing lexical environment |
---|
382 | ;; which itself isn't being compiled |
---|
383 | (references-allowed-p t) ;;whether a reference to the function CAN be captured |
---|
384 | (references-needed-p nil) ;;whether a reference to the function NEEDS to be |
---|
385 | ;;captured, because the function name is used in a |
---|
386 | ;;(function ...) form. Obviously implies |
---|
387 | ;;references-allowed-p. |
---|
388 | ) |
---|
389 | |
---|
390 | (defvar *local-functions* ()) |
---|
391 | |
---|
392 | (defknown find-local-function (t) t) |
---|
393 | (defun find-local-function (name) |
---|
394 | (dolist (local-function *local-functions* nil) |
---|
395 | (when (equal name (local-function-name local-function)) |
---|
396 | (return local-function)))) |
---|
397 | |
---|
398 | (defvar *using-arg-array* nil) |
---|
399 | (defvar *hairy-arglist-p* nil) |
---|
400 | |
---|
401 | |
---|
402 | (defvar *block* nil |
---|
403 | "The innermost block applicable to the current lexical environment.") |
---|
404 | (defvar *blocks* () |
---|
405 | "The list of blocks in effect in the current lexical environment. |
---|
406 | |
---|
407 | The top node does not need to be equal to the value of `*block*`. E.g. |
---|
408 | when processing the bindings of a LET form, `*block*` is bound to the node |
---|
409 | of that LET, while the block is not considered 'in effect': that only happens |
---|
410 | until the body is being processed.") |
---|
411 | |
---|
412 | (defstruct node |
---|
413 | form |
---|
414 | children |
---|
415 | (compiland *current-compiland*)) |
---|
416 | ;; No need for a special constructor: nobody instantiates |
---|
417 | ;; nodes directly |
---|
418 | |
---|
419 | (declaim (inline add-node-child)) |
---|
420 | (defun add-node-child (parent child) |
---|
421 | "Add a child node to the `children` list of a parent node, |
---|
422 | if that parent belongs to the same compiland." |
---|
423 | (when parent |
---|
424 | (when (eq (node-compiland parent) *current-compiland*) |
---|
425 | (push child (node-children parent))))) |
---|
426 | |
---|
427 | ;; control-transferring blocks: TAGBODY, CATCH, to do: BLOCK |
---|
428 | |
---|
429 | (defstruct (control-transferring-node (:include node)) |
---|
430 | ;; If non-nil, the TAGBODY contains local blocks which "contaminate" the |
---|
431 | ;; environment, with GO forms in them which target tags in this TAGBODY |
---|
432 | ;; Non-nil if and only if the block doesn't modify the environment |
---|
433 | needs-environment-restoration |
---|
434 | ) |
---|
435 | ;; No need for a special constructor: nobody instantiates |
---|
436 | ;; control-transferring-nodes directly |
---|
437 | |
---|
438 | (defstruct (tagbody-node (:conc-name tagbody-) |
---|
439 | (:include control-transferring-node) |
---|
440 | (:constructor %make-tagbody-node ())) |
---|
441 | ;; True if a tag in this tagbody is the target of a non-local GO. |
---|
442 | non-local-go-p |
---|
443 | ;; Tags in the tagbody form; a list of tag structures |
---|
444 | tags |
---|
445 | ;; Contains a variable whose value uniquely identifies the |
---|
446 | ;; lexical scope from this block, to be used by GO |
---|
447 | id-variable) |
---|
448 | (defknown make-tagbody-node () t) |
---|
449 | (defun make-tagbody-node () |
---|
450 | (let ((block (%make-tagbody-node))) |
---|
451 | (push block (compiland-blocks *current-compiland*)) |
---|
452 | (add-node-child *block* block) |
---|
453 | block)) |
---|
454 | |
---|
455 | (defstruct (catch-node (:conc-name catch-) |
---|
456 | (:include control-transferring-node) |
---|
457 | (:constructor %make-catch-node ())) |
---|
458 | ;; The catch tag-form is evaluated, meaning we |
---|
459 | ;; have no predefined value to store here |
---|
460 | ) |
---|
461 | (defknown make-catch-node () t) |
---|
462 | (defun make-catch-node () |
---|
463 | (let ((block (%make-catch-node))) |
---|
464 | (push block (compiland-blocks *current-compiland*)) |
---|
465 | (add-node-child *block* block) |
---|
466 | block)) |
---|
467 | |
---|
468 | (defstruct (block-node (:conc-name block-) |
---|
469 | (:include control-transferring-node) |
---|
470 | (:constructor %make-block-node (name))) |
---|
471 | name ;; Block name |
---|
472 | (exit (gensym)) |
---|
473 | target |
---|
474 | ;; True if there is a non-local RETURN from this block. |
---|
475 | non-local-return-p |
---|
476 | ;; Contains a variable whose value uniquely identifies the |
---|
477 | ;; lexical scope from this block, to be used by RETURN-FROM |
---|
478 | id-variable |
---|
479 | ;; A list of all RETURN-FROM value forms associated with this block |
---|
480 | return-value-forms) |
---|
481 | |
---|
482 | (defknown make-block-node (t) t) |
---|
483 | (defun make-block-node (name) |
---|
484 | (let ((block (%make-block-node name))) |
---|
485 | (push block (compiland-blocks *current-compiland*)) |
---|
486 | (add-node-child *block* block) |
---|
487 | block)) |
---|
488 | |
---|
489 | (defstruct (jump-node (:conc-name jump-) |
---|
490 | (:include node) |
---|
491 | (:constructor |
---|
492 | %make-jump-node (non-local-p target-block target-tag))) |
---|
493 | non-local-p |
---|
494 | target-block |
---|
495 | target-tag) |
---|
496 | (defun make-jump-node (form non-local-p target-block &optional target-tag) |
---|
497 | (let ((node (%make-jump-node non-local-p target-block target-tag))) |
---|
498 | ;; Don't push into compiland blocks, as this as a node rather than a block |
---|
499 | (setf (node-form node) form) |
---|
500 | (add-node-child *block* node) |
---|
501 | node)) |
---|
502 | |
---|
503 | |
---|
504 | ;; binding blocks: LET, LET*, FLET, LABELS, M-V-B, PROGV, LOCALLY |
---|
505 | ;; |
---|
506 | ;; Binding blocks can carry references to local (optionally special) variable bindings, |
---|
507 | ;; contain free special bindings or both |
---|
508 | |
---|
509 | (defstruct (binding-node (:include node)) |
---|
510 | ;; number of the register of the saved dynamic env, or NIL if none |
---|
511 | environment-register |
---|
512 | ;; Not used for LOCALLY and FLET; LABELS uses vars to store its functions |
---|
513 | vars |
---|
514 | free-specials) |
---|
515 | ;; nobody instantiates any binding nodes directly, so there's no reason |
---|
516 | ;; to create a constructor with the approprate administration code |
---|
517 | |
---|
518 | (defstruct (let/let*-node (:conc-name let-) |
---|
519 | (:include binding-node) |
---|
520 | (:constructor %make-let/let*-node ()))) |
---|
521 | (defknown make-let/let*-node () t) |
---|
522 | (defun make-let/let*-node () |
---|
523 | (let ((block (%make-let/let*-node))) |
---|
524 | (push block (compiland-blocks *current-compiland*)) |
---|
525 | (add-node-child *block* block) |
---|
526 | block)) |
---|
527 | |
---|
528 | (defstruct (flet-node (:conc-name flet-) |
---|
529 | (:include binding-node) |
---|
530 | (:constructor %make-flet-node ()))) |
---|
531 | (defknown make-flet-node () t) |
---|
532 | (defun make-flet-node () |
---|
533 | (let ((block (%make-flet-node))) |
---|
534 | (push block (compiland-blocks *current-compiland*)) |
---|
535 | (add-node-child *block* block) |
---|
536 | block)) |
---|
537 | |
---|
538 | (defstruct (labels-node (:conc-name labels-) |
---|
539 | (:include binding-node) |
---|
540 | (:constructor %make-labels-node ()))) |
---|
541 | (defknown make-labels-node () t) |
---|
542 | (defun make-labels-node () |
---|
543 | (let ((block (%make-labels-node))) |
---|
544 | (push block (compiland-blocks *current-compiland*)) |
---|
545 | (add-node-child *block* block) |
---|
546 | block)) |
---|
547 | |
---|
548 | (defstruct (m-v-b-node (:conc-name m-v-b-) |
---|
549 | (:include binding-node) |
---|
550 | (:constructor %make-m-v-b-node ()))) |
---|
551 | (defknown make-m-v-b-node () t) |
---|
552 | (defun make-m-v-b-node () |
---|
553 | (let ((block (%make-m-v-b-node))) |
---|
554 | (push block (compiland-blocks *current-compiland*)) |
---|
555 | (add-node-child *block* block) |
---|
556 | block)) |
---|
557 | |
---|
558 | (defstruct (progv-node (:conc-name progv-) |
---|
559 | (:include binding-node) |
---|
560 | (:constructor %make-progv-node ()))) |
---|
561 | (defknown make-progv-node () t) |
---|
562 | (defun make-progv-node () |
---|
563 | (let ((block (%make-progv-node))) |
---|
564 | (push block (compiland-blocks *current-compiland*)) |
---|
565 | block)) |
---|
566 | |
---|
567 | (defstruct (locally-node (:conc-name locally-) |
---|
568 | (:include binding-node) |
---|
569 | (:constructor %make-locally-node ()))) |
---|
570 | (defknown make-locally-node () t) |
---|
571 | (defun make-locally-node () |
---|
572 | (let ((block (%make-locally-node))) |
---|
573 | (push block (compiland-blocks *current-compiland*)) |
---|
574 | (add-node-child *block* block) |
---|
575 | block)) |
---|
576 | |
---|
577 | ;; blocks requiring non-local exits: UNWIND-PROTECT, SYS:SYNCHRONIZED-ON |
---|
578 | |
---|
579 | (defstruct (protected-node (:include node) |
---|
580 | (:constructor %make-protected-node ()))) |
---|
581 | (defknown make-protected-node () t) |
---|
582 | (defun make-protected-node () |
---|
583 | (let ((block (%make-protected-node))) |
---|
584 | (push block (compiland-blocks *current-compiland*)) |
---|
585 | (add-node-child *block* block) |
---|
586 | block)) |
---|
587 | |
---|
588 | (defstruct (unwind-protect-node (:conc-name unwind-protect-) |
---|
589 | (:include protected-node) |
---|
590 | (:constructor %make-unwind-protect-node ()))) |
---|
591 | (defknown make-unwind-protect-node () t) |
---|
592 | (defun make-unwind-protect-node () |
---|
593 | (let ((block (%make-unwind-protect-node))) |
---|
594 | (push block (compiland-blocks *current-compiland*)) |
---|
595 | (add-node-child *block* block) |
---|
596 | block)) |
---|
597 | |
---|
598 | (defstruct (synchronized-node (:conc-name synchronized-) |
---|
599 | (:include protected-node) |
---|
600 | (:constructor %make-synchronized-node ()))) |
---|
601 | (defknown make-synchronized-node () t) |
---|
602 | (defun make-synchronized-node () |
---|
603 | (let ((block (%make-synchronized-node))) |
---|
604 | (push block (compiland-blocks *current-compiland*)) |
---|
605 | (add-node-child *block* block) |
---|
606 | block)) |
---|
607 | |
---|
608 | (defun find-block (name) |
---|
609 | (dolist (block *blocks*) |
---|
610 | (when (and (block-node-p block) |
---|
611 | (eq name (block-name block))) |
---|
612 | (return block)))) |
---|
613 | |
---|
614 | (defun %find-enclosed-blocks (form) |
---|
615 | "Helper function for `find-enclosed-blocks`, implementing the actual |
---|
616 | algorithm specified there." |
---|
617 | (cond |
---|
618 | ((node-p form) (list form)) |
---|
619 | ((atom form) nil) |
---|
620 | (t |
---|
621 | ;; We can't use MAPCAN or DOLIST here: they'll choke on dotted lists |
---|
622 | (do* ((tail form (cdr tail)) |
---|
623 | blocks) |
---|
624 | ((null tail) blocks) |
---|
625 | (setf blocks |
---|
626 | (nconc (%find-enclosed-blocks (if (consp tail) |
---|
627 | (car tail) tail)) |
---|
628 | blocks)) |
---|
629 | (when (not (listp tail)) |
---|
630 | (return blocks)))))) |
---|
631 | |
---|
632 | (defun find-enclosed-blocks (form) |
---|
633 | "Returns the immediate enclosed blocks by searching the form's subforms. |
---|
634 | |
---|
635 | More deeply nested blocks can be reached through the `node-children` |
---|
636 | field of the immediate enclosed blocks." |
---|
637 | (when *blocks* |
---|
638 | ;; when the innermost enclosing block doesn't have node-children, |
---|
639 | ;; there's really nothing to search for. |
---|
640 | (let ((first-enclosing-block (car *blocks*))) |
---|
641 | (when (and (eq *current-compiland* |
---|
642 | (node-compiland first-enclosing-block)) |
---|
643 | (null (node-children first-enclosing-block))) |
---|
644 | (return-from find-enclosed-blocks)))) |
---|
645 | |
---|
646 | (%find-enclosed-blocks form)) |
---|
647 | |
---|
648 | |
---|
649 | (defun some-nested-block (predicate blocks) |
---|
650 | "Applies `predicate` recursively to the `blocks` and its children, |
---|
651 | until predicate returns non-NIL, returning that value. |
---|
652 | |
---|
653 | `blocks` may be a single block or a list of blocks." |
---|
654 | (when blocks |
---|
655 | (some #'(lambda (b) |
---|
656 | (or (funcall predicate b) |
---|
657 | (some-nested-block predicate (node-children b)))) |
---|
658 | (if (listp blocks) |
---|
659 | blocks |
---|
660 | (list blocks))))) |
---|
661 | |
---|
662 | (defknown node-constant-p (t) boolean) |
---|
663 | (defun node-constant-p (object) |
---|
664 | (cond ((node-p object) |
---|
665 | nil) |
---|
666 | ((var-ref-p object) |
---|
667 | nil) |
---|
668 | ((constantp object) |
---|
669 | t) |
---|
670 | (t |
---|
671 | nil))) |
---|
672 | |
---|
673 | (defknown block-requires-non-local-exit-p (t) boolean) |
---|
674 | (defun block-requires-non-local-exit-p (object) |
---|
675 | "A block which *always* requires a 'non-local-exit' is a block which |
---|
676 | requires a transfer control exception to be thrown: e.g. Go and Return. |
---|
677 | |
---|
678 | Non-local exits are required by blocks which do more in their cleanup |
---|
679 | than just restore the lastSpecialBinding (= dynamic environment). |
---|
680 | " |
---|
681 | (or (unwind-protect-node-p object) |
---|
682 | (catch-node-p object) |
---|
683 | (synchronized-node-p object))) |
---|
684 | |
---|
685 | (defun node-opstack-unsafe-p (node) |
---|
686 | (or (when (jump-node-p node) |
---|
687 | (let ((target-block (jump-target-block node))) |
---|
688 | (and (null (jump-non-local-p node)) |
---|
689 | (member target-block *blocks*)))) |
---|
690 | (when (tagbody-node-p node) (tagbody-non-local-go-p node)) |
---|
691 | (when (block-node-p node) (block-non-local-return-p node)) |
---|
692 | (catch-node-p node))) |
---|
693 | |
---|
694 | (defknown block-creates-runtime-bindings-p (t) boolean) |
---|
695 | (defun block-creates-runtime-bindings-p (block) |
---|
696 | ;; FIXME: This may be false, if the bindings to be |
---|
697 | ;; created are a quoted list |
---|
698 | (progv-node-p block)) |
---|
699 | |
---|
700 | (defknown enclosed-by-runtime-bindings-creating-block-p (t) boolean) |
---|
701 | (defun enclosed-by-runtime-bindings-creating-block-p (outermost-block) |
---|
702 | "Indicates whether the code being compiled/analyzed is enclosed in a |
---|
703 | block which creates special bindings at runtime." |
---|
704 | (dolist (enclosing-block *blocks*) |
---|
705 | (when (eq enclosing-block outermost-block) |
---|
706 | (return-from enclosed-by-runtime-bindings-creating-block-p nil)) |
---|
707 | (when (block-creates-runtime-bindings-p enclosing-block) |
---|
708 | (return-from enclosed-by-runtime-bindings-creating-block-p t)))) |
---|
709 | |
---|
710 | (defknown enclosed-by-protected-block-p (&optional t) boolean) |
---|
711 | (defun enclosed-by-protected-block-p (&optional outermost-block) |
---|
712 | "Indicates whether the code being compiled/analyzed is enclosed in |
---|
713 | a block which requires a non-local transfer of control exception to |
---|
714 | be generated. |
---|
715 | " |
---|
716 | (dolist (enclosing-block *blocks*) |
---|
717 | (when (eq enclosing-block outermost-block) |
---|
718 | (return-from enclosed-by-protected-block-p nil)) |
---|
719 | (when (block-requires-non-local-exit-p enclosing-block) |
---|
720 | (return-from enclosed-by-protected-block-p t)))) |
---|
721 | |
---|
722 | (defknown enclosed-by-environment-setting-block-p (&optional t) boolean) |
---|
723 | (defun enclosed-by-environment-setting-block-p (&optional outermost-block) |
---|
724 | (dolist (enclosing-block *blocks*) |
---|
725 | (when (eq enclosing-block outermost-block) |
---|
726 | (return nil)) |
---|
727 | (when (and (binding-node-p enclosing-block) |
---|
728 | (binding-node-environment-register enclosing-block)) |
---|
729 | (return t)))) |
---|
730 | |
---|
731 | (defknown environment-register-to-restore (&optional t) t) |
---|
732 | (defun environment-register-to-restore (&optional outermost-block) |
---|
733 | "Returns the environment register which contains the |
---|
734 | saved environment from the outermost enclosing block: |
---|
735 | |
---|
736 | That's the one which contains the environment used in the outermost block." |
---|
737 | (flet ((outermost-register (last-register block) |
---|
738 | (when (eq block outermost-block) |
---|
739 | (return-from environment-register-to-restore last-register)) |
---|
740 | (or (and (binding-node-p block) |
---|
741 | (binding-node-environment-register block)) |
---|
742 | last-register))) |
---|
743 | (reduce #'outermost-register *blocks* |
---|
744 | :initial-value nil))) |
---|
745 | |
---|
746 | (defstruct tag |
---|
747 | ;; The symbol (or integer) naming the tag |
---|
748 | name |
---|
749 | ;; The symbol which is the jump target in JVM byte code |
---|
750 | label |
---|
751 | ;; The associated TAGBODY |
---|
752 | block |
---|
753 | (compiland *current-compiland*) |
---|
754 | used |
---|
755 | used-non-locally) |
---|
756 | |
---|
757 | (defknown find-tag (t) t) |
---|
758 | (defun find-tag (name) |
---|
759 | (dolist (tag *visible-tags*) |
---|
760 | (when (eql name (tag-name tag)) |
---|
761 | (return tag)))) |
---|
762 | |
---|
763 | (defun process-ignore/ignorable (declaration names variables) |
---|
764 | (when (memq declaration '(IGNORE IGNORABLE)) |
---|
765 | (let ((what (if (eq declaration 'IGNORE) "ignored" "ignorable"))) |
---|
766 | (dolist (name names) |
---|
767 | (unless (and (consp name) (eq (car name) 'FUNCTION)) |
---|
768 | (let ((variable (find-variable name variables))) |
---|
769 | (cond ((null variable) |
---|
770 | (compiler-style-warn "Declaring unknown variable ~S to be ~A." |
---|
771 | name what)) |
---|
772 | ((variable-special-p variable) |
---|
773 | (compiler-style-warn "Declaring special variable ~S to be ~A." |
---|
774 | name what)) |
---|
775 | ((eq declaration 'IGNORE) |
---|
776 | (setf (variable-ignore-p variable) t)) |
---|
777 | (t |
---|
778 | (setf (variable-ignorable-p variable) t))))))))) |
---|
779 | |
---|
780 | (defun finalize-generic-functions () |
---|
781 | (dolist (sym '(make-instance |
---|
782 | initialize-instance |
---|
783 | shared-initialize)) |
---|
784 | (let ((gf (and (fboundp sym) (fdefinition sym)))) |
---|
785 | (when (typep gf 'generic-function) |
---|
786 | (unless (compiled-function-p gf) |
---|
787 | (mop::finalize-generic-function gf)))))) |
---|
788 | |
---|
789 | (finalize-generic-functions) |
---|
790 | |
---|
791 | (provide 'jvm) |
---|