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