1.0.19.3: more careful PROGV and SET
[sbcl/tcr.git] / src / compiler / main.lisp
blob6a75cf05b12f347aa40899d0d883974faa96de74
1 ;;;; the top level interfaces to the compiler, plus some other
2 ;;;; compiler-related stuff (e.g. CL:CALL-ARGUMENTS-LIMIT) which
3 ;;;; doesn't obviously belong anywhere else
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
14 (in-package "SB!C")
16 ;;; FIXME: Doesn't this belong somewhere else, like early-c.lisp?
17 (declaim (special *constants* *free-vars* *component-being-compiled*
18 *code-vector* *next-location* *result-fixups*
19 *free-funs* *source-paths*
20 *seen-blocks* *seen-funs* *list-conflicts-table*
21 *continuation-number* *continuation-numbers*
22 *number-continuations* *tn-id* *tn-ids* *id-tns*
23 *label-ids* *label-id* *id-labels*
24 *undefined-warnings* *compiler-error-count*
25 *compiler-warning-count* *compiler-style-warning-count*
26 *compiler-note-count*
27 *compiler-error-bailout*
28 #!+sb-show *compiler-trace-output*
29 *last-source-context* *last-original-source*
30 *last-source-form* *last-format-string* *last-format-args*
31 *last-message-count* *last-error-context*
32 *lexenv* *fun-names-in-this-file*
33 *allow-instrumenting*))
35 ;;; Whether call of a function which cannot be defined causes a full
36 ;;; warning.
37 (defvar *flame-on-necessarily-undefined-function* nil)
39 (defvar *check-consistency* nil)
41 ;;; Set to NIL to disable loop analysis for register allocation.
42 (defvar *loop-analyze* t)
44 ;;; Bind this to a stream to capture various internal debugging output.
45 (defvar *compiler-trace-output* nil)
47 ;;; The current block compilation state. These are initialized to the
48 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
49 ;;; called with.
50 ;;;
51 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
52 ;;; argument, which overrides any internal declarations.
53 (defvar *block-compile*)
54 (defvar *block-compile-arg*)
55 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
56 (defvar *entry-points*)
57 (declaim (list *entry-points*))
59 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
60 ;;; lambdas resulting from compiling subforms. (In reverse order.)
61 (defvar *toplevel-lambdas*)
62 (declaim (list *toplevel-lambdas*))
64 ;;; The current non-macroexpanded toplevel form as printed when
65 ;;; *compile-print* is true.
66 (defvar *top-level-form-noted* nil)
68 (defvar sb!xc:*compile-verbose* t
69 #!+sb-doc
70 "The default for the :VERBOSE argument to COMPILE-FILE.")
71 (defvar sb!xc:*compile-print* t
72 #!+sb-doc
73 "The default for the :PRINT argument to COMPILE-FILE.")
74 (defvar *compile-progress* nil
75 #!+sb-doc
76 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
77 information about the phases of compilation of each function. (This
78 is useful mainly in large block compilations.)")
80 (defvar sb!xc:*compile-file-pathname* nil
81 #!+sb-doc
82 "The defaulted pathname of the file currently being compiled, or NIL if not
83 compiling.")
84 (defvar sb!xc:*compile-file-truename* nil
85 #!+sb-doc
86 "The TRUENAME of the file currently being compiled, or NIL if not
87 compiling.")
89 (declaim (type (or pathname null)
90 sb!xc:*compile-file-pathname*
91 sb!xc:*compile-file-truename*))
93 ;;; the SOURCE-INFO structure for the current compilation. This is
94 ;;; null globally to indicate that we aren't currently in any
95 ;;; identifiable compilation.
96 (defvar *source-info* nil)
98 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
99 ;;; normally causes nested uses to be no-ops).
100 (defvar *in-compilation-unit* nil)
102 ;;; Count of the number of compilation units dynamically enclosed by
103 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
104 (defvar *aborted-compilation-unit-count*)
106 ;;; Mumble conditional on *COMPILE-PROGRESS*.
107 (defun maybe-mumble (&rest foo)
108 (when *compile-progress*
109 (compiler-mumble "~&")
110 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
111 (apply #'compiler-mumble foo))))
113 (deftype object () '(or fasl-output core-object null))
115 (defvar *compile-object* nil)
116 (declaim (type object *compile-object*))
118 (defvar *fopcompile-label-counter*)
120 ;; Used during compilation to map code paths to the matching
121 ;; instrumentation conses.
122 (defvar *code-coverage-records* nil)
123 ;; Used during compilation to keep track of with source paths have been
124 ;; instrumented in which blocks.
125 (defvar *code-coverage-blocks* nil)
126 ;; Stores the code coverage instrumentation results. Keys are namestrings,
127 ;; the value is a list of (CONS PATH STATE), where STATE is NIL for
128 ;; a path that has not been visited, and T for one that has.
129 (defvar *code-coverage-info* (make-hash-table :test 'equal))
132 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
134 (defmacro sb!xc:with-compilation-unit (options &body body)
135 #!+sb-doc
136 "WITH-COMPILATION-UNIT ({Key Value}*) Form*
137 This form affects compilations that take place within its dynamic extent. It
138 is intended to be wrapped around the compilation of all files in the same
139 system. These keywords are defined:
141 :OVERRIDE Boolean-Form
142 One of the effects of this form is to delay undefined warnings
143 until the end of the form, instead of giving them at the end of each
144 compilation. If OVERRIDE is NIL (the default), then the outermost
145 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
146 OVERRIDE true causes that form to grab any enclosed warnings, even if
147 it is enclosed by another WITH-COMPILATION-UNIT.
149 :SOURCE-PLIST Plist-Form
150 Attaches the value returned by the Plist-Form to internal debug-source
151 information of functions compiled in within the dynamic contour.
152 Primarily for use by development environments, in order to eg. associate
153 function definitions with editor-buffers. Can be accessed as
154 SB-INTROSPECT:DEFINITION-SOURCE-PLIST. If multiple, nested
155 WITH-COMPILATION-UNITs provide :SOURCE-PLISTs, they are appended
156 togather, innermost left. If Unaffected by :OVERRIDE."
157 `(%with-compilation-unit (lambda () ,@body) ,@options))
159 (defvar *source-plist* nil)
161 (defun %with-compilation-unit (fn &key override source-plist)
162 (declare (type function fn))
163 (let ((succeeded-p nil)
164 (*source-plist* (append source-plist *source-plist*)))
165 (if (and *in-compilation-unit* (not override))
166 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
167 ;; ordinarily (unless OVERRIDE) basically a no-op.
168 (unwind-protect
169 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
170 (unless succeeded-p
171 (incf *aborted-compilation-unit-count*)))
172 (let ((*aborted-compilation-unit-count* 0)
173 (*compiler-error-count* 0)
174 (*compiler-warning-count* 0)
175 (*compiler-style-warning-count* 0)
176 (*compiler-note-count* 0)
177 (*undefined-warnings* nil)
178 (*in-compilation-unit* t))
179 (sb!thread:with-recursive-lock (*big-compiler-lock*)
180 (handler-bind ((parse-unknown-type
181 (lambda (c)
182 (note-undefined-reference
183 (parse-unknown-type-specifier c)
184 :type))))
185 (unwind-protect
186 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
187 (unless succeeded-p
188 (incf *aborted-compilation-unit-count*))
189 (summarize-compilation-unit (not succeeded-p)))))))))
191 ;;; Is FUN-NAME something that no conforming program can rely on
192 ;;; defining as a function?
193 (defun fun-name-reserved-by-ansi-p (fun-name)
194 (eq (symbol-package (fun-name-block-name fun-name))
195 *cl-package*))
197 ;;; This is to be called at the end of a compilation unit. It signals
198 ;;; any residual warnings about unknown stuff, then prints the total
199 ;;; error counts. ABORT-P should be true when the compilation unit was
200 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
201 ;;; enclosed nested compilation units that were aborted.
202 (defun summarize-compilation-unit (abort-p)
203 (unless abort-p
204 (handler-bind ((style-warning #'compiler-style-warning-handler)
205 (warning #'compiler-warning-handler))
207 (let ((undefs (sort *undefined-warnings* #'string<
208 :key (lambda (x)
209 (let ((x (undefined-warning-name x)))
210 (if (symbolp x)
211 (symbol-name x)
212 (prin1-to-string x)))))))
213 (dolist (undef undefs)
214 (let ((name (undefined-warning-name undef))
215 (kind (undefined-warning-kind undef))
216 (warnings (undefined-warning-warnings undef))
217 (undefined-warning-count (undefined-warning-count undef)))
218 (dolist (*compiler-error-context* warnings)
219 (if #-sb-xc-host (and (eq kind :function)
220 (fun-name-reserved-by-ansi-p name)
221 *flame-on-necessarily-undefined-function*)
222 #+sb-xc-host nil
223 (case name
224 ((declare)
225 (compiler-warn
226 "~@<There is no function named ~S. References to ~S in ~
227 some contexts (like starts of blocks) have special ~
228 meaning, but here it would have to be a function, ~
229 and that shouldn't be right.~:@>"
230 name name))
232 (compiler-warn
233 "~@<The ~(~A~) ~S is undefined, and its name is ~
234 reserved by ANSI CL so that even if it were ~
235 defined later, the code doing so would not be ~
236 portable.~:@>"
237 kind name)))
238 (if (eq kind :variable)
239 (compiler-warn "undefined ~(~A~): ~S" kind name)
240 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
241 (let ((warn-count (length warnings)))
242 (when (and warnings (> undefined-warning-count warn-count))
243 (let ((more (- undefined-warning-count warn-count)))
244 (if (eq kind :variable)
245 (compiler-warn
246 "~W more use~:P of undefined ~(~A~) ~S"
247 more kind name)
248 (compiler-style-warn
249 "~W more use~:P of undefined ~(~A~) ~S"
250 more kind name)))))))
252 (dolist (kind '(:variable :function :type))
253 (let ((summary (mapcar #'undefined-warning-name
254 (remove kind undefs :test #'neq
255 :key #'undefined-warning-kind))))
256 (when summary
257 (if (eq kind :variable)
258 (compiler-warn
259 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
260 ~% ~{~<~% ~1:;~S~>~^ ~}"
261 (cdr summary) kind summary)
262 (compiler-style-warn
263 "~:[This ~(~A~) is~;These ~(~A~)s are~] undefined:~
264 ~% ~{~<~% ~1:;~S~>~^ ~}"
265 (cdr summary) kind summary))))))))
267 (unless (and (not abort-p)
268 (zerop *aborted-compilation-unit-count*)
269 (zerop *compiler-error-count*)
270 (zerop *compiler-warning-count*)
271 (zerop *compiler-style-warning-count*)
272 (zerop *compiler-note-count*))
273 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
274 (format *error-output* "~&compilation unit ~:[finished~;aborted~]~
275 ~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
276 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
277 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
278 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
279 ~[~:;~:*~& printed ~W note~:P~]"
280 abort-p
281 *aborted-compilation-unit-count*
282 *compiler-error-count*
283 *compiler-warning-count*
284 *compiler-style-warning-count*
285 *compiler-note-count*))
286 (terpri *error-output*)
287 (force-output *error-output*)))
289 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
290 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
291 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
292 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
293 (defmacro with-compilation-values (&body body)
294 `(with-ir1-namespace
295 (let ((*warnings-p* nil)
296 (*failure-p* nil))
297 (values (progn ,@body)
298 *warnings-p*
299 *failure-p*))))
301 ;;;; component compilation
303 (defparameter *max-optimize-iterations* 3 ; ARB
304 #!+sb-doc
305 "The upper limit on the number of times that we will consecutively do IR1
306 optimization that doesn't introduce any new code. A finite limit is
307 necessary, since type inference may take arbitrarily long to converge.")
309 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
310 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
312 ;;; Repeatedly optimize COMPONENT until no further optimizations can
313 ;;; be found or we hit our iteration limit. When we hit the limit, we
314 ;;; clear the component and block REOPTIMIZE flags to discourage the
315 ;;; next optimization attempt from pounding on the same code.
316 (defun ir1-optimize-until-done (component)
317 (declare (type component component))
318 (maybe-mumble "opt")
319 (event ir1-optimize-until-done)
320 (let ((count 0)
321 (cleared-reanalyze nil)
322 (fastp nil))
323 (loop
324 (when (component-reanalyze component)
325 (setq count 0)
326 (setq cleared-reanalyze t)
327 (setf (component-reanalyze component) nil))
328 (setf (component-reoptimize component) nil)
329 (ir1-optimize component fastp)
330 (cond ((component-reoptimize component)
331 (incf count)
332 (when (and (>= count *max-optimize-iterations*)
333 (not (component-reanalyze component))
334 (eq (component-reoptimize component) :maybe))
335 (maybe-mumble "*")
336 (cond ((retry-delayed-ir1-transforms :optimize)
337 (maybe-mumble "+")
338 (setq count 0))
340 (event ir1-optimize-maxed-out)
341 (setf (component-reoptimize component) nil)
342 (do-blocks (block component)
343 (setf (block-reoptimize block) nil))
344 (return)))))
345 ((retry-delayed-ir1-transforms :optimize)
346 (setf count 0)
347 (maybe-mumble "+"))
349 (maybe-mumble " ")
350 (return)))
351 (setq fastp (>= count *max-optimize-iterations*))
352 (maybe-mumble (if fastp "-" ".")))
353 (when cleared-reanalyze
354 (setf (component-reanalyze component) t)))
355 (values))
357 (defparameter *constraint-propagate* t)
359 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
360 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
361 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
362 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
364 ;;; FIXME: It would be good to document why it's important to have a
365 ;;; large value here, and what the drawbacks of an excessively large
366 ;;; value are; and it might also be good to make it depend on
367 ;;; optimization policy.
368 (defparameter *reoptimize-after-type-check-max* 10)
370 (defevent reoptimize-maxed-out
371 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
373 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
374 (defun dfo-as-needed (component)
375 (declare (type component component))
376 (when (component-reanalyze component)
377 (maybe-mumble "DFO")
378 (loop
379 (find-dfo component)
380 (unless (component-reanalyze component)
381 (maybe-mumble " ")
382 (return))
383 (maybe-mumble ".")))
384 (values))
386 ;;; Do all the IR1 phases for a non-top-level component.
387 (defun ir1-phases (component)
388 (declare (type component component))
389 (aver-live-component component)
390 (let ((*constraint-number* 0)
391 (loop-count 1)
392 (*delayed-ir1-transforms* nil))
393 (declare (special *constraint-number* *delayed-ir1-transforms*))
394 (loop
395 (ir1-optimize-until-done component)
396 (when (or (component-new-functionals component)
397 (component-reanalyze-functionals component))
398 (maybe-mumble "locall ")
399 (locall-analyze-component component))
400 (dfo-as-needed component)
401 (when *constraint-propagate*
402 (maybe-mumble "constraint ")
403 (constraint-propagate component))
404 (when (retry-delayed-ir1-transforms :constraint)
405 (maybe-mumble "Rtran "))
406 (flet ((want-reoptimization-p ()
407 (or (component-reoptimize component)
408 (component-reanalyze component)
409 (component-new-functionals component)
410 (component-reanalyze-functionals component))))
411 (unless (and (want-reoptimization-p)
412 ;; We delay the generation of type checks until
413 ;; the type constraints have had time to
414 ;; propagate, else the compiler can confuse itself.
415 (< loop-count (- *reoptimize-after-type-check-max* 4)))
416 (maybe-mumble "type ")
417 (generate-type-checks component)
418 (unless (want-reoptimization-p)
419 (return))))
420 (when (>= loop-count *reoptimize-after-type-check-max*)
421 (maybe-mumble "[reoptimize limit]")
422 (event reoptimize-maxed-out)
423 (return))
424 (incf loop-count)))
426 (ir1-finalize component)
427 (values))
429 (defun %compile-component (component)
430 (let ((*code-segment* nil)
431 (*elsewhere* nil))
432 (maybe-mumble "GTN ")
433 (gtn-analyze component)
434 (maybe-mumble "LTN ")
435 (ltn-analyze component)
436 (dfo-as-needed component)
437 (maybe-mumble "control ")
438 (control-analyze component #'make-ir2-block)
440 (when (or (ir2-component-values-receivers (component-info component))
441 (component-dx-lvars component))
442 (maybe-mumble "stack ")
443 (stack-analyze component)
444 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
445 ;; stack analysis. There shouldn't be any unreachable code after
446 ;; control, so this won't delete anything.
447 (dfo-as-needed component))
449 (unwind-protect
450 (progn
451 (maybe-mumble "IR2tran ")
452 (init-assembler)
453 (entry-analyze component)
454 (ir2-convert component)
456 (when (policy *lexenv* (>= speed compilation-speed))
457 (maybe-mumble "copy ")
458 (copy-propagate component))
460 (select-representations component)
462 (when *check-consistency*
463 (maybe-mumble "check2 ")
464 (check-ir2-consistency component))
466 (delete-unreferenced-tns component)
468 (maybe-mumble "life ")
469 (lifetime-analyze component)
471 (when *compile-progress*
472 (compiler-mumble "") ; Sync before doing more output.
473 (pre-pack-tn-stats component *standard-output*))
475 (when *check-consistency*
476 (maybe-mumble "check-life ")
477 (check-life-consistency component))
479 (maybe-mumble "pack ")
480 (pack component)
482 (when *check-consistency*
483 (maybe-mumble "check-pack ")
484 (check-pack-consistency component))
486 (when *compiler-trace-output*
487 (describe-component component *compiler-trace-output*)
488 (describe-ir2-component component *compiler-trace-output*))
490 (maybe-mumble "code ")
491 (multiple-value-bind (code-length trace-table fixup-notes)
492 (generate-code component)
494 #-sb-xc-host
495 (when *compiler-trace-output*
496 (format *compiler-trace-output*
497 "~|~%disassembly of code for ~S~2%" component)
498 (sb!disassem:disassemble-assem-segment *code-segment*
499 *compiler-trace-output*))
501 (etypecase *compile-object*
502 (fasl-output
503 (maybe-mumble "fasl")
504 (fasl-dump-component component
505 *code-segment*
506 code-length
507 trace-table
508 fixup-notes
509 *compile-object*))
510 (core-object
511 (maybe-mumble "core")
512 (make-core-component component
513 *code-segment*
514 code-length
515 trace-table
516 fixup-notes
517 *compile-object*))
518 (null))))))
520 ;; We're done, so don't bother keeping anything around.
521 (setf (component-info component) :dead)
523 (values))
525 ;;; Delete components with no external entry points before we try to
526 ;;; generate code. Unreachable closures can cause IR2 conversion to
527 ;;; puke on itself, since it is the reference to the closure which
528 ;;; normally causes the components to be combined.
529 (defun delete-if-no-entries (component)
530 (dolist (fun (component-lambdas component) (delete-component component))
531 (when (functional-has-external-references-p fun)
532 (return))
533 (case (functional-kind fun)
534 (:toplevel (return))
535 (:external
536 (unless (every (lambda (ref)
537 (eq (node-component ref) component))
538 (leaf-refs fun))
539 (return))))))
541 (defun compile-component (component)
543 ;; miscellaneous sanity checks
545 ;; FIXME: These are basically pretty wimpy compared to the checks done
546 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
547 ;; make those internal consistency checks work again and use them.
548 (aver-live-component component)
549 (do-blocks (block component)
550 (aver (eql (block-component block) component)))
551 (dolist (lambda (component-lambdas component))
552 ;; sanity check to prevent weirdness from propagating insidiously as
553 ;; far from its root cause as it did in bug 138: Make sure that
554 ;; thing-to-COMPONENT links are consistent.
555 (aver (eql (lambda-component lambda) component))
556 (aver (eql (node-component (lambda-bind lambda)) component)))
558 (let* ((*component-being-compiled* component))
560 ;; Record xref information before optimization. This way the
561 ;; stored xref data reflects the real source as closely as
562 ;; possible.
563 (record-component-xrefs component)
565 (ir1-phases component)
567 (when *loop-analyze*
568 (dfo-as-needed component)
569 (find-dominators component)
570 (loop-analyze component))
573 (when (and *loop-analyze* *compiler-trace-output*)
574 (labels ((print-blocks (block)
575 (format *compiler-trace-output* " ~A~%" block)
576 (when (block-loop-next block)
577 (print-blocks (block-loop-next block))))
578 (print-loop (loop)
579 (format *compiler-trace-output* "loop=~A~%" loop)
580 (print-blocks (loop-blocks loop))
581 (dolist (l (loop-inferiors loop))
582 (print-loop l))))
583 (print-loop (component-outer-loop component))))
586 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
587 (maybe-mumble "env ")
588 (physenv-analyze component)
589 (dfo-as-needed component)
591 (delete-if-no-entries component)
593 (unless (eq (block-next (component-head component))
594 (component-tail component))
595 (%compile-component component)))
597 (clear-constant-info)
599 (values))
601 ;;;; clearing global data structures
602 ;;;;
603 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
604 ;;;; global data structures entirely when possible and consing up the
605 ;;;; others from scratch instead of clearing and reusing them?
607 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
608 ;;; addition to allowing stuff to be reclaimed, this is required for
609 ;;; correct assignment of constant offsets, since we need to assign a
610 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
611 ;;; slots, since they are used to keep track of functions across
612 ;;; component boundaries.
613 (defun clear-constant-info ()
614 (maphash (lambda (k v)
615 (declare (ignore k))
616 (setf (leaf-info v) nil))
617 *constants*)
618 (maphash (lambda (k v)
619 (declare (ignore k))
620 (when (constant-p v)
621 (setf (leaf-info v) nil)))
622 *free-vars*)
623 (values))
625 ;;; Blow away the REFS for all global variables, and let COMPONENT
626 ;;; be recycled.
627 (defun clear-ir1-info (component)
628 (declare (type component component))
629 (labels ((blast (x)
630 (maphash (lambda (k v)
631 (declare (ignore k))
632 (when (leaf-p v)
633 (setf (leaf-refs v)
634 (delete-if #'here-p (leaf-refs v)))
635 (when (basic-var-p v)
636 (setf (basic-var-sets v)
637 (delete-if #'here-p (basic-var-sets v))))))
639 (here-p (x)
640 (eq (node-component x) component)))
641 (blast *free-vars*)
642 (blast *free-funs*)
643 (blast *constants*))
644 (values))
646 ;;; Clear global variables used by the compiler.
648 ;;; FIXME: It seems kinda nasty and unmaintainable to have to do this,
649 ;;; and it adds overhead even when people aren't using the compiler.
650 ;;; Perhaps we could make these global vars unbound except when
651 ;;; actually in use, so that this function could go away.
652 (defun clear-stuff (&optional (debug-too t))
654 ;; Clear global tables.
655 (when (boundp '*free-funs*)
656 (clrhash *free-funs*)
657 (clrhash *free-vars*)
658 (clrhash *constants*))
660 ;; Clear debug counters and tables.
661 (clrhash *seen-blocks*)
662 (clrhash *seen-funs*)
663 (clrhash *list-conflicts-table*)
665 (when debug-too
666 (clrhash *continuation-numbers*)
667 (clrhash *number-continuations*)
668 (setq *continuation-number* 0)
669 (clrhash *tn-ids*)
670 (clrhash *id-tns*)
671 (setq *tn-id* 0)
672 (clrhash *label-ids*)
673 (clrhash *id-labels*)
674 (setq *label-id* 0))
676 ;; (Note: The CMU CL code used to set CL::*GENSYM-COUNTER* to zero here.
677 ;; Superficially, this seemed harmful -- the user could reasonably be
678 ;; surprised if *GENSYM-COUNTER* turned back to zero when something was
679 ;; compiled. A closer inspection showed that this actually turned out to be
680 ;; harmless in practice, because CLEAR-STUFF was only called from within
681 ;; forms which bound CL::*GENSYM-COUNTER* to zero. However, this means that
682 ;; even though zeroing CL::*GENSYM-COUNTER* here turned out to be harmless in
683 ;; practice, it was also useless in practice. So we don't do it any more.)
685 (values))
687 ;;;; trace output
689 ;;; Print out some useful info about COMPONENT to STREAM.
690 (defun describe-component (component *standard-output*)
691 (declare (type component component))
692 (format t "~|~%;;;; component: ~S~2%" (component-name component))
693 (print-all-blocks component)
694 (values))
696 (defun describe-ir2-component (component *standard-output*)
697 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
698 (format t "entries:~%")
699 (dolist (entry (ir2-component-entries (component-info component)))
700 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
701 (label-id (entry-info-offset entry))
702 (entry-info-name entry)
703 (entry-info-closure-tn entry)))
704 (terpri)
705 (pre-pack-tn-stats component *standard-output*)
706 (terpri)
707 (print-ir2-blocks component)
708 (terpri)
709 (values))
711 ;;;; file reading
712 ;;;;
713 ;;;; When reading from a file, we have to keep track of some source
714 ;;;; information. We also exploit our ability to back up for printing
715 ;;;; the error context and for recovering from errors.
716 ;;;;
717 ;;;; The interface we provide to this stuff is the stream-oid
718 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
719 ;;;; of getting the next source form.
721 ;;; A FILE-INFO structure holds all the source information for a
722 ;;; given file.
723 (def!struct (file-info
724 (:copier nil)
725 #-no-ansi-print-object
726 (:print-object (lambda (s stream)
727 (print-unreadable-object (s stream :type t)
728 (princ (file-info-name s) stream)))))
729 ;; If a file, the truename of the corresponding source file. If from
730 ;; a Lisp form, :LISP. If from a stream, :STREAM.
731 (name (missing-arg) :type (or pathname (eql :lisp)))
732 ;; the external format that we'll call OPEN with, if NAME is a file.
733 (external-format nil)
734 ;; the defaulted, but not necessarily absolute file name (i.e. prior
735 ;; to TRUENAME call.) Null if not a file. This is used to set
736 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
737 ;; debug-info.
738 (untruename nil :type (or pathname null))
739 ;; the file's write date (if relevant)
740 (write-date nil :type (or unsigned-byte null))
741 ;; the source path root number of the first form in this file (i.e.
742 ;; the total number of forms converted previously in this
743 ;; compilation)
744 (source-root 0 :type unsigned-byte)
745 ;; parallel vectors containing the forms read out of the file and
746 ;; the file positions that reading of each form started at (i.e. the
747 ;; end of the previous form)
748 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
749 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
751 ;;; The SOURCE-INFO structure provides a handle on all the source
752 ;;; information for an entire compilation.
753 (def!struct (source-info
754 #-no-ansi-print-object
755 (:print-object (lambda (s stream)
756 (print-unreadable-object (s stream :type t))))
757 (:copier nil))
758 ;; the UT that compilation started at
759 (start-time (get-universal-time) :type unsigned-byte)
760 ;; the FILE-INFO structure for this compilation
761 (file-info nil :type (or file-info null))
762 ;; the stream that we are using to read the FILE-INFO, or NIL if
763 ;; no stream has been opened yet
764 (stream nil :type (or stream null))
765 ;; if the current compilation is recursive (e.g., due to EVAL-WHEN
766 ;; processing at compile-time), the invoking compilation's
767 ;; source-info.
768 (parent nil :type (or source-info null)))
770 ;;; Given a pathname, return a SOURCE-INFO structure.
771 (defun make-file-source-info (file external-format)
772 (make-source-info
773 :file-info (make-file-info :name (truename file)
774 :untruename (merge-pathnames file)
775 :external-format external-format
776 :write-date (file-write-date file))))
778 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
779 (defun make-lisp-source-info (form &key parent)
780 (make-source-info
781 :file-info (make-file-info :name :lisp
782 :forms (vector form)
783 :positions '#(0))
784 :parent parent))
786 ;;; Walk up the SOURCE-INFO list until we either reach a SOURCE-INFO
787 ;;; with no parent (e.g., from a REPL evaluation) or until we reach a
788 ;;; SOURCE-INFO whose FILE-INFO denotes a file.
789 (defun get-toplevelish-file-info (&optional (source-info *source-info*))
790 (if source-info
791 (do* ((sinfo source-info (source-info-parent sinfo))
792 (finfo (source-info-file-info sinfo)
793 (source-info-file-info sinfo)))
794 ((or (not (source-info-p (source-info-parent sinfo)))
795 (pathnamep (file-info-name finfo)))
796 finfo))))
798 ;;; Return a form read from STREAM; or for EOF use the trick,
799 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
800 ;;; error happens, then convert it to standard abort-the-compilation
801 ;;; error condition (possibly recording some extra location
802 ;;; information).
803 (defun read-for-compile-file (stream position)
804 (handler-case (read stream nil stream)
805 (reader-error (condition)
806 (error 'input-error-in-compile-file
807 :condition condition
808 ;; We don't need to supply :POSITION here because
809 ;; READER-ERRORs already know their position in the file.
811 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
812 ;; (and that this is not a READER-ERROR) when it encounters end of
813 ;; file in the middle of something it's trying to read.
814 (end-of-file (condition)
815 (error 'input-error-in-compile-file
816 :condition condition
817 ;; We need to supply :POSITION here because the END-OF-FILE
818 ;; condition doesn't carry the position that the user
819 ;; probably cares about, where the failed READ began.
820 :position position))))
822 ;;; If STREAM is present, return it, otherwise open a stream to the
823 ;;; current file. There must be a current file.
825 ;;; FIXME: This is probably an unnecessarily roundabout way to do
826 ;;; things now that we process a single file in COMPILE-FILE (unlike
827 ;;; the old CMU CL code, which accepted multiple files). Also, the old
828 ;;; comment said
829 ;;; When we open a new file, we also reset *PACKAGE* and policy.
830 ;;; This gives the effect of rebinding around each file.
831 ;;; which doesn't seem to be true now. Check to make sure that if
832 ;;; such rebinding is necessary, it's still done somewhere.
833 (defun get-source-stream (info)
834 (declare (type source-info info))
835 (or (source-info-stream info)
836 (let* ((file-info (source-info-file-info info))
837 (name (file-info-name file-info))
838 (external-format (file-info-external-format file-info)))
839 (setf sb!xc:*compile-file-truename* name
840 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
841 (source-info-stream info)
842 (open name :direction :input
843 :external-format external-format)))))
845 ;;; Close the stream in INFO if it is open.
846 (defun close-source-info (info)
847 (declare (type source-info info))
848 (let ((stream (source-info-stream info)))
849 (when stream (close stream)))
850 (setf (source-info-stream info) nil)
851 (values))
853 ;;; Loop over FORMS retrieved from INFO. Used by COMPILE-FILE and
854 ;;; LOAD when loading from a FILE-STREAM associated with a source
855 ;;; file.
856 (defmacro do-forms-from-info (((form &rest keys) info)
857 &body body)
858 (aver (symbolp form))
859 (once-only ((info info))
860 `(let ((*source-info* ,info))
861 (loop (destructuring-bind (,form &key ,@keys &allow-other-keys)
862 (let* ((file-info (source-info-file-info ,info))
863 (stream (get-source-stream ,info))
864 (pos (file-position stream))
865 (form (read-for-compile-file stream pos)))
866 (if (eq form stream) ; i.e., if EOF
867 (return)
868 (let* ((forms (file-info-forms file-info))
869 (current-idx (+ (fill-pointer forms)
870 (file-info-source-root
871 file-info))))
872 (vector-push-extend form forms)
873 (vector-push-extend pos (file-info-positions
874 file-info))
875 (list form :current-index current-idx))))
876 ,@body)))))
878 ;;; Read and compile the source file.
879 (defun sub-sub-compile-file (info)
880 (do-forms-from-info ((form current-index) info)
881 (find-source-paths form current-index)
882 (process-toplevel-form
883 form `(original-source-start 0 ,current-index) nil)))
885 ;;; Return the INDEX'th source form read from INFO and the position
886 ;;; where it was read.
887 (defun find-source-root (index info)
888 (declare (type index index) (type source-info info))
889 (let ((file-info (source-info-file-info info)))
890 (values (aref (file-info-forms file-info) index)
891 (aref (file-info-positions file-info) index))))
893 ;;;; processing of top level forms
895 ;;; This is called by top level form processing when we are ready to
896 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
897 ;;; convert the form, but delay compilation, pushing the result on
898 ;;; *TOPLEVEL-LAMBDAS* instead.
899 (defun convert-and-maybe-compile (form path)
900 (declare (list path))
901 (let ((*top-level-form-noted* (note-top-level-form form t)))
902 ;; Don't bother to compile simple objects that just sit there.
903 (when (and form (or (symbolp form) (consp form)))
904 (if (fopcompilable-p form)
905 (let ((*fopcompile-label-counter* 0))
906 (fopcompile form path nil))
907 (let ((*lexenv* (make-lexenv
908 :policy *policy*
909 :handled-conditions *handled-conditions*
910 :disabled-package-locks *disabled-package-locks*))
911 (tll (ir1-toplevel form path nil)))
912 (if (eq *block-compile* t)
913 (push tll *toplevel-lambdas*)
914 (compile-toplevel (list tll) nil))
915 nil)))))
917 ;;; Macroexpand FORM in the current environment with an error handler.
918 ;;; We only expand one level, so that we retain all the intervening
919 ;;; forms in the source path.
920 (defun preprocessor-macroexpand-1 (form)
921 (handler-case (sb!xc:macroexpand-1 form *lexenv*)
922 (error (condition)
923 (compiler-error "(during macroexpansion of ~A)~%~A"
924 (let ((*print-level* 2)
925 (*print-length* 2))
926 (format nil "~S" form))
927 condition))))
929 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
930 ;;; the forms, and PATH is the source path of the FORM they came out of.
931 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
932 (defun process-toplevel-progn (forms path compile-time-too)
933 (declare (list forms) (list path))
934 (dolist (form forms)
935 (process-toplevel-form form path compile-time-too)))
937 ;;; Process a top level use of LOCALLY, or anything else (e.g.
938 ;;; MACROLET) at top level which has declarations and ordinary forms.
939 ;;; We parse declarations and then recursively process the body.
940 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
941 (declare (list path))
942 (multiple-value-bind (forms decls)
943 (parse-body body :doc-string-allowed nil :toplevel t)
944 (let* ((*lexenv* (process-decls decls vars funs))
945 ;; FIXME: VALUES declaration
947 ;; Binding *POLICY* is pretty much of a hack, since it
948 ;; causes LOCALLY to "capture" enclosed proclamations. It
949 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
950 ;; value of *POLICY* as the policy. The need for this hack
951 ;; is due to the quirk that there is no way to represent in
952 ;; a POLICY that an optimize quality came from the default.
954 ;; FIXME: Ideally, something should be done so that DECLAIM
955 ;; inside LOCALLY works OK. Failing that, at least we could
956 ;; issue a warning instead of silently screwing up.
957 (*policy* (lexenv-policy *lexenv*))
958 ;; This is probably also a hack
959 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
960 ;; ditto
961 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
962 (process-toplevel-progn forms path compile-time-too))))
964 ;;; Parse an EVAL-WHEN situations list, returning three flags,
965 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
966 ;;; the types of situations present in the list.
967 (defun parse-eval-when-situations (situations)
968 (when (or (not (listp situations))
969 (set-difference situations
970 '(:compile-toplevel
971 compile
972 :load-toplevel
973 load
974 :execute
975 eval)))
976 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
977 (let ((deprecated-names (intersection situations '(compile load eval))))
978 (when deprecated-names
979 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
980 deprecated-names)))
981 (values (intersection '(:compile-toplevel compile)
982 situations)
983 (intersection '(:load-toplevel load) situations)
984 (intersection '(:execute eval) situations)))
987 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
988 (defun functional-components (f)
989 (declare (type functional f))
990 (etypecase f
991 (clambda (list (lambda-component f)))
992 (optional-dispatch (let ((result nil))
993 (flet ((maybe-frob (maybe-clambda)
994 (when (and maybe-clambda
995 (promise-ready-p maybe-clambda))
996 (pushnew (lambda-component
997 (force maybe-clambda))
998 result))))
999 (map nil #'maybe-frob (optional-dispatch-entry-points f))
1000 (maybe-frob (optional-dispatch-more-entry f))
1001 (maybe-frob (optional-dispatch-main-entry f)))
1002 result))))
1004 (defun make-functional-from-toplevel-lambda (lambda-expression
1005 &key
1006 name
1007 (path
1008 ;; I'd thought NIL should
1009 ;; work, but it doesn't.
1010 ;; -- WHN 2001-09-20
1011 (missing-arg)))
1012 (let* ((*current-path* path)
1013 (component (make-empty-component))
1014 (*current-component* component)
1015 (debug-name-tail (or name (name-lambdalike lambda-expression)))
1016 (source-name (or name '.anonymous.)))
1017 (setf (component-name component) (debug-name 'initial-component debug-name-tail)
1018 (component-kind component) :initial)
1019 (let* ((locall-fun (let ((*allow-instrumenting* t))
1020 (funcall #'ir1-convert-lambdalike
1021 lambda-expression
1022 :source-name source-name)))
1023 ;; Convert the XEP using the policy of the real
1024 ;; function. Otherwise the wrong policy will be used for
1025 ;; deciding whether to type-check the parameters of the
1026 ;; real function (via CONVERT-CALL / PROPAGATE-TO-ARGS).
1027 ;; -- JES, 2007-02-27
1028 (*lexenv* (make-lexenv :policy (lexenv-policy
1029 (functional-lexenv locall-fun))))
1030 (fun (ir1-convert-lambda (make-xep-lambda-expression locall-fun)
1031 :source-name source-name
1032 :debug-name (debug-name 'tl-xep debug-name-tail))))
1033 (when name
1034 (assert-global-function-definition-type name locall-fun))
1035 (setf (functional-entry-fun fun) locall-fun
1036 (functional-kind fun) :external
1037 (functional-has-external-references-p locall-fun) t
1038 (functional-has-external-references-p fun) t)
1039 fun)))
1041 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1042 ;;; description of the result.
1043 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1044 ;;; into core and return the compiled FUNCTION value.
1045 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1046 ;;; into the fasl file and return a dump handle.
1048 ;;; If NAME is provided, then we try to use it as the name of the
1049 ;;; function for debugging/diagnostic information.
1050 (defun %compile (lambda-expression
1051 *compile-object*
1052 &key
1053 name
1054 (path
1055 ;; This magical idiom seems to be the appropriate
1056 ;; path for compiling standalone LAMBDAs, judging
1057 ;; from the CMU CL code and experiment, so it's a
1058 ;; nice default for things where we don't have a
1059 ;; real source path (as in e.g. inside CL:COMPILE).
1060 '(original-source-start 0 0)))
1061 (when name
1062 (legal-fun-name-or-type-error name))
1063 (let* ((*lexenv* (make-lexenv
1064 :policy *policy*
1065 :handled-conditions *handled-conditions*
1066 :disabled-package-locks *disabled-package-locks*))
1067 (*compiler-sset-counter* 0)
1068 (fun (make-functional-from-toplevel-lambda lambda-expression
1069 :name name
1070 :path path)))
1072 ;; FIXME: The compile-it code from here on is sort of a
1073 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1074 ;; better to find a way to share the code there; or
1075 ;; alternatively, to use this code to replace the code there.
1076 ;; (The second alternative might be pretty easy if we used
1077 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1078 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1080 (locall-analyze-clambdas-until-done (list fun))
1082 (let ((components-from-dfo (find-initial-dfo (list fun))))
1083 (dolist (component-from-dfo components-from-dfo)
1084 (compile-component component-from-dfo)
1085 (replace-toplevel-xeps component-from-dfo))
1087 (let ((entry-table (etypecase *compile-object*
1088 (fasl-output (fasl-output-entry-table
1089 *compile-object*))
1090 (core-object (core-object-entry-table
1091 *compile-object*)))))
1092 (multiple-value-bind (result found-p)
1093 (gethash (leaf-info fun) entry-table)
1094 (aver found-p)
1095 (prog1
1096 result
1097 ;; KLUDGE: This code duplicates some other code in this
1098 ;; file. In the great reorganzation, the flow of program
1099 ;; logic changed from the original CMUCL model, and that
1100 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1101 ;; longer followed for CORE-OBJECTS, leading to BUG
1102 ;; 156. This place is transparently not the right one for
1103 ;; this code, but I don't have a clear enough overview of
1104 ;; the compiler to know how to rearrange it all so that
1105 ;; this operation fits in nicely, and it was blocking
1106 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1107 ;; ((..)) (DEFUN FOO ...))
1109 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1110 ;; ideal way, as (1) definitions typed in at the REPL
1111 ;; without an INLINE declaration will give a NULL
1112 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1113 ;; and (2) INLINE declarations will yield a
1114 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1115 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1116 ;; 2002-07-02
1118 ;; (2) is probably fairly easy to fix -- it is, after all,
1119 ;; a matter of list manipulation (or possibly of teaching
1120 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1121 ;; significantly harder, as the association between
1122 ;; function object and source is a tricky one.
1124 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1125 ;; non-NULL list) when the function in question has been
1126 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1127 ;; work when it has been compiled as part of the top-level
1128 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1129 ;; ...). -- CSR, 2002-11-02
1130 (when (core-object-p *compile-object*)
1131 (fix-core-source-info *source-info* *compile-object* result))
1133 (mapc #'clear-ir1-info components-from-dfo)
1134 (clear-stuff)))))))
1136 (defun process-toplevel-cold-fset (name lambda-expression path)
1137 (unless (producing-fasl-file)
1138 (error "can't COLD-FSET except in a fasl file"))
1139 (legal-fun-name-or-type-error name)
1140 (fasl-dump-cold-fset name
1141 (%compile lambda-expression
1142 *compile-object*
1143 :name name
1144 :path path)
1145 *compile-object*)
1146 (values))
1148 (defun note-top-level-form (form &optional finalp)
1149 (when *compile-print*
1150 (cond ((not *top-level-form-noted*)
1151 (let ((*print-length* 2)
1152 (*print-level* 2)
1153 (*print-pretty* nil))
1154 (with-compiler-io-syntax
1155 (compiler-mumble "~&; ~:[compiling~;converting~] ~S"
1156 *block-compile* form)))
1157 form)
1158 ((and finalp
1159 (eq :top-level-forms *compile-print*)
1160 (neq form *top-level-form-noted*))
1161 (let ((*print-length* 1)
1162 (*print-level* 1)
1163 (*print-pretty* nil))
1164 (with-compiler-io-syntax
1165 (compiler-mumble "~&; ... top level ~S" form)))
1166 form)
1168 *top-level-form-noted*))))
1170 ;;; Process a top level FORM with the specified source PATH.
1171 ;;; * If this is a magic top level form, then do stuff.
1172 ;;; * If this is a macro, then expand it.
1173 ;;; * Otherwise, just compile it.
1175 ;;; COMPILE-TIME-TOO is as defined in ANSI
1176 ;;; "3.2.3.1 Processing of Top Level Forms".
1177 (defun process-toplevel-form (form path compile-time-too)
1178 (declare (list path))
1180 (catch 'process-toplevel-form-error-abort
1181 (let* ((path (or (get-source-path form) (cons form path)))
1182 (*compiler-error-bailout*
1183 (lambda (&optional condition)
1184 (convert-and-maybe-compile
1185 (make-compiler-error-form condition form)
1186 path)
1187 (throw 'process-toplevel-form-error-abort nil))))
1189 (flet ((default-processor (form)
1190 (let ((*top-level-form-noted* (note-top-level-form form)))
1191 ;; When we're cross-compiling, consider: what should we
1192 ;; do when we hit e.g.
1193 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1194 ;; (DEFUN FOO (X) (+ 7 X)))?
1195 ;; DEFUN has a macro definition in the cross-compiler,
1196 ;; and a different macro definition in the target
1197 ;; compiler. The only sensible thing is to use the
1198 ;; target compiler's macro definition, since the
1199 ;; cross-compiler's macro is in general into target
1200 ;; functions which can't meaningfully be executed at
1201 ;; cross-compilation time. So make sure we do the EVAL
1202 ;; here, before we macroexpand.
1204 ;; Then things get even dicier with something like
1205 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1206 ;; where we have to make sure that we don't uncross
1207 ;; the SB!XC: prefix before we do EVAL, because otherwise
1208 ;; we'd be trying to redefine the cross-compilation host's
1209 ;; constants.
1211 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1212 #+sb-xc-host
1213 (progn
1214 (when compile-time-too
1215 (eval form)) ; letting xc host EVAL do its own macroexpansion
1216 (let* (;; (We uncross the operator name because things
1217 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1218 ;; should be equivalent to their CL: counterparts
1219 ;; when being compiled as target code. We leave
1220 ;; the rest of the form uncrossed because macros
1221 ;; might yet expand into EVAL-WHEN stuff, and
1222 ;; things inside EVAL-WHEN can't be uncrossed
1223 ;; until after we've EVALed them in the
1224 ;; cross-compilation host.)
1225 (slightly-uncrossed (cons (uncross (first form))
1226 (rest form)))
1227 (expanded (preprocessor-macroexpand-1
1228 slightly-uncrossed)))
1229 (if (eq expanded slightly-uncrossed)
1230 ;; (Now that we're no longer processing toplevel
1231 ;; forms, and hence no longer need to worry about
1232 ;; EVAL-WHEN, we can uncross everything.)
1233 (convert-and-maybe-compile expanded path)
1234 ;; (We have to demote COMPILE-TIME-TOO to NIL
1235 ;; here, no matter what it was before, since
1236 ;; otherwise we'd tend to EVAL subforms more than
1237 ;; once, because of WHEN COMPILE-TIME-TOO form
1238 ;; above.)
1239 (process-toplevel-form expanded path nil))))
1240 ;; When we're not cross-compiling, we only need to
1241 ;; macroexpand once, so we can follow the 1-thru-6
1242 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1243 ;; Top Level Forms".
1244 #-sb-xc-host
1245 (let ((expanded
1246 (let ((*current-path* path))
1247 (preprocessor-macroexpand-1 form))))
1248 (cond ((eq expanded form)
1249 (when compile-time-too
1250 (eval-in-lexenv form *lexenv*))
1251 (convert-and-maybe-compile form path))
1253 (process-toplevel-form expanded
1254 path
1255 compile-time-too)))))))
1256 (if (atom form)
1257 #+sb-xc-host
1258 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1259 ;; (1) SBCL gets smart enough to handle global
1260 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1261 ;; implementors start using symbol macros in a way which
1262 ;; interacts with SB-XC/CL distinction.)
1263 (convert-and-maybe-compile form path)
1264 #-sb-xc-host
1265 (default-processor form)
1266 (flet ((need-at-least-one-arg (form)
1267 (unless (cdr form)
1268 (compiler-error "~S form is too short: ~S"
1269 (car form)
1270 form))))
1271 (case (car form)
1272 ;; In the cross-compiler, top level COLD-FSET arranges
1273 ;; for static linking at cold init time.
1274 #+sb-xc-host
1275 ((cold-fset)
1276 (aver (not compile-time-too))
1277 (destructuring-bind (cold-fset fun-name lambda-expression) form
1278 (declare (ignore cold-fset))
1279 (process-toplevel-cold-fset fun-name
1280 lambda-expression
1281 path)))
1282 ((eval-when macrolet symbol-macrolet);things w/ 1 arg before body
1283 (need-at-least-one-arg form)
1284 (destructuring-bind (special-operator magic &rest body) form
1285 (ecase special-operator
1286 ((eval-when)
1287 ;; CT, LT, and E here are as in Figure 3-7 of ANSI
1288 ;; "3.2.3.1 Processing of Top Level Forms".
1289 (multiple-value-bind (ct lt e)
1290 (parse-eval-when-situations magic)
1291 (let ((new-compile-time-too (or ct
1292 (and compile-time-too
1293 e))))
1294 (cond (lt (process-toplevel-progn
1295 body path new-compile-time-too))
1296 (new-compile-time-too (eval-in-lexenv
1297 `(progn ,@body)
1298 *lexenv*))))))
1299 ((macrolet)
1300 (funcall-in-macrolet-lexenv
1301 magic
1302 (lambda (&key funs prepend)
1303 (declare (ignore funs))
1304 (aver (null prepend))
1305 (process-toplevel-locally body
1306 path
1307 compile-time-too))
1308 :compile))
1309 ((symbol-macrolet)
1310 (funcall-in-symbol-macrolet-lexenv
1311 magic
1312 (lambda (&key vars prepend)
1313 (aver (null prepend))
1314 (process-toplevel-locally body
1315 path
1316 compile-time-too
1317 :vars vars))
1318 :compile)))))
1319 ((locally)
1320 (process-toplevel-locally (rest form) path compile-time-too))
1321 ((progn)
1322 (process-toplevel-progn (rest form) path compile-time-too))
1323 (t (default-processor form))))))))
1325 (values))
1327 ;;;; load time value support
1328 ;;;;
1329 ;;;; (See EMIT-MAKE-LOAD-FORM.)
1331 ;;; Return T if we are currently producing a fasl file and hence
1332 ;;; constants need to be dumped carefully.
1333 (defun producing-fasl-file ()
1334 (fasl-output-p *compile-object*))
1336 ;;; Compile FORM and arrange for it to be called at load-time. Return
1337 ;;; the dumper handle and our best guess at the type of the object.
1338 (defun compile-load-time-value (form)
1339 (let ((lambda (compile-load-time-stuff form t)))
1340 (values
1341 (fasl-dump-load-time-value-lambda lambda *compile-object*)
1342 (let ((type (leaf-type lambda)))
1343 (if (fun-type-p type)
1344 (single-value-type (fun-type-returns type))
1345 *wild-type*)))))
1347 ;;; Compile the FORMS and arrange for them to be called (for effect,
1348 ;;; not value) at load time.
1349 (defun compile-make-load-form-init-forms (forms)
1350 (let ((lambda (compile-load-time-stuff `(progn ,@forms) nil)))
1351 (fasl-dump-toplevel-lambda-call lambda *compile-object*)))
1353 ;;; Do the actual work of COMPILE-LOAD-TIME-VALUE or
1354 ;;; COMPILE-MAKE-LOAD-FORM-INIT-FORMS.
1355 (defun compile-load-time-stuff (form for-value)
1356 (with-ir1-namespace
1357 (let* ((*lexenv* (make-null-lexenv))
1358 (lambda (ir1-toplevel form *current-path* for-value nil)))
1359 (compile-toplevel (list lambda) t)
1360 lambda)))
1362 ;;; This is called by COMPILE-TOPLEVEL when it was passed T for
1363 ;;; LOAD-TIME-VALUE-P (which happens in COMPILE-LOAD-TIME-STUFF). We
1364 ;;; don't try to combine this component with anything else and frob
1365 ;;; the name. If not in a :TOPLEVEL component, then don't bother
1366 ;;; compiling, because it was merged with a run-time component.
1367 (defun compile-load-time-value-lambda (lambdas)
1368 (aver (null (cdr lambdas)))
1369 (let* ((lambda (car lambdas))
1370 (component (lambda-component lambda)))
1371 (when (eql (component-kind component) :toplevel)
1372 (setf (component-name component) (leaf-debug-name lambda))
1373 (compile-component component)
1374 (clear-ir1-info component))))
1376 ;;;; COMPILE-FILE
1378 (defun object-call-toplevel-lambda (tll)
1379 (declare (type functional tll))
1380 (let ((object *compile-object*))
1381 (etypecase object
1382 (fasl-output (fasl-dump-toplevel-lambda-call tll object))
1383 (core-object (core-call-toplevel-lambda tll object))
1384 (null))))
1386 ;;; Smash LAMBDAS into a single component, compile it, and arrange for
1387 ;;; the resulting function to be called.
1388 (defun sub-compile-toplevel-lambdas (lambdas)
1389 (declare (list lambdas))
1390 (when lambdas
1391 (multiple-value-bind (component tll) (merge-toplevel-lambdas lambdas)
1392 (compile-component component)
1393 (clear-ir1-info component)
1394 (object-call-toplevel-lambda tll)))
1395 (values))
1397 ;;; Compile top level code and call the top level lambdas. We pick off
1398 ;;; top level lambdas in non-top-level components here, calling
1399 ;;; SUB-c-t-l-l on each subsequence of normal top level lambdas.
1400 (defun compile-toplevel-lambdas (lambdas)
1401 (declare (list lambdas))
1402 (let ((len (length lambdas)))
1403 (flet ((loser (start)
1404 (or (position-if (lambda (x)
1405 (not (eq (component-kind
1406 (node-component (lambda-bind x)))
1407 :toplevel)))
1408 lambdas
1409 ;; this used to read ":start start", but
1410 ;; start can be greater than len, which
1411 ;; is an error according to ANSI - CSR,
1412 ;; 2002-04-25
1413 :start (min start len))
1414 len)))
1415 (do* ((start 0 (1+ loser))
1416 (loser (loser start) (loser start)))
1417 ((>= start len))
1418 (sub-compile-toplevel-lambdas (subseq lambdas start loser))
1419 (unless (= loser len)
1420 (object-call-toplevel-lambda (elt lambdas loser))))))
1421 (values))
1423 ;;; Compile LAMBDAS (a list of CLAMBDAs for top level forms) into the
1424 ;;; object file.
1426 ;;; LOAD-TIME-VALUE-P seems to control whether it's MAKE-LOAD-FORM and
1427 ;;; COMPILE-LOAD-TIME-VALUE stuff. -- WHN 20000201
1428 (defun compile-toplevel (lambdas load-time-value-p)
1429 (declare (list lambdas))
1431 (maybe-mumble "locall ")
1432 (locall-analyze-clambdas-until-done lambdas)
1434 (maybe-mumble "IDFO ")
1435 (multiple-value-bind (components top-components hairy-top)
1436 (find-initial-dfo lambdas)
1437 (let ((all-components (append components top-components)))
1438 (when *check-consistency*
1439 (maybe-mumble "[check]~%")
1440 (check-ir1-consistency all-components))
1442 (dolist (component (append hairy-top top-components))
1443 (pre-physenv-analyze-toplevel component))
1445 (dolist (component components)
1446 (compile-component component)
1447 (replace-toplevel-xeps component))
1449 (when *check-consistency*
1450 (maybe-mumble "[check]~%")
1451 (check-ir1-consistency all-components))
1453 (if load-time-value-p
1454 (compile-load-time-value-lambda lambdas)
1455 (compile-toplevel-lambdas lambdas))
1457 (mapc #'clear-ir1-info components)
1458 (clear-stuff)))
1459 (values))
1461 ;;; Actually compile any stuff that has been queued up for block
1462 ;;; compilation.
1463 (defun finish-block-compilation ()
1464 (when *block-compile*
1465 (when *compile-print*
1466 (compiler-mumble "~&; block compiling converted top level forms..."))
1467 (when *toplevel-lambdas*
1468 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1469 (setq *toplevel-lambdas* ()))
1470 (setq *block-compile* nil)
1471 (setq *entry-points* nil)))
1473 (defun handle-condition-p (condition)
1474 (let ((lexenv
1475 (etypecase *compiler-error-context*
1476 (node
1477 (node-lexenv *compiler-error-context*))
1478 (compiler-error-context
1479 (let ((lexenv (compiler-error-context-lexenv
1480 *compiler-error-context*)))
1481 (aver lexenv)
1482 lexenv))
1483 (null *lexenv*))))
1484 (let ((muffles (lexenv-handled-conditions lexenv)))
1485 (if (null muffles) ; common case
1487 (dolist (muffle muffles nil)
1488 (destructuring-bind (typespec . restart-name) muffle
1489 (when (and (typep condition typespec)
1490 (find-restart restart-name condition))
1491 (return t))))))))
1493 (defun handle-condition-handler (condition)
1494 (let ((lexenv
1495 (etypecase *compiler-error-context*
1496 (node
1497 (node-lexenv *compiler-error-context*))
1498 (compiler-error-context
1499 (let ((lexenv (compiler-error-context-lexenv
1500 *compiler-error-context*)))
1501 (aver lexenv)
1502 lexenv))
1503 (null *lexenv*))))
1504 (let ((muffles (lexenv-handled-conditions lexenv)))
1505 (aver muffles)
1506 (dolist (muffle muffles (bug "fell through"))
1507 (destructuring-bind (typespec . restart-name) muffle
1508 (when (typep condition typespec)
1509 (awhen (find-restart restart-name condition)
1510 (invoke-restart it))))))))
1512 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1513 ;;; Return (VALUES ABORT-P WARNINGS-P FAILURE-P).
1514 (defun sub-compile-file (info)
1515 (declare (type source-info info))
1516 (let ((*package* (sane-package))
1517 (*readtable* *readtable*)
1518 (sb!xc:*compile-file-pathname* nil) ; really bound in
1519 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1520 (*policy* *policy*)
1521 (*code-coverage-records* (make-hash-table :test 'equal))
1522 (*code-coverage-blocks* (make-hash-table :test 'equal))
1523 (*handled-conditions* *handled-conditions*)
1524 (*disabled-package-locks* *disabled-package-locks*)
1525 (*lexenv* (make-null-lexenv))
1526 (*block-compile* *block-compile-arg*)
1527 (*toplevel-lambdas* ())
1528 (*fun-names-in-this-file* ())
1529 (*allow-instrumenting* nil)
1530 (*compiler-error-bailout*
1531 (lambda ()
1532 (compiler-mumble "~2&; fatal error, aborting compilation~%")
1533 (return-from sub-compile-file (values t t t))))
1534 (*current-path* nil)
1535 (*last-source-context* nil)
1536 (*last-original-source* nil)
1537 (*last-source-form* nil)
1538 (*last-format-string* nil)
1539 (*last-format-args* nil)
1540 (*last-message-count* 0)
1541 ;; FIXME: Do we need this rebinding here? It's a literal
1542 ;; translation of the old CMU CL rebinding to
1543 ;; (OR *BACKEND-INFO-ENVIRONMENT* *INFO-ENVIRONMENT*),
1544 ;; and it's not obvious whether the rebinding to itself is
1545 ;; needed that SBCL doesn't need *BACKEND-INFO-ENVIRONMENT*.
1546 (*info-environment* *info-environment*)
1547 (*compiler-sset-counter* 0)
1548 (*gensym-counter* 0))
1549 (handler-case
1550 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1551 (with-compilation-values
1552 (sb!xc:with-compilation-unit ()
1553 (clear-stuff)
1555 (sub-sub-compile-file info)
1557 (unless (zerop (hash-table-count *code-coverage-records*))
1558 ;; Dump the code coverage records into the fasl.
1559 (fopcompile `(record-code-coverage
1560 ',(namestring *compile-file-pathname*)
1561 ',(let (list)
1562 (maphash (lambda (k v)
1563 (declare (ignore k))
1564 (push v list))
1565 *code-coverage-records*)
1566 list))
1568 nil))
1570 (finish-block-compilation)
1571 (let ((object *compile-object*))
1572 (etypecase object
1573 (fasl-output (fasl-dump-source-info info object))
1574 (core-object (fix-core-source-info info object))
1575 (null)))
1576 nil)))
1577 ;; Some errors are sufficiently bewildering that we just fail
1578 ;; immediately, without trying to recover and compile more of
1579 ;; the input file.
1580 (fatal-compiler-error (condition)
1581 (signal condition)
1582 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
1583 (format *error-output*
1584 "~@<compilation aborted because of fatal error: ~2I~_~A~:>"
1585 condition))
1586 (finish-output *error-output*)
1587 (values t t t)))))
1589 ;;; Return a pathname for the named file. The file must exist.
1590 (defun verify-source-file (pathname-designator)
1591 (let* ((pathname (pathname pathname-designator))
1592 (default-host (make-pathname :host (pathname-host pathname))))
1593 (flet ((try-with-type (path type error-p)
1594 (let ((new (merge-pathnames
1595 path (make-pathname :type type
1596 :defaults default-host))))
1597 (if (probe-file new)
1599 (and error-p (truename new))))))
1600 (cond ((typep pathname 'logical-pathname)
1601 (try-with-type pathname "LISP" t))
1602 ((probe-file pathname) pathname)
1603 ((try-with-type pathname "lisp" nil))
1604 ((try-with-type pathname "lisp" t))))))
1606 (defun elapsed-time-to-string (tsec)
1607 (multiple-value-bind (tmin sec) (truncate tsec 60)
1608 (multiple-value-bind (thr min) (truncate tmin 60)
1609 (format nil "~D:~2,'0D:~2,'0D" thr min sec))))
1611 ;;; Print some junk at the beginning and end of compilation.
1612 (defun print-compile-start-note (source-info)
1613 (declare (type source-info source-info))
1614 (let ((file-info (source-info-file-info source-info)))
1615 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1616 (namestring (file-info-name file-info))
1617 (sb!int:format-universal-time nil
1618 (file-info-write-date
1619 file-info)
1620 :style :government
1621 :print-weekday nil
1622 :print-timezone nil)))
1623 (values))
1625 (defun print-compile-end-note (source-info won)
1626 (declare (type source-info source-info))
1627 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1629 (elapsed-time-to-string
1630 (- (get-universal-time)
1631 (source-info-start-time source-info))))
1632 (values))
1634 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1635 ;;; out of the compile, then abort the writing of the output file, so
1636 ;;; that we don't overwrite it with known garbage.
1637 (defun sb!xc:compile-file
1638 (input-file
1639 &key
1641 ;; ANSI options
1642 (output-file (cfp-output-file-default input-file))
1643 ;; FIXME: ANSI doesn't seem to say anything about
1644 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1645 ;; function..
1646 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1647 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1648 (external-format :default)
1650 ;; extensions
1651 (trace-file nil)
1652 ((:block-compile *block-compile-arg*) nil))
1653 #!+sb-doc
1654 "Compile INPUT-FILE, producing a corresponding fasl file and
1655 returning its filename.
1657 :PRINT
1658 If true, a message per non-macroexpanded top level form is printed
1659 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1660 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1661 no such message, but their subforms do.
1663 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1664 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1665 For example, compiling an IN-PACKAGE form will result in a message about
1666 a top level SETQ in addition to the message about the IN-PACKAGE form'
1667 itself.
1669 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1671 :BLOCK-COMPILE
1672 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1673 argument, it is not currently supported. (non-standard)
1675 :TRACE-FILE
1676 If given, internal data structures are dumped to the specified
1677 file, or if a value of T is given, to a file of *.trace type
1678 derived from the input file name. (non-standard)"
1679 ;;; Block compilation is currently broken.
1681 "Also, as a workaround for vaguely-non-ANSI behavior, the
1682 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1683 multiple functions are compiled together as a unit, resolving function
1684 references at compile time. NIL means that global function names are
1685 never resolved at compilation time. Currently NIL is the default
1686 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1687 of the ANSI spec allows this behavior under all circumstances, the
1688 compiler's runtime scales badly when it tries to do this for large
1689 files. If/when this performance problem is fixed, the block
1690 compilation default behavior will probably be made dependent on the
1691 SPEED and COMPILATION-SPEED optimization values, and the
1692 :BLOCK-COMPILE argument will probably become deprecated."
1694 (let* ((fasl-output nil)
1695 (output-file-name nil)
1696 (abort-p t)
1697 (warnings-p nil)
1698 (failure-p t) ; T in case error keeps this from being set later
1699 (input-pathname (verify-source-file input-file))
1700 (source-info (make-file-source-info input-pathname external-format))
1701 (*compiler-trace-output* nil)) ; might be modified below
1703 (unwind-protect
1704 (progn
1705 (when output-file
1706 (setq output-file-name
1707 (sb!xc:compile-file-pathname input-file
1708 :output-file output-file))
1709 (setq fasl-output
1710 (open-fasl-output output-file-name
1711 (namestring input-pathname))))
1712 (when trace-file
1713 (let* ((default-trace-file-pathname
1714 (make-pathname :type "trace" :defaults input-pathname))
1715 (trace-file-pathname
1716 (if (eql trace-file t)
1717 default-trace-file-pathname
1718 (merge-pathnames trace-file
1719 default-trace-file-pathname))))
1720 (setf *compiler-trace-output*
1721 (open trace-file-pathname
1722 :if-exists :supersede
1723 :direction :output))))
1725 (when sb!xc:*compile-verbose*
1726 (print-compile-start-note source-info))
1728 (let ((*compile-object* fasl-output))
1729 (setf (values abort-p warnings-p failure-p)
1730 (sub-compile-file source-info))))
1732 (close-source-info source-info)
1734 (when fasl-output
1735 (close-fasl-output fasl-output abort-p)
1736 (setq output-file-name
1737 (pathname (fasl-output-stream fasl-output)))
1738 (when (and (not abort-p) sb!xc:*compile-verbose*)
1739 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1741 (when sb!xc:*compile-verbose*
1742 (print-compile-end-note source-info (not abort-p)))
1744 (when *compiler-trace-output*
1745 (close *compiler-trace-output*)))
1747 ;; CLHS says that the first value is NIL if the "file could not
1748 ;; be created". We interpret this to mean "a valid fasl could not
1749 ;; be created" -- which can happen if the compilation is aborted
1750 ;; before the whole file has been processed, due to eg. a reader
1751 ;; error.
1752 (values (when (and (not abort-p) output-file)
1753 ;; Hack around filesystem race condition...
1754 (or (probe-file output-file-name) output-file-name))
1755 warnings-p
1756 failure-p)))
1758 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1759 ;;; the OUTPUT-FILE argument
1761 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1762 ;;; that results from merging the INPUT-FILE with the value of
1763 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1764 ;;; default to the appropriate implementation-defined default type for
1765 ;;; compiled files.
1766 (defun cfp-output-file-default (input-file)
1767 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1768 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1769 retyped))
1771 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1772 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1773 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1774 ;;; pathname, it is translated into a physical pathname as if by
1775 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1776 ;;; So I haven't really tried to make this precisely ANSI-compatible
1777 ;;; at the level of e.g. whether it returns logical pathname or a
1778 ;;; physical pathname. Patches to make it more correct are welcome.
1779 ;;; -- WHN 2000-12-09
1780 (defun sb!xc:compile-file-pathname (input-file
1781 &key
1782 (output-file nil output-file-p)
1783 &allow-other-keys)
1784 #!+sb-doc
1785 "Return a pathname describing what file COMPILE-FILE would write to given
1786 these arguments."
1787 (if output-file-p
1788 (merge-pathnames output-file (cfp-output-file-default input-file))
1789 (cfp-output-file-default input-file)))
1791 ;;;; MAKE-LOAD-FORM stuff
1793 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1794 ;;; finds a constant structure, it invokes this to arrange for proper
1795 ;;; dumping. If it turns out that the constant has already been
1796 ;;; dumped, then we don't need to do anything.
1798 ;;; If the constant hasn't been dumped, then we check to see whether
1799 ;;; we are in the process of creating it. We detect this by
1800 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1801 ;;; the constants we are in the process of creating. Actually, each
1802 ;;; entry is a list of the constant and any init forms that need to be
1803 ;;; processed on behalf of that constant.
1805 ;;; It's not necessarily an error for this to happen. If we are
1806 ;;; processing the init form for some object that showed up *after*
1807 ;;; the original reference to this constant, then we just need to
1808 ;;; defer the processing of that init form. To detect this, we
1809 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1810 ;;; constants created since the last time we started processing an
1811 ;;; init form. If the constant passed to emit-make-load-form shows up
1812 ;;; in this list, then there is a circular chain through creation
1813 ;;; forms, which is an error.
1815 ;;; If there is some intervening init form, then we blow out of
1816 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1817 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1818 ;;; offending init form can be tacked onto the init forms for the
1819 ;;; circular object.
1821 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1822 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1823 ;;; whether the creation form is the magic value
1824 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1825 ;;; dumper will eventually get its hands on the object and use the
1826 ;;; normal structure dumping noise on it.
1828 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1829 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1830 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1831 ;;; dumper to use that result instead whenever it sees this constant.
1833 ;;; Now we try to compile the init form. We bind
1834 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1835 ;;; form (and any init forms that were added because of circularity
1836 ;;; detection). If this works, great. If not, we add the init forms to
1837 ;;; the init forms for the object that caused the problems and let it
1838 ;;; deal with it.
1839 (defvar *constants-being-created* nil)
1840 (defvar *constants-created-since-last-init* nil)
1841 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1842 (defun emit-make-load-form (constant &optional (name nil namep))
1843 (aver (fasl-output-p *compile-object*))
1844 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1845 ;; KLUDGE: This special hack is because I was too lazy
1846 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1847 ;; function of LAYOUT returns nontrivial forms when
1848 ;; building the cross-compiler but :IGNORE-IT when
1849 ;; cross-compiling or running under the target Lisp. --
1850 ;; WHN 19990914
1851 #+sb-xc-host (typep constant 'layout))
1852 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1853 (when circular-ref
1854 (when (find constant *constants-created-since-last-init* :test #'eq)
1855 (throw constant t))
1856 (throw 'pending-init circular-ref)))
1857 (multiple-value-bind (creation-form init-form)
1858 (if namep
1859 ;; If the constant is a reference to a named constant, we can
1860 ;; just use SYMBOL-VALUE during LOAD.
1861 (values `(symbol-value ',name) nil)
1862 (handler-case
1863 (sb!xc:make-load-form constant (make-null-lexenv))
1864 (error (condition)
1865 (compiler-error condition))))
1866 (case creation-form
1867 (:sb-just-dump-it-normally
1868 (fasl-validate-structure constant *compile-object*)
1870 (:ignore-it
1871 nil)
1873 (let* ((name (write-to-string constant :level 1 :length 2))
1874 (info (if init-form
1875 (list constant name init-form)
1876 (list constant))))
1877 (let ((*constants-being-created*
1878 (cons info *constants-being-created*))
1879 (*constants-created-since-last-init*
1880 (cons constant *constants-created-since-last-init*)))
1881 (when
1882 (catch constant
1883 (fasl-note-handle-for-constant
1884 constant
1885 (compile-load-time-value
1886 creation-form)
1887 *compile-object*)
1888 nil)
1889 (compiler-error "circular references in creation form for ~S"
1890 constant)))
1891 (when (cdr info)
1892 (let* ((*constants-created-since-last-init* nil)
1893 (circular-ref
1894 (catch 'pending-init
1895 (loop for (name form) on (cdr info) by #'cddr
1896 collect name into names
1897 collect form into forms
1898 finally (compile-make-load-form-init-forms forms))
1899 nil)))
1900 (when circular-ref
1901 (setf (cdr circular-ref)
1902 (append (cdr circular-ref) (cdr info))))))))))))
1905 ;;;; Host compile time definitions
1906 #+sb-xc-host
1907 (defun compile-in-lexenv (name lambda lexenv)
1908 (declare (ignore lexenv))
1909 (compile name lambda))
1911 #+sb-xc-host
1912 (defun eval-in-lexenv (form lexenv)
1913 (declare (ignore lexenv))
1914 (eval form))