Delete a ton of cruft from the globaldb initialization logic.
[sbcl.git] / src / compiler / main.lisp
blob57142af36d85548c95893c01f0b651ef9abed3ff
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 *free-funs* *source-paths*
19 *continuation-number* *continuation-numbers*
20 *number-continuations* *tn-id* *tn-ids* *id-tns*
21 *label-ids* *label-id* *id-labels*
22 *undefined-warnings* *compiler-error-count*
23 *compiler-warning-count* *compiler-style-warning-count*
24 *compiler-note-count*
25 *compiler-error-bailout*
26 #!+sb-show *compiler-trace-output*
27 *last-source-context* *last-original-source*
28 *last-source-form* *last-format-string* *last-format-args*
29 *last-message-count* *last-error-context*
30 *lexenv* *fun-names-in-this-file*
31 *allow-instrumenting*))
33 ;;; Whether reference to a thing which cannot be defined causes a full
34 ;;; warning.
35 (defvar *flame-on-necessarily-undefined-thing* nil)
37 (defvar *check-consistency* nil)
39 ;;; Set to NIL to disable loop analysis for register allocation.
40 (defvar *loop-analyze* t)
42 ;;; Bind this to a stream to capture various internal debugging output.
43 (defvar *compiler-trace-output* nil)
45 ;;; The current block compilation state. These are initialized to the
46 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
47 ;;; called with.
48 ;;;
49 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
50 ;;; argument, which overrides any internal declarations.
51 (defvar *block-compile*)
52 (defvar *block-compile-arg*)
53 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
54 (defvar *entry-points*)
55 (declaim (list *entry-points*))
57 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
58 ;;; lambdas resulting from compiling subforms. (In reverse order.)
59 (defvar *toplevel-lambdas*)
60 (declaim (list *toplevel-lambdas*))
62 ;;; The current non-macroexpanded toplevel form as printed when
63 ;;; *compile-print* is true.
64 (defvar *top-level-form-noted* nil)
66 (defvar sb!xc:*compile-verbose* t
67 #!+sb-doc
68 "The default for the :VERBOSE argument to COMPILE-FILE.")
69 (defvar sb!xc:*compile-print* t
70 #!+sb-doc
71 "The default for the :PRINT argument to COMPILE-FILE.")
72 (defvar *compile-progress* nil
73 #!+sb-doc
74 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
75 information about the phases of compilation of each function. (This
76 is useful mainly in large block compilations.)")
78 (defvar sb!xc:*compile-file-pathname* nil
79 #!+sb-doc
80 "The defaulted pathname of the file currently being compiled, or NIL if not
81 compiling.")
82 (defvar sb!xc:*compile-file-truename* nil
83 #!+sb-doc
84 "The TRUENAME of the file currently being compiled, or NIL if not
85 compiling.")
87 (declaim (type (or pathname null)
88 sb!xc:*compile-file-pathname*
89 sb!xc:*compile-file-truename*))
91 ;;; the SOURCE-INFO structure for the current compilation. This is
92 ;;; null globally to indicate that we aren't currently in any
93 ;;; identifiable compilation.
94 (defvar *source-info* nil)
96 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
97 ;;; normally causes nested uses to be no-ops).
98 (defvar *in-compilation-unit* nil)
100 ;;; Count of the number of compilation units dynamically enclosed by
101 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
102 (defvar *aborted-compilation-unit-count*)
104 ;;; Mumble conditional on *COMPILE-PROGRESS*.
105 (defun maybe-mumble (&rest foo)
106 (when *compile-progress*
107 (compiler-mumble "~&")
108 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
109 (apply #'compiler-mumble foo))))
111 (deftype object () '(or fasl-output core-object null))
113 (defvar *compile-object* nil)
114 (declaim (type object *compile-object*))
115 (defvar *compile-toplevel-object* nil)
117 (defvar *emit-cfasl* nil)
119 (defvar *fopcompile-label-counter*)
121 ;; Used during compilation to map code paths to the matching
122 ;; instrumentation conses.
123 (defvar *code-coverage-records* nil)
124 ;; Used during compilation to keep track of with source paths have been
125 ;; instrumented in which blocks.
126 (defvar *code-coverage-blocks* nil)
127 ;; Stores the code coverage instrumentation results. Keys are namestrings,
128 ;; the value is a list of (CONS PATH STATE), where STATE is NIL for
129 ;; a path that has not been visited, and T for one that has.
130 (defvar *code-coverage-info* (make-hash-table :test 'equal))
133 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
135 (defmacro sb!xc:with-compilation-unit (options &body body)
136 #!+sb-doc
137 "Affects compilations that take place within its dynamic extent. It is
138 intended to be eg. wrapped around the compilation of all files in the same system.
140 Following options are defined:
142 :OVERRIDE Boolean-Form
143 One of the effects of this form is to delay undefined warnings until the
144 end of the form, instead of giving them at the end of each compilation.
145 If OVERRIDE is NIL (the default), then the outermost
146 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
147 OVERRIDE true causes that form to grab any enclosed warnings, even if it
148 is enclosed by another WITH-COMPILATION-UNIT.
150 :POLICY Optimize-Declaration-Form
151 Provides dynamic scoping for global compiler optimization qualities and
152 restrictions, limiting effects of subsequent OPTIMIZE proclamations and
153 calls to SB-EXT:RESTRICT-COMPILER-POLICY to the dynamic scope of BODY.
155 If OVERRIDE is false, specified POLICY is merged with current global
156 policy. If OVERRIDE is true, current global policy, including any
157 restrictions, is discarded in favor of the specified POLICY.
159 Supplying POLICY NIL is equivalent to the option not being supplied at
160 all, ie. dynamic scoping of policy does not take place.
162 This option is an SBCL-specific experimental extension: Interface
163 subject to change.
165 :SOURCE-NAMESTRING Namestring-Form
166 Attaches the value returned by the Namestring-Form to the internal
167 debug-source information as the namestring of the source file. Normally
168 the namestring of the input-file for COMPILE-FILE is used: this option
169 can be used to provide source-file information for functions compiled
170 using COMPILE, or to override the input-file of COMPILE-FILE.
172 If both an outer and an inner WITH-COMPILATION-UNIT provide a
173 SOURCE-NAMESTRING, the inner one takes precedence. Unaffected
174 by :OVERRIDE.
176 This is an SBCL-specific extension.
178 :SOURCE-PLIST Plist-Form
179 Attaches the value returned by the Plist-Form to internal debug-source
180 information of functions compiled in within the dynamic extent of BODY.
182 Primarily for use by development environments, in order to eg. associate
183 function definitions with editor-buffers. Can be accessed using
184 SB-INTROSPECT:DEFINITION-SOURCE-PLIST.
186 If an outer WITH-COMPILATION-UNIT form also provide a SOURCE-PLIST, it
187 is appended to the end of the provided SOURCE-PLIST. Unaffected
188 by :OVERRIDE.
190 This is an SBCL-specific extension.
192 Examples:
194 ;; Prevent proclamations from the file leaking, and restrict
195 ;; SAFETY to 3 -- otherwise uses the current global policy.
196 (with-compilation-unit (:policy '(optimize))
197 (restrict-compiler-policy 'safety 3)
198 (load \"foo.lisp\"))
200 ;; Using default policy instead of the current global one,
201 ;; except for DEBUG 3.
202 (with-compilation-unit (:policy '(optimize debug)
203 :override t)
204 (load \"foo.lisp\"))
206 ;; Same as if :POLICY had not been specified at all: SAFETY 3
207 ;; proclamation leaks out from WITH-COMPILATION-UNIT.
208 (with-compilation-unit (:policy nil)
209 (declaim (optimize safety))
210 (load \"foo.lisp\"))
212 `(%with-compilation-unit (lambda () ,@body) ,@options))
214 (defvar *source-plist* nil)
215 (defvar *source-namestring* nil)
217 (defun %with-compilation-unit (fn &key override policy source-plist source-namestring)
218 (declare (type function fn))
219 (flet ((with-it ()
220 (let ((succeeded-p nil)
221 (*source-plist* (append source-plist *source-plist*))
222 (*source-namestring* (or source-namestring *source-namestring*)))
223 (if (and *in-compilation-unit* (not override))
224 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
225 ;; ordinarily (unless OVERRIDE) basically a no-op.
226 (unwind-protect
227 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
228 (unless succeeded-p
229 (incf *aborted-compilation-unit-count*)))
230 (let ((*aborted-compilation-unit-count* 0)
231 (*compiler-error-count* 0)
232 (*compiler-warning-count* 0)
233 (*compiler-style-warning-count* 0)
234 (*compiler-note-count* 0)
235 (*undefined-warnings* nil)
236 (*in-compilation-unit* t))
237 (handler-bind ((parse-unknown-type
238 (lambda (c)
239 (note-undefined-reference
240 (parse-unknown-type-specifier c)
241 :type))))
242 (unwind-protect
243 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
244 (unless succeeded-p
245 (incf *aborted-compilation-unit-count*))
246 (summarize-compilation-unit (not succeeded-p)))))))))
247 (if policy
248 (let ((*policy* (process-optimize-decl policy (unless override *policy*)))
249 (*policy-restrictions* (unless override *policy-restrictions*)))
250 (with-it))
251 (with-it))))
253 ;;; Is NAME something that no conforming program can rely on
254 ;;; defining?
255 (defun name-reserved-by-ansi-p (name kind)
256 (ecase kind
257 (:function
258 (eq (symbol-package (fun-name-block-name name))
259 *cl-package*))
260 (:type
261 (let ((symbol (typecase name
262 (symbol name)
263 ((cons symbol) (car name))
264 (t (return-from name-reserved-by-ansi-p nil)))))
265 (eq (symbol-package symbol) *cl-package*)))))
267 ;;; This is to be called at the end of a compilation unit. It signals
268 ;;; any residual warnings about unknown stuff, then prints the total
269 ;;; error counts. ABORT-P should be true when the compilation unit was
270 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
271 ;;; enclosed nested compilation units that were aborted.
272 (defun summarize-compilation-unit (abort-p)
273 (let (summary)
274 (unless abort-p
275 (handler-bind ((style-warning #'compiler-style-warning-handler)
276 (warning #'compiler-warning-handler))
278 (let ((undefs (sort *undefined-warnings* #'string<
279 :key (lambda (x)
280 (let ((x (undefined-warning-name x)))
281 (if (symbolp x)
282 (symbol-name x)
283 (prin1-to-string x)))))))
284 (dolist (kind '(:variable :function :type))
285 (let ((names (mapcar #'undefined-warning-name
286 (remove kind undefs :test #'neq
287 :key #'undefined-warning-kind))))
288 (when names (push (cons kind names) summary))))
289 (dolist (undef undefs)
290 (let ((name (undefined-warning-name undef))
291 (kind (undefined-warning-kind undef))
292 (warnings (undefined-warning-warnings undef))
293 (undefined-warning-count (undefined-warning-count undef)))
294 (dolist (*compiler-error-context* warnings)
295 (if #-sb-xc-host (and (member kind '(:function :type))
296 (name-reserved-by-ansi-p name kind)
297 *flame-on-necessarily-undefined-thing*)
298 #+sb-xc-host nil
299 (ecase kind
300 (:function
301 (compiler-warn
302 "~@<The function ~S is undefined, and its name is ~
303 reserved by ANSI CL so that even if it were ~
304 defined later, the code doing so would not be ~
305 portable.~:@>" name))
306 (:type
307 (if (and (consp name) (eq 'quote (car name)))
308 (compiler-warn
309 "~@<Undefined type ~S. The name starts with ~S: ~
310 probably use of a quoted type name in a context ~
311 where the name is not evaluated.~:@>"
312 name 'quote)
313 (compiler-warn
314 "~@<Undefined type ~S. Note that name ~S is ~
315 reserved by ANSI CL, so code defining a type with ~
316 that name would not be portable.~:@>" name
317 name))))
318 (if (eq kind :variable)
319 (compiler-warn "undefined ~(~A~): ~S" kind name)
320 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
321 (let ((warn-count (length warnings)))
322 (when (and warnings (> undefined-warning-count warn-count))
323 (let ((more (- undefined-warning-count warn-count)))
324 (if (eq kind :variable)
325 (compiler-warn
326 "~W more use~:P of undefined ~(~A~) ~S"
327 more kind name)
328 (compiler-style-warn
329 "~W more use~:P of undefined ~(~A~) ~S"
330 more kind name))))))))))
332 (unless (and (not abort-p)
333 (zerop *aborted-compilation-unit-count*)
334 (zerop *compiler-error-count*)
335 (zerop *compiler-warning-count*)
336 (zerop *compiler-style-warning-count*)
337 (zerop *compiler-note-count*))
338 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
339 (format *error-output* "~&compilation unit ~:[finished~;aborted~]"
340 abort-p)
341 (dolist (cell summary)
342 (destructuring-bind (kind &rest names) cell
343 (format *error-output*
344 "~& Undefined ~(~A~)~p:~
345 ~% ~{~<~% ~1:;~S~>~^ ~}"
346 kind (length names) names)))
347 (format *error-output* "~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
348 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
349 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
350 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
351 ~[~:;~:*~& printed ~W note~:P~]"
352 *aborted-compilation-unit-count*
353 *compiler-error-count*
354 *compiler-warning-count*
355 *compiler-style-warning-count*
356 *compiler-note-count*))
357 (terpri *error-output*)
358 (force-output *error-output*))))
360 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
361 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
362 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
363 ;;; This also wraps up WITH-IR1-NAMESPACE functionality.
364 (defmacro with-compilation-values (&body body)
365 ;; These bindings could just as well be in WITH-IR1-NAMESPACE, but
366 ;; since they're primarily debugging tools, it's nicer to have
367 ;; a wider unique scope by ID.
368 `(let ((*continuation-number* 0)
369 (*continuation-numbers* (make-hash-table :test 'eq))
370 (*number-continuations* (make-hash-table :test 'eql))
371 (*tn-id* 0)
372 (*tn-ids* (make-hash-table :test 'eq))
373 (*id-tns* (make-hash-table :test 'eql))
374 (*label-id* 0)
375 (*label-ids* (make-hash-table :test 'eq))
376 (*id-labels* (make-hash-table :test 'eql)))
377 (unwind-protect
378 (let ((*warnings-p* nil)
379 (*failure-p* nil))
380 (handler-bind ((compiler-error #'compiler-error-handler)
381 (style-warning #'compiler-style-warning-handler)
382 (warning #'compiler-warning-handler))
383 (values (progn ,@body)
384 *warnings-p*
385 *failure-p*)))
386 (clrhash *tn-ids*)
387 (clrhash *id-tns*)
388 (clrhash *continuation-numbers*)
389 (clrhash *number-continuations*)
390 (clrhash *label-ids*)
391 (clrhash *id-labels*))))
393 ;;;; component compilation
395 (defparameter *max-optimize-iterations* 3 ; ARB
396 #!+sb-doc
397 "The upper limit on the number of times that we will consecutively do IR1
398 optimization that doesn't introduce any new code. A finite limit is
399 necessary, since type inference may take arbitrarily long to converge.")
401 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
402 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
404 ;;; Repeatedly optimize COMPONENT until no further optimizations can
405 ;;; be found or we hit our iteration limit. When we hit the limit, we
406 ;;; clear the component and block REOPTIMIZE flags to discourage the
407 ;;; next optimization attempt from pounding on the same code.
408 (defun ir1-optimize-until-done (component)
409 (declare (type component component))
410 (maybe-mumble "opt")
411 (event ir1-optimize-until-done)
412 (let ((count 0)
413 (cleared-reanalyze nil)
414 (fastp nil))
415 (loop
416 (when (component-reanalyze component)
417 (setq count 0)
418 (setq cleared-reanalyze t)
419 (setf (component-reanalyze component) nil))
420 (setf (component-reoptimize component) nil)
421 (ir1-optimize component fastp)
422 (cond ((component-reoptimize component)
423 (incf count)
424 (when (and (>= count *max-optimize-iterations*)
425 (not (component-reanalyze component))
426 (eq (component-reoptimize component) :maybe))
427 (maybe-mumble "*")
428 (cond ((retry-delayed-ir1-transforms :optimize)
429 (maybe-mumble "+")
430 (setq count 0))
432 (event ir1-optimize-maxed-out)
433 (setf (component-reoptimize component) nil)
434 (do-blocks (block component)
435 (setf (block-reoptimize block) nil))
436 (return)))))
437 ((retry-delayed-ir1-transforms :optimize)
438 (setf count 0)
439 (maybe-mumble "+"))
441 (maybe-mumble " ")
442 (return)))
443 (setq fastp (>= count *max-optimize-iterations*))
444 (maybe-mumble (if fastp "-" ".")))
445 (when cleared-reanalyze
446 (setf (component-reanalyze component) t)))
447 (values))
449 (defparameter *constraint-propagate* t)
451 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
452 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
453 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
454 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
456 ;;; FIXME: It would be good to document why it's important to have a
457 ;;; large value here, and what the drawbacks of an excessively large
458 ;;; value are; and it might also be good to make it depend on
459 ;;; optimization policy.
460 (defparameter *reoptimize-after-type-check-max* 10)
462 (defevent reoptimize-maxed-out
463 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
465 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
466 (defun dfo-as-needed (component)
467 (declare (type component component))
468 (when (component-reanalyze component)
469 (maybe-mumble "DFO")
470 (loop
471 (find-dfo component)
472 (unless (component-reanalyze component)
473 (maybe-mumble " ")
474 (return))
475 (maybe-mumble ".")))
476 (values))
478 ;;; Do all the IR1 phases for a non-top-level component.
479 (defun ir1-phases (component)
480 (declare (type component component))
481 (aver-live-component component)
482 (let ((*constraint-universe* (make-array 64 ; arbitrary, but don't
483 ;make this 0.
484 :fill-pointer 0 :adjustable t))
485 (loop-count 1)
486 (*delayed-ir1-transforms* nil))
487 (declare (special *constraint-universe* *delayed-ir1-transforms*))
488 (loop
489 (ir1-optimize-until-done component)
490 (when (or (component-new-functionals component)
491 (component-reanalyze-functionals component))
492 (maybe-mumble "locall ")
493 (locall-analyze-component component))
494 (dfo-as-needed component)
495 (when *constraint-propagate*
496 (maybe-mumble "constraint ")
497 (constraint-propagate component))
498 (when (retry-delayed-ir1-transforms :constraint)
499 (maybe-mumble "Rtran "))
500 (flet ((want-reoptimization-p ()
501 (or (component-reoptimize component)
502 (component-reanalyze component)
503 (component-new-functionals component)
504 (component-reanalyze-functionals component))))
505 (unless (and (want-reoptimization-p)
506 ;; We delay the generation of type checks until
507 ;; the type constraints have had time to
508 ;; propagate, else the compiler can confuse itself.
509 (< loop-count (- *reoptimize-after-type-check-max* 4)))
510 (maybe-mumble "type ")
511 (generate-type-checks component)
512 (unless (want-reoptimization-p)
513 (return))))
514 (when (>= loop-count *reoptimize-after-type-check-max*)
515 (maybe-mumble "[reoptimize limit]")
516 (event reoptimize-maxed-out)
517 (return))
518 (incf loop-count)))
520 (when *check-consistency*
521 (do-blocks-backwards (block component)
522 (awhen (flush-dead-code block)
523 (let ((*compiler-error-context* it))
524 (compiler-warn "dead code detected at the end of ~S"
525 'ir1-phases)))))
527 (ir1-finalize component)
528 (values))
530 (defun %compile-component (component)
531 (let ((*code-segment* nil)
532 (*elsewhere* nil)
533 #!+inline-constants
534 (*constant-segment* nil)
535 #!+inline-constants
536 (*constant-table* nil)
537 #!+inline-constants
538 (*constant-vector* nil))
539 (maybe-mumble "GTN ")
540 (gtn-analyze component)
541 (maybe-mumble "LTN ")
542 (ltn-analyze component)
543 (dfo-as-needed component)
544 (maybe-mumble "control ")
545 (control-analyze component #'make-ir2-block)
547 (when (or (ir2-component-values-receivers (component-info component))
548 (component-dx-lvars component))
549 (maybe-mumble "stack ")
550 (find-dominators component)
551 (stack-analyze component)
552 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
553 ;; stack analysis. There shouldn't be any unreachable code after
554 ;; control, so this won't delete anything.
555 (dfo-as-needed component))
557 (unwind-protect
558 (progn
559 (maybe-mumble "IR2tran ")
560 (init-assembler)
561 (entry-analyze component)
562 (ir2-convert component)
564 (when (policy *lexenv* (>= speed compilation-speed))
565 (maybe-mumble "copy ")
566 (copy-propagate component))
568 (ir2-optimize component)
570 (select-representations component)
572 (when *check-consistency*
573 (maybe-mumble "check2 ")
574 (check-ir2-consistency component))
576 (delete-unreferenced-tns component)
578 (maybe-mumble "life ")
579 (lifetime-analyze component)
581 (when *compile-progress*
582 (compiler-mumble "") ; Sync before doing more output.
583 (pre-pack-tn-stats component *standard-output*))
585 (when *check-consistency*
586 (maybe-mumble "check-life ")
587 (check-life-consistency component))
589 (maybe-mumble "pack ")
590 (sb!regalloc:pack component)
592 (when *check-consistency*
593 (maybe-mumble "check-pack ")
594 (check-pack-consistency component))
596 (when *compiler-trace-output*
597 (describe-component component *compiler-trace-output*)
598 (describe-ir2-component component *compiler-trace-output*))
600 (maybe-mumble "code ")
601 (multiple-value-bind (code-length fixup-notes)
602 (generate-code component)
604 #-sb-xc-host
605 (when *compiler-trace-output*
606 (format *compiler-trace-output*
607 "~|~%disassembly of code for ~S~2%" component)
608 (sb!disassem:disassemble-assem-segment *code-segment*
609 *compiler-trace-output*))
611 (etypecase *compile-object*
612 (fasl-output
613 (maybe-mumble "fasl")
614 (fasl-dump-component component
615 *code-segment*
616 code-length
617 fixup-notes
618 *compile-object*))
619 (core-object
620 (maybe-mumble "core")
621 (make-core-component component
622 *code-segment*
623 code-length
624 fixup-notes
625 *compile-object*))
626 (null))))))
628 ;; We're done, so don't bother keeping anything around.
629 (setf (component-info component) :dead)
631 (values))
633 ;;; Delete components with no external entry points before we try to
634 ;;; generate code. Unreachable closures can cause IR2 conversion to
635 ;;; puke on itself, since it is the reference to the closure which
636 ;;; normally causes the components to be combined.
637 (defun delete-if-no-entries (component)
638 (dolist (fun (component-lambdas component) (delete-component component))
639 (when (functional-has-external-references-p fun)
640 (return))
641 (case (functional-kind fun)
642 (:toplevel (return))
643 (:external
644 (unless (every (lambda (ref)
645 (eq (node-component ref) component))
646 (leaf-refs fun))
647 (return))))))
649 (defun compile-component (component)
651 ;; miscellaneous sanity checks
653 ;; FIXME: These are basically pretty wimpy compared to the checks done
654 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
655 ;; make those internal consistency checks work again and use them.
656 (aver-live-component component)
657 (do-blocks (block component)
658 (aver (eql (block-component block) component)))
659 (dolist (lambda (component-lambdas component))
660 ;; sanity check to prevent weirdness from propagating insidiously as
661 ;; far from its root cause as it did in bug 138: Make sure that
662 ;; thing-to-COMPONENT links are consistent.
663 (aver (eql (lambda-component lambda) component))
664 (aver (eql (node-component (lambda-bind lambda)) component)))
666 (let* ((*component-being-compiled* component))
668 ;; Record xref information before optimization. This way the
669 ;; stored xref data reflects the real source as closely as
670 ;; possible.
671 (record-component-xrefs component)
673 (ir1-phases component)
675 (when *loop-analyze*
676 (dfo-as-needed component)
677 (find-dominators component)
678 (loop-analyze component))
681 (when (and *loop-analyze* *compiler-trace-output*)
682 (labels ((print-blocks (block)
683 (format *compiler-trace-output* " ~A~%" block)
684 (when (block-loop-next block)
685 (print-blocks (block-loop-next block))))
686 (print-loop (loop)
687 (format *compiler-trace-output* "loop=~A~%" loop)
688 (print-blocks (loop-blocks loop))
689 (dolist (l (loop-inferiors loop))
690 (print-loop l))))
691 (print-loop (component-outer-loop component))))
694 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
695 (maybe-mumble "env ")
696 (physenv-analyze component)
697 (dfo-as-needed component)
699 (delete-if-no-entries component)
701 (unless (eq (block-next (component-head component))
702 (component-tail component))
703 (%compile-component component)))
705 (clear-constant-info)
707 (values))
709 ;;;; clearing global data structures
710 ;;;;
711 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
712 ;;;; global data structures entirely when possible and consing up the
713 ;;;; others from scratch instead of clearing and reusing them?
715 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
716 ;;; addition to allowing stuff to be reclaimed, this is required for
717 ;;; correct assignment of constant offsets, since we need to assign a
718 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
719 ;;; slots, since they are used to keep track of functions across
720 ;;; component boundaries.
721 (defun clear-constant-info ()
722 (maphash (lambda (k v)
723 (declare (ignore k))
724 (setf (leaf-info v) nil)
725 (setf (constant-boxed-tn v) nil))
726 *constants*)
727 (maphash (lambda (k v)
728 (declare (ignore k))
729 (when (constant-p v)
730 (setf (leaf-info v) nil)
731 (setf (constant-boxed-tn v) nil)))
732 *free-vars*)
733 (values))
735 ;;; Blow away the REFS for all global variables, and let COMPONENT
736 ;;; be recycled.
737 (defun clear-ir1-info (component)
738 (declare (type component component))
739 (labels ((blast (x)
740 (maphash (lambda (k v)
741 (declare (ignore k))
742 (when (leaf-p v)
743 (setf (leaf-refs v)
744 (delete-if #'here-p (leaf-refs v)))
745 (when (basic-var-p v)
746 (setf (basic-var-sets v)
747 (delete-if #'here-p (basic-var-sets v))))))
749 (here-p (x)
750 (eq (node-component x) component)))
751 (blast *free-vars*)
752 (blast *free-funs*)
753 (blast *constants*))
754 (values))
756 ;;;; trace output
758 ;;; Print out some useful info about COMPONENT to STREAM.
759 (defun describe-component (component *standard-output*)
760 (declare (type component component))
761 (format t "~|~%;;;; component: ~S~2%" (component-name component))
762 (print-all-blocks component)
763 (values))
765 (defun describe-ir2-component (component *standard-output*)
766 (format t "~%~|~%;;;; IR2 component: ~S~2%" (component-name component))
767 (format t "entries:~%")
768 (dolist (entry (ir2-component-entries (component-info component)))
769 (format t "~4TL~D: ~S~:[~; [closure]~]~%"
770 (label-id (entry-info-offset entry))
771 (entry-info-name entry)
772 (entry-info-closure-tn entry)))
773 (terpri)
774 (pre-pack-tn-stats component *standard-output*)
775 (terpri)
776 (print-ir2-blocks component)
777 (terpri)
778 (values))
780 ;;;; file reading
781 ;;;;
782 ;;;; When reading from a file, we have to keep track of some source
783 ;;;; information. We also exploit our ability to back up for printing
784 ;;;; the error context and for recovering from errors.
785 ;;;;
786 ;;;; The interface we provide to this stuff is the stream-oid
787 ;;;; SOURCE-INFO structure. The bookkeeping is done as a side effect
788 ;;;; of getting the next source form.
790 ;;; A FILE-INFO structure holds all the source information for a
791 ;;; given file.
792 (def!struct (file-info
793 (:copier nil)
794 #-no-ansi-print-object
795 (:print-object (lambda (s stream)
796 (print-unreadable-object (s stream :type t)
797 (princ (file-info-name s) stream)))))
798 ;; If a file, the truename of the corresponding source file. If from
799 ;; a Lisp form, :LISP. If from a stream, :STREAM.
800 (name (missing-arg) :type (or pathname (eql :lisp)))
801 ;; the external format that we'll call OPEN with, if NAME is a file.
802 (external-format nil)
803 ;; the defaulted, but not necessarily absolute file name (i.e. prior
804 ;; to TRUENAME call.) Null if not a file. This is used to set
805 ;; *COMPILE-FILE-PATHNAME*, and if absolute, is dumped in the
806 ;; debug-info.
807 (untruename nil :type (or pathname null))
808 ;; the file's write date (if relevant)
809 (write-date nil :type (or unsigned-byte null))
810 ;; the source path root number of the first form in this file (i.e.
811 ;; the total number of forms converted previously in this
812 ;; compilation)
813 (source-root 0 :type unsigned-byte)
814 ;; parallel vectors containing the forms read out of the file and
815 ;; the file positions that reading of each form started at (i.e. the
816 ;; end of the previous form)
817 (forms (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t))
818 (positions (make-array 10 :fill-pointer 0 :adjustable t) :type (vector t)))
820 ;;; The SOURCE-INFO structure provides a handle on all the source
821 ;;; information for an entire compilation.
822 (def!struct (source-info
823 #-no-ansi-print-object
824 (:print-object (lambda (s stream)
825 (print-unreadable-object (s stream :type t))))
826 (:copier nil))
827 ;; the UT that compilation started at
828 (start-time (get-universal-time) :type unsigned-byte)
829 ;; the IRT that compilation started at
830 (start-real-time (get-internal-real-time) :type unsigned-byte)
831 ;; the FILE-INFO structure for this compilation
832 (file-info nil :type (or file-info null))
833 ;; the stream that we are using to read the FILE-INFO, or NIL if
834 ;; no stream has been opened yet
835 (stream nil :type (or stream null))
836 ;; for coalescing DEFINITION-SOURCE-LOCATION of effectively toplevel forms
837 ;; inside one truly toplevel form.
838 (last-defn-source-loc)
839 ;; if the current compilation is recursive (e.g., due to EVAL-WHEN
840 ;; processing at compile-time), the invoking compilation's
841 ;; source-info.
842 (parent nil :type (or source-info null)))
844 ;;; Given a pathname, return a SOURCE-INFO structure.
845 (defun make-file-source-info (file external-format)
846 (make-source-info
847 :file-info (make-file-info :name (truename file)
848 :untruename (merge-pathnames file)
849 :external-format external-format
850 :write-date (file-write-date file))))
852 ;;; Return a SOURCE-INFO to describe the incremental compilation of FORM.
853 (defun make-lisp-source-info (form &key parent)
854 (make-source-info
855 :file-info (make-file-info :name :lisp
856 :forms (vector form)
857 :positions '#(0))
858 :parent parent))
860 ;;; Walk up the SOURCE-INFO list until we either reach a SOURCE-INFO
861 ;;; with no parent (e.g., from a REPL evaluation) or until we reach a
862 ;;; SOURCE-INFO whose FILE-INFO denotes a file.
863 (defun get-toplevelish-file-info (&optional (source-info *source-info*))
864 (if source-info
865 (do* ((sinfo source-info (source-info-parent sinfo))
866 (finfo (source-info-file-info sinfo)
867 (source-info-file-info sinfo)))
868 ((or (not (source-info-p (source-info-parent sinfo)))
869 (pathnamep (file-info-name finfo)))
870 finfo))))
872 ;;; Return a form read from STREAM; or for EOF use the trick,
873 ;;; popularized by Kent Pitman, of returning STREAM itself. If an
874 ;;; error happens, then convert it to standard abort-the-compilation
875 ;;; error condition (possibly recording some extra location
876 ;;; information). CONDITION-NAME is what to signal on error,
877 ;;; and should be INPUT-ERROR-IN-COMPILE-FILE or a subclass of it.
878 ;;; The signaled condition encapsulates a reader condition.
879 (defun read-for-compile-file (stream position condition-name)
880 (handler-case
881 (read-preserving-whitespace stream nil stream)
882 (reader-error (condition)
883 (compiler-error condition-name
884 ;; We don't need to supply :POSITION here because
885 ;; READER-ERRORs already know their position in the file.
886 :condition condition
887 :stream stream))
888 ;; ANSI, in its wisdom, says that READ should return END-OF-FILE
889 ;; (and that this is not a READER-ERROR) when it encounters end of
890 ;; file in the middle of something it's trying to read.
891 (end-of-file (condition)
892 (compiler-error condition-name
893 :condition condition
894 ;; We need to supply :POSITION here because the END-OF-FILE
895 ;; condition doesn't carry the position that the user
896 ;; probably cares about, where the failed READ began.
897 :position position
898 :stream stream))
899 (error (condition)
900 (compiler-error condition-name
901 :condition condition
902 :position position
903 :stream stream))))
905 ;;; If STREAM is present, return it, otherwise open a stream to the
906 ;;; current file. There must be a current file.
908 ;;; FIXME: This is probably an unnecessarily roundabout way to do
909 ;;; things now that we process a single file in COMPILE-FILE (unlike
910 ;;; the old CMU CL code, which accepted multiple files). Also, the old
911 ;;; comment said
912 ;;; When we open a new file, we also reset *PACKAGE* and policy.
913 ;;; This gives the effect of rebinding around each file.
914 ;;; which doesn't seem to be true now. Check to make sure that if
915 ;;; such rebinding is necessary, it's still done somewhere.
916 (defun get-source-stream (info)
917 (declare (type source-info info))
918 (or (source-info-stream info)
919 (let* ((file-info (source-info-file-info info))
920 (name (file-info-name file-info))
921 (external-format (file-info-external-format file-info)))
922 (setf sb!xc:*compile-file-truename* name
923 sb!xc:*compile-file-pathname* (file-info-untruename file-info)
924 (source-info-stream info)
925 (open name :direction :input
926 :external-format external-format)))))
928 ;;; Close the stream in INFO if it is open.
929 (defun close-source-info (info)
930 (declare (type source-info info))
931 (let ((stream (source-info-stream info)))
932 (when stream (close stream)))
933 (setf (source-info-stream info) nil)
934 (values))
936 ;;; Loop over FORMS retrieved from INFO. Used by COMPILE-FILE and
937 ;;; LOAD when loading from a FILE-STREAM associated with a source
938 ;;; file. ON-ERROR is the name of a condition class that should
939 ;;; be signaled if anything goes wrong during a READ.
940 (defmacro do-forms-from-info (((form &rest keys) info
941 &optional (on-error ''input-error-in-load))
942 &body body)
943 (aver (symbolp form))
944 (once-only ((info info))
945 `(let ((*source-info* ,info))
946 (loop (destructuring-bind (,form &key ,@keys &allow-other-keys)
947 (let* ((file-info (source-info-file-info ,info))
948 (stream (get-source-stream ,info))
949 (pos (file-position stream))
950 (form (read-for-compile-file stream pos ,on-error)))
951 (if (eq form stream) ; i.e., if EOF
952 (return)
953 (let* ((forms (file-info-forms file-info))
954 (current-idx (+ (fill-pointer forms)
955 (file-info-source-root
956 file-info))))
957 (vector-push-extend form forms)
958 (vector-push-extend pos (file-info-positions
959 file-info))
960 (list form :current-index current-idx))))
961 ,@body)))))
963 ;;; Read and compile the source file.
964 (defun sub-sub-compile-file (info)
965 (do-forms-from-info ((form current-index) info
966 'input-error-in-compile-file)
967 (with-source-paths
968 (find-source-paths form current-index)
969 (process-toplevel-form
970 form `(original-source-start 0 ,current-index) nil)))
971 ;; It's easy to get into a situation where cold-init crashes and the only
972 ;; backtrace you get from ldb is TOP-LEVEL-FORM, which means you're anywhere
973 ;; within the 23000 or so blobs of code deferred until cold-init.
974 ;; Seeing each file finish narrows things down without the noise of :sb-show,
975 ;; but this hack messes up form positions, so it's not on unless asked for.
976 #+nil ; change to #+sb-xc-host if desired
977 (let ((file-info (get-toplevelish-file-info info)))
978 (declare (ignorable file-info))
979 (let* ((forms (file-info-forms file-info))
980 (form
981 `(write-string
982 ,(format nil "Completed TLFs: ~A~%" (file-info-name file-info))))
983 (index
984 (+ (fill-pointer forms) (file-info-source-root file-info))))
985 (with-source-paths
986 (find-source-paths form index)
987 (process-toplevel-form
988 form `(original-source-start 0 ,index) nil)))))
990 ;;; Return the INDEX'th source form read from INFO and the position
991 ;;; where it was read.
992 (defun find-source-root (index info)
993 (declare (type index index) (type source-info info))
994 (let ((file-info (source-info-file-info info)))
995 (values (aref (file-info-forms file-info) index)
996 (aref (file-info-positions file-info) index))))
998 ;;;; processing of top level forms
1000 ;;; This is called by top level form processing when we are ready to
1001 ;;; actually compile something. If *BLOCK-COMPILE* is T, then we still
1002 ;;; convert the form, but delay compilation, pushing the result on
1003 ;;; *TOPLEVEL-LAMBDAS* instead.
1004 (defun convert-and-maybe-compile (form path &optional (expand t))
1005 (declare (list path))
1006 (let ((*top-level-form-noted* (note-top-level-form form t)))
1007 ;; Don't bother to compile simple objects that just sit there.
1008 (when (and form (or (symbolp form) (consp form)))
1009 (if (fopcompilable-p form expand)
1010 (let ((*fopcompile-label-counter* 0))
1011 (fopcompile form path nil expand))
1012 (with-ir1-namespace
1013 (let ((*lexenv* (make-lexenv
1014 :policy *policy*
1015 :handled-conditions *handled-conditions*
1016 :disabled-package-locks *disabled-package-locks*))
1017 (tll (ir1-toplevel form path nil)))
1018 (if (eq *block-compile* t)
1019 (push tll *toplevel-lambdas*)
1020 (compile-toplevel (list tll) nil))
1021 nil))))))
1023 ;;; Macroexpand FORM in the current environment with an error handler.
1024 ;;; We only expand one level, so that we retain all the intervening
1025 ;;; forms in the source path. A compiler-macro takes precedence over
1026 ;;; an ordinary macro as specified in CLHS 3.2.3.1
1027 ;;; Note that this function is _only_ for processing of toplevel forms.
1028 ;;; Non-toplevel forms use IR1-CONVERT-FUNCTOID which considers compiler macros.
1029 (defun preprocessor-macroexpand-1 (form)
1030 (if (listp form)
1031 (let ((expansion (expand-compiler-macro form)))
1032 (if (neq expansion form)
1033 (return-from preprocessor-macroexpand-1
1034 (values expansion t)))))
1035 (handler-case (%macroexpand-1 form *lexenv*)
1036 (error (condition)
1037 (compiler-error "(during macroexpansion of ~A)~%~A"
1038 (let ((*print-level* 2)
1039 (*print-length* 2))
1040 (format nil "~S" form))
1041 condition))))
1043 ;;; Process a PROGN-like portion of a top level form. FORMS is a list of
1044 ;;; the forms, and PATH is the source path of the FORM they came out of.
1045 ;;; COMPILE-TIME-TOO is as in ANSI "3.2.3.1 Processing of Top Level Forms".
1046 (defun process-toplevel-progn (forms path compile-time-too)
1047 (declare (list forms) (list path))
1048 (dolist (form forms)
1049 (process-toplevel-form form path compile-time-too)))
1051 ;;; Process a top level use of LOCALLY, or anything else (e.g.
1052 ;;; MACROLET) at top level which has declarations and ordinary forms.
1053 ;;; We parse declarations and then recursively process the body.
1054 (defun process-toplevel-locally (body path compile-time-too &key vars funs)
1055 (declare (list path))
1056 (multiple-value-bind (forms decls)
1057 (parse-body body :doc-string-allowed nil :toplevel t)
1058 (with-ir1-namespace
1059 (let* ((*lexenv* (process-decls decls vars funs))
1060 ;; FIXME: VALUES declaration
1062 ;; Binding *POLICY* is pretty much of a hack, since it
1063 ;; causes LOCALLY to "capture" enclosed proclamations. It
1064 ;; is necessary because CONVERT-AND-MAYBE-COMPILE uses the
1065 ;; value of *POLICY* as the policy. The need for this hack
1066 ;; is due to the quirk that there is no way to represent in
1067 ;; a POLICY that an optimize quality came from the default.
1069 ;; FIXME: Ideally, something should be done so that DECLAIM
1070 ;; inside LOCALLY works OK. Failing that, at least we could
1071 ;; issue a warning instead of silently screwing up.
1072 (*policy* (lexenv-policy *lexenv*))
1073 ;; This is probably also a hack
1074 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
1075 ;; ditto
1076 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*)))
1077 (process-toplevel-progn forms path compile-time-too)))))
1079 ;;; Parse an EVAL-WHEN situations list, returning three flags,
1080 ;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
1081 ;;; the types of situations present in the list.
1082 (defun parse-eval-when-situations (situations)
1083 (when (or (not (listp situations))
1084 (set-difference situations
1085 '(:compile-toplevel
1086 compile
1087 :load-toplevel
1088 load
1089 :execute
1090 eval)))
1091 (compiler-error "bad EVAL-WHEN situation list: ~S" situations))
1092 (let ((deprecated-names (intersection situations '(compile load eval))))
1093 (when deprecated-names
1094 (style-warn "using deprecated EVAL-WHEN situation names~{ ~S~}"
1095 deprecated-names)))
1096 (values (intersection '(:compile-toplevel compile)
1097 situations)
1098 (intersection '(:load-toplevel load) situations)
1099 (intersection '(:execute eval) situations)))
1102 ;;; utilities for extracting COMPONENTs of FUNCTIONALs
1103 (defun functional-components (f)
1104 (declare (type functional f))
1105 (etypecase f
1106 (clambda (list (lambda-component f)))
1107 (optional-dispatch (let ((result nil))
1108 (flet ((maybe-frob (maybe-clambda)
1109 (when (and maybe-clambda
1110 (promise-ready-p maybe-clambda))
1111 (pushnew (lambda-component
1112 (force maybe-clambda))
1113 result))))
1114 (map nil #'maybe-frob (optional-dispatch-entry-points f))
1115 (maybe-frob (optional-dispatch-more-entry f))
1116 (maybe-frob (optional-dispatch-main-entry f)))
1117 result))))
1119 (defun make-functional-from-toplevel-lambda (lambda-expression
1120 &key
1121 name
1122 (path
1123 ;; I'd thought NIL should
1124 ;; work, but it doesn't.
1125 ;; -- WHN 2001-09-20
1126 (missing-arg)))
1127 (let* ((*current-path* path)
1128 (component (make-empty-component))
1129 (*current-component* component)
1130 (debug-name-tail (or name (name-lambdalike lambda-expression)))
1131 (source-name (or name '.anonymous.)))
1132 (setf (component-name component) (debug-name 'initial-component debug-name-tail)
1133 (component-kind component) :initial)
1134 (let* ((fun (let ((*allow-instrumenting* t))
1135 (funcall #'ir1-convert-lambdalike
1136 lambda-expression
1137 :source-name source-name)))
1138 ;; Convert the XEP using the policy of the real function. Otherwise
1139 ;; the wrong policy will be used for deciding whether to type-check
1140 ;; the parameters of the real function (via CONVERT-CALL /
1141 ;; PROPAGATE-TO-ARGS). -- JES, 2007-02-27
1142 (*lexenv* (make-lexenv :policy (lexenv-policy (functional-lexenv fun))))
1143 (xep (ir1-convert-lambda (make-xep-lambda-expression fun)
1144 :source-name source-name
1145 :debug-name (debug-name 'tl-xep debug-name-tail)
1146 :system-lambda t)))
1147 (when name
1148 (assert-global-function-definition-type name fun))
1149 (setf (functional-kind xep) :external
1150 (functional-entry-fun xep) fun
1151 (functional-entry-fun fun) xep
1152 (component-reanalyze component) t
1153 (functional-has-external-references-p xep) t)
1154 (reoptimize-component component :maybe)
1155 (locall-analyze-xep-entry-point fun)
1156 ;; Any leftover REFs to FUN outside local calls get replaced with the
1157 ;; XEP.
1158 (substitute-leaf-if (lambda (ref)
1159 (let* ((lvar (ref-lvar ref))
1160 (dest (when lvar (lvar-dest lvar)))
1161 (kind (when (basic-combination-p dest)
1162 (basic-combination-kind dest))))
1163 (neq :local kind)))
1165 fun)
1166 xep)))
1168 ;;; Compile LAMBDA-EXPRESSION into *COMPILE-OBJECT*, returning a
1169 ;;; description of the result.
1170 ;;; * If *COMPILE-OBJECT* is a CORE-OBJECT, then write the function
1171 ;;; into core and return the compiled FUNCTION value.
1172 ;;; * If *COMPILE-OBJECT* is a fasl file, then write the function
1173 ;;; into the fasl file and return a dump handle.
1175 ;;; If NAME is provided, then we try to use it as the name of the
1176 ;;; function for debugging/diagnostic information.
1177 (defun %compile (lambda-expression
1178 *compile-object*
1179 &key
1180 name
1181 (path
1182 ;; This magical idiom seems to be the appropriate
1183 ;; path for compiling standalone LAMBDAs, judging
1184 ;; from the CMU CL code and experiment, so it's a
1185 ;; nice default for things where we don't have a
1186 ;; real source path (as in e.g. inside CL:COMPILE).
1187 '(original-source-start 0 0)))
1188 (when name
1189 (legal-fun-name-or-type-error name))
1190 (with-ir1-namespace
1191 (let* ((*lexenv* (make-lexenv
1192 :policy *policy*
1193 :handled-conditions *handled-conditions*
1194 :disabled-package-locks *disabled-package-locks*))
1195 (*compiler-sset-counter* 0)
1196 (fun (make-functional-from-toplevel-lambda lambda-expression
1197 :name name
1198 :path path)))
1200 ;; FIXME: The compile-it code from here on is sort of a
1201 ;; twisted version of the code in COMPILE-TOPLEVEL. It'd be
1202 ;; better to find a way to share the code there; or
1203 ;; alternatively, to use this code to replace the code there.
1204 ;; (The second alternative might be pretty easy if we used
1205 ;; the :LOCALL-ONLY option to IR1-FOR-LAMBDA. Then maybe the
1206 ;; whole FUNCTIONAL-KIND=:TOPLEVEL case could go away..)
1208 (locall-analyze-clambdas-until-done (list fun))
1210 (let ((components-from-dfo (find-initial-dfo (list fun))))
1211 (dolist (component-from-dfo components-from-dfo)
1212 (compile-component component-from-dfo)
1213 (replace-toplevel-xeps component-from-dfo))
1215 (let ((entry-table (etypecase *compile-object*
1216 (fasl-output (fasl-output-entry-table
1217 *compile-object*))
1218 (core-object (core-object-entry-table
1219 *compile-object*)))))
1220 (multiple-value-bind (result found-p)
1221 (gethash (leaf-info fun) entry-table)
1222 (aver found-p)
1223 (prog1
1224 result
1225 ;; KLUDGE: This code duplicates some other code in this
1226 ;; file. In the great reorganzation, the flow of program
1227 ;; logic changed from the original CMUCL model, and that
1228 ;; path (as of sbcl-0.7.5 in SUB-COMPILE-FILE) was no
1229 ;; longer followed for CORE-OBJECTS, leading to BUG
1230 ;; 156. This place is transparently not the right one for
1231 ;; this code, but I don't have a clear enough overview of
1232 ;; the compiler to know how to rearrange it all so that
1233 ;; this operation fits in nicely, and it was blocking
1234 ;; reimplementation of (DECLAIM (INLINE FOO)) (MACROLET
1235 ;; ((..)) (DEFUN FOO ...))
1237 ;; FIXME: This KLUDGE doesn't solve all the problem in an
1238 ;; ideal way, as (1) definitions typed in at the REPL
1239 ;; without an INLINE declaration will give a NULL
1240 ;; FUNCTION-LAMBDA-EXPRESSION (allowable, but not ideal)
1241 ;; and (2) INLINE declarations will yield a
1242 ;; FUNCTION-LAMBDA-EXPRESSION headed by
1243 ;; SB-C:LAMBDA-WITH-LEXENV, even for null LEXENV. -- CSR,
1244 ;; 2002-07-02
1246 ;; (2) is probably fairly easy to fix -- it is, after all,
1247 ;; a matter of list manipulation (or possibly of teaching
1248 ;; CL:FUNCTION about SB-C:LAMBDA-WITH-LEXENV). (1) is
1249 ;; significantly harder, as the association between
1250 ;; function object and source is a tricky one.
1252 ;; FUNCTION-LAMBDA-EXPRESSION "works" (i.e. returns a
1253 ;; non-NULL list) when the function in question has been
1254 ;; compiled by (COMPILE <x> '(LAMBDA ...)); it does not
1255 ;; work when it has been compiled as part of the top-level
1256 ;; EVAL strategy of compiling everything inside (LAMBDA ()
1257 ;; ...). -- CSR, 2002-11-02
1258 (when (core-object-p *compile-object*)
1259 (fix-core-source-info *source-info* *compile-object* result))
1261 (mapc #'clear-ir1-info components-from-dfo))))))))
1263 (defun note-top-level-form (form &optional finalp)
1264 (when *compile-print*
1265 (cond ((not *top-level-form-noted*)
1266 (let ((*print-length* 2)
1267 (*print-level* 2)
1268 (*print-pretty* nil))
1269 (with-compiler-io-syntax
1270 (compiler-mumble
1271 #-sb-xc-host "~&; ~:[compiling~;converting~] ~S"
1272 #+sb-xc-host "~&; ~:[x-compiling~;x-converting~] ~S"
1273 *block-compile* form)))
1274 form)
1275 ((and finalp
1276 (eq :top-level-forms *compile-print*)
1277 (neq form *top-level-form-noted*))
1278 (let ((*print-length* 1)
1279 (*print-level* 1)
1280 (*print-pretty* nil))
1281 (with-compiler-io-syntax
1282 (compiler-mumble "~&; ... top level ~S" form)))
1283 form)
1285 *top-level-form-noted*))))
1287 ;;; Handle the evaluation the a :COMPILE-TOPLEVEL body during
1288 ;;; compilation. Normally just evaluate in the appropriate
1289 ;;; environment, but also compile if outputting a CFASL.
1290 (defun eval-compile-toplevel (body path)
1291 (flet ((frob ()
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)))))
1296 (if (null *macro-policy*)
1297 (frob)
1298 (let* ((*lexenv*
1299 (make-lexenv :policy (process-optimize-decl (macro-policy-decls nil)
1300 (lexenv-policy *lexenv*))
1301 :default *lexenv*))
1302 ;; In case a null lexenv is created, it needs to get the newly
1303 ;; effective global policy, not the policy currently in *POLICY*.
1304 (*policy* (lexenv-policy *lexenv*)))
1305 (frob)))))
1307 ;;; Process a top level FORM with the specified source PATH.
1308 ;;; * If this is a magic top level form, then do stuff.
1309 ;;; * If this is a macro, then expand it.
1310 ;;; * Otherwise, just compile it.
1312 ;;; COMPILE-TIME-TOO is as defined in ANSI
1313 ;;; "3.2.3.1 Processing of Top Level Forms".
1314 (defun process-toplevel-form (form path compile-time-too)
1315 (declare (list path))
1317 (catch 'process-toplevel-form-error-abort
1318 (let* ((path (or (get-source-path form) (cons form path)))
1319 (*current-path* path)
1320 (*compiler-error-bailout*
1321 (lambda (&optional condition)
1322 (convert-and-maybe-compile
1323 (make-compiler-error-form condition form)
1324 path)
1325 (throw 'process-toplevel-form-error-abort nil))))
1327 (flet ((default-processor (form)
1328 (let ((*top-level-form-noted* (note-top-level-form form)))
1329 ;; When we're cross-compiling, consider: what should we
1330 ;; do when we hit e.g.
1331 ;; (EVAL-WHEN (:COMPILE-TOPLEVEL)
1332 ;; (DEFUN FOO (X) (+ 7 X)))?
1333 ;; DEFUN has a macro definition in the cross-compiler,
1334 ;; and a different macro definition in the target
1335 ;; compiler. The only sensible thing is to use the
1336 ;; target compiler's macro definition, since the
1337 ;; cross-compiler's macro is in general into target
1338 ;; functions which can't meaningfully be executed at
1339 ;; cross-compilation time. So make sure we do the EVAL
1340 ;; here, before we macroexpand.
1342 ;; Then things get even dicier with something like
1343 ;; (DEFCONSTANT-EQX SB!XC:LAMBDA-LIST-KEYWORDS ..)
1344 ;; where we have to make sure that we don't uncross
1345 ;; the SB!XC: prefix before we do EVAL, because otherwise
1346 ;; we'd be trying to redefine the cross-compilation host's
1347 ;; constants.
1349 ;; (Isn't it fun to cross-compile Common Lisp?:-)
1350 #+sb-xc-host
1351 (progn
1352 (when compile-time-too
1353 (eval form)) ; letting xc host EVAL do its own macroexpansion
1354 (let* (;; (We uncross the operator name because things
1355 ;; like SB!XC:DEFCONSTANT and SB!XC:DEFTYPE
1356 ;; should be equivalent to their CL: counterparts
1357 ;; when being compiled as target code. We leave
1358 ;; the rest of the form uncrossed because macros
1359 ;; might yet expand into EVAL-WHEN stuff, and
1360 ;; things inside EVAL-WHEN can't be uncrossed
1361 ;; until after we've EVALed them in the
1362 ;; cross-compilation host.)
1363 (slightly-uncrossed (cons (uncross (first form))
1364 (rest form)))
1365 (expanded (preprocessor-macroexpand-1
1366 slightly-uncrossed)))
1367 (if (eq expanded slightly-uncrossed)
1368 ;; (Now that we're no longer processing toplevel
1369 ;; forms, and hence no longer need to worry about
1370 ;; EVAL-WHEN, we can uncross everything.)
1371 (convert-and-maybe-compile expanded path)
1372 ;; (We have to demote COMPILE-TIME-TOO to NIL
1373 ;; here, no matter what it was before, since
1374 ;; otherwise we'd tend to EVAL subforms more than
1375 ;; once, because of WHEN COMPILE-TIME-TOO form
1376 ;; above.)
1377 (process-toplevel-form expanded path nil))))
1378 ;; When we're not cross-compiling, we only need to
1379 ;; macroexpand once, so we can follow the 1-thru-6
1380 ;; sequence of steps in ANSI's "3.2.3.1 Processing of
1381 ;; Top Level Forms".
1382 #-sb-xc-host
1383 (let ((expanded (preprocessor-macroexpand-1 form)))
1384 (cond ((eq expanded form)
1385 (when compile-time-too
1386 (eval-compile-toplevel (list form) path))
1387 (convert-and-maybe-compile form path nil))
1389 (process-toplevel-form expanded
1390 path
1391 compile-time-too)))))))
1392 (if (atom form)
1393 #+sb-xc-host
1394 ;; (There are no xc EVAL-WHEN issues in the ATOM case until
1395 ;; (1) SBCL gets smart enough to handle global
1396 ;; DEFINE-SYMBOL-MACRO or SYMBOL-MACROLET and (2) SBCL
1397 ;; implementors start using symbol macros in a way which
1398 ;; interacts with SB-XC/CL distinction.)
1399 (convert-and-maybe-compile form path)
1400 #-sb-xc-host
1401 (default-processor form)
1402 (flet ((need-at-least-one-arg (form)
1403 (unless (cdr form)
1404 (compiler-error "~S form is too short: ~S"
1405 (car form)
1406 form))))
1407 (case (car form)
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 (values))
1585 ;;; Actually compile any stuff that has been queued up for block
1586 ;;; compilation.
1587 (defun finish-block-compilation ()
1588 (when *block-compile*
1589 (when *compile-print*
1590 (compiler-mumble "~&; block compiling converted top level forms..."))
1591 (when *toplevel-lambdas*
1592 (compile-toplevel (nreverse *toplevel-lambdas*) nil)
1593 (setq *toplevel-lambdas* ()))
1594 (setq *block-compile* nil)
1595 (setq *entry-points* nil)))
1597 (defun handle-condition-p (condition)
1598 (let ((lexenv
1599 (etypecase *compiler-error-context*
1600 (node
1601 (node-lexenv *compiler-error-context*))
1602 (compiler-error-context
1603 (let ((lexenv (compiler-error-context-lexenv
1604 *compiler-error-context*)))
1605 (aver lexenv)
1606 lexenv))
1607 (null *lexenv*))))
1608 (let ((muffles (lexenv-handled-conditions lexenv)))
1609 (if (null muffles) ; common case
1611 (dolist (muffle muffles nil)
1612 (destructuring-bind (typespec . restart-name) muffle
1613 (when (and (typep condition typespec)
1614 (find-restart restart-name condition))
1615 (return t))))))))
1617 (defun handle-condition-handler (condition)
1618 (let ((lexenv
1619 (etypecase *compiler-error-context*
1620 (node
1621 (node-lexenv *compiler-error-context*))
1622 (compiler-error-context
1623 (let ((lexenv (compiler-error-context-lexenv
1624 *compiler-error-context*)))
1625 (aver lexenv)
1626 lexenv))
1627 (null *lexenv*))))
1628 (let ((muffles (lexenv-handled-conditions lexenv)))
1629 (aver muffles)
1630 (dolist (muffle muffles (bug "fell through"))
1631 (destructuring-bind (typespec . restart-name) muffle
1632 (when (typep condition typespec)
1633 (awhen (find-restart restart-name condition)
1634 (invoke-restart it))))))))
1636 ;;; Read all forms from INFO and compile them, with output to OBJECT.
1637 ;;; Return (VALUES ABORT-P WARNINGS-P FAILURE-P).
1638 (defun sub-compile-file (info)
1639 (declare (type source-info info))
1640 (let ((*package* (sane-package))
1641 (*readtable* *readtable*)
1642 (sb!xc:*compile-file-pathname* nil) ; really bound in
1643 (sb!xc:*compile-file-truename* nil) ; SUB-SUB-COMPILE-FILE
1644 (*policy* *policy*)
1645 (*macro-policy* *macro-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 (*compiler-sset-counter* 0)
1667 (sb!xc:*gensym-counter* 0))
1668 (handler-case
1669 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1670 (with-compilation-values
1671 (sb!xc:with-compilation-unit ()
1672 (with-world-lock ()
1673 (sub-sub-compile-file info)
1674 (unless (zerop (hash-table-count *code-coverage-records*))
1675 ;; Dump the code coverage records into the fasl.
1676 (with-source-paths
1677 (fopcompile `(record-code-coverage
1678 ',(namestring *compile-file-pathname*)
1679 ',(let (list)
1680 (maphash (lambda (k v)
1681 (declare (ignore k))
1682 (push v list))
1683 *code-coverage-records*)
1684 list))
1686 nil)))
1687 (finish-block-compilation)
1688 (let ((object *compile-object*))
1689 (etypecase object
1690 (fasl-output (fasl-dump-source-info info object))
1691 (core-object (fix-core-source-info info object))
1692 (null)))
1693 nil))))
1694 ;; Some errors are sufficiently bewildering that we just fail
1695 ;; immediately, without trying to recover and compile more of
1696 ;; the input file.
1697 (fatal-compiler-error (condition)
1698 (signal condition)
1699 (fresh-line *error-output*)
1700 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
1701 (format *error-output*
1702 "~@<~@:_compilation aborted because of fatal error: ~2I~_~A~@:_~:>"
1703 (encapsulated-condition condition)))
1704 (finish-output *error-output*)
1705 (values t t t)))))
1707 ;;; Return a pathname for the named file. The file must exist.
1708 (defun verify-source-file (pathname-designator)
1709 (let* ((pathname (pathname pathname-designator))
1710 (default-host (make-pathname :host (pathname-host pathname))))
1711 (flet ((try-with-type (path type error-p)
1712 (let ((new (merge-pathnames
1713 path (make-pathname :type type
1714 :defaults default-host))))
1715 (if (probe-file new)
1717 (and error-p (truename new))))))
1718 (cond ((typep pathname 'logical-pathname)
1719 (try-with-type pathname "LISP" t))
1720 ((probe-file pathname) pathname)
1721 ((try-with-type pathname "lisp" nil))
1722 ((try-with-type pathname "lisp" t))))))
1724 (defun elapsed-time-to-string (internal-time-delta)
1725 (multiple-value-bind (tsec remainder)
1726 (truncate internal-time-delta internal-time-units-per-second)
1727 (let ((ms (truncate remainder (/ internal-time-units-per-second 1000))))
1728 (multiple-value-bind (tmin sec) (truncate tsec 60)
1729 (multiple-value-bind (thr min) (truncate tmin 60)
1730 (format nil "~D:~2,'0D:~2,'0D.~3,'0D" thr min sec ms))))))
1732 ;;; Print some junk at the beginning and end of compilation.
1733 (defun print-compile-start-note (source-info)
1734 (declare (type source-info source-info))
1735 (let ((file-info (source-info-file-info source-info)))
1736 (compiler-mumble "~&; compiling file ~S (written ~A):~%"
1737 (namestring (file-info-name file-info))
1738 (sb!int:format-universal-time nil
1739 (file-info-write-date
1740 file-info)
1741 :style :government
1742 :print-weekday nil
1743 :print-timezone nil)))
1744 (values))
1746 (defun print-compile-end-note (source-info won)
1747 (declare (type source-info source-info))
1748 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1750 (elapsed-time-to-string
1751 (- (get-internal-real-time)
1752 (source-info-start-real-time source-info))))
1753 (values))
1755 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1756 ;;; out of the compile, then abort the writing of the output file, so
1757 ;;; that we don't overwrite it with known garbage.
1758 (defun sb!xc:compile-file
1759 (input-file
1760 &key
1762 ;; ANSI options
1763 (output-file (cfp-output-file-default input-file))
1764 ;; FIXME: ANSI doesn't seem to say anything about
1765 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1766 ;; function..
1767 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1768 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1769 (external-format :default)
1771 ;; extensions
1772 (trace-file nil)
1773 ((:block-compile *block-compile-arg*) nil)
1774 (emit-cfasl *emit-cfasl*))
1775 #!+sb-doc
1776 "Compile INPUT-FILE, producing a corresponding fasl file and
1777 returning its filename.
1779 :PRINT
1780 If true, a message per non-macroexpanded top level form is printed
1781 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1782 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1783 no such message, but their subforms do.
1785 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1786 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1787 For example, compiling an IN-PACKAGE form will result in a message about
1788 a top level SETQ in addition to the message about the IN-PACKAGE form'
1789 itself.
1791 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1793 :BLOCK-COMPILE
1794 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1795 argument, it is not currently supported. (non-standard)
1797 :TRACE-FILE
1798 If given, internal data structures are dumped to the specified
1799 file, or if a value of T is given, to a file of *.trace type
1800 derived from the input file name. (non-standard)
1802 :EMIT-CFASL
1803 (Experimental). If true, outputs the toplevel compile-time effects
1804 of this file into a separate .cfasl file."
1805 ;;; Block compilation is currently broken.
1807 "Also, as a workaround for vaguely-non-ANSI behavior, the
1808 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1809 multiple functions are compiled together as a unit, resolving function
1810 references at compile time. NIL means that global function names are
1811 never resolved at compilation time. Currently NIL is the default
1812 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1813 of the ANSI spec allows this behavior under all circumstances, the
1814 compiler's runtime scales badly when it tries to do this for large
1815 files. If/when this performance problem is fixed, the block
1816 compilation default behavior will probably be made dependent on the
1817 SPEED and COMPILATION-SPEED optimization values, and the
1818 :BLOCK-COMPILE argument will probably become deprecated."
1820 (let* ((fasl-output nil)
1821 (cfasl-output nil)
1822 (output-file-name nil)
1823 (coutput-file-name nil)
1824 (abort-p t)
1825 (warnings-p nil)
1826 (failure-p t) ; T in case error keeps this from being set later
1827 (input-pathname (verify-source-file input-file))
1828 (source-info (make-file-source-info input-pathname external-format))
1829 (*compiler-trace-output* nil)) ; might be modified below
1831 (unwind-protect
1832 (progn
1833 (when output-file
1834 (setq output-file-name
1835 (sb!xc:compile-file-pathname input-file
1836 :output-file output-file))
1837 (setq fasl-output
1838 (open-fasl-output output-file-name
1839 (namestring input-pathname))))
1840 (when emit-cfasl
1841 (setq coutput-file-name
1842 (make-pathname :type "cfasl"
1843 :defaults output-file-name))
1844 (setq cfasl-output
1845 (open-fasl-output coutput-file-name
1846 (namestring input-pathname))))
1847 (when trace-file
1848 (let* ((default-trace-file-pathname
1849 (make-pathname :type "trace" :defaults input-pathname))
1850 (trace-file-pathname
1851 (if (eql trace-file t)
1852 default-trace-file-pathname
1853 (merge-pathnames trace-file
1854 default-trace-file-pathname))))
1855 (setf *compiler-trace-output*
1856 (open trace-file-pathname
1857 :if-exists :supersede
1858 :direction :output))))
1860 (when sb!xc:*compile-verbose*
1861 (print-compile-start-note source-info))
1863 (let ((*compile-object* fasl-output)
1864 (*compile-toplevel-object* cfasl-output))
1865 (setf (values abort-p warnings-p failure-p)
1866 (sub-compile-file source-info))))
1868 (close-source-info source-info)
1870 (when fasl-output
1871 (close-fasl-output fasl-output abort-p)
1872 (setq output-file-name
1873 (pathname (fasl-output-stream fasl-output)))
1874 (when (and (not abort-p) sb!xc:*compile-verbose*)
1875 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1877 (when cfasl-output
1878 (close-fasl-output cfasl-output abort-p)
1879 (when (and (not abort-p) sb!xc:*compile-verbose*)
1880 (compiler-mumble "; ~A written~%" (namestring coutput-file-name))))
1882 (when sb!xc:*compile-verbose*
1883 (print-compile-end-note source-info (not abort-p)))
1885 (when *compiler-trace-output*
1886 (close *compiler-trace-output*)))
1888 ;; CLHS says that the first value is NIL if the "file could not
1889 ;; be created". We interpret this to mean "a valid fasl could not
1890 ;; be created" -- which can happen if the compilation is aborted
1891 ;; before the whole file has been processed, due to eg. a reader
1892 ;; error.
1893 (values (when (and (not abort-p) output-file)
1894 ;; Hack around filesystem race condition...
1895 (or (probe-file output-file-name) output-file-name))
1896 warnings-p
1897 failure-p)))
1899 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1900 ;;; the OUTPUT-FILE argument
1902 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1903 ;;; that results from merging the INPUT-FILE with the value of
1904 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1905 ;;; default to the appropriate implementation-defined default type for
1906 ;;; compiled files.
1907 (defun cfp-output-file-default (input-file)
1908 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1909 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1910 retyped))
1912 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1913 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1914 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1915 ;;; pathname, it is translated into a physical pathname as if by
1916 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1917 ;;; So I haven't really tried to make this precisely ANSI-compatible
1918 ;;; at the level of e.g. whether it returns logical pathname or a
1919 ;;; physical pathname. Patches to make it more correct are welcome.
1920 ;;; -- WHN 2000-12-09
1921 (defun sb!xc:compile-file-pathname (input-file
1922 &key
1923 (output-file nil output-file-p)
1924 &allow-other-keys)
1925 #!+sb-doc
1926 "Return a pathname describing what file COMPILE-FILE would write to given
1927 these arguments."
1928 (if output-file-p
1929 (merge-pathnames output-file (cfp-output-file-default input-file))
1930 (cfp-output-file-default input-file)))
1932 ;;;; MAKE-LOAD-FORM stuff
1934 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1935 ;;; finds a constant structure, it invokes this to arrange for proper
1936 ;;; dumping. If it turns out that the constant has already been
1937 ;;; dumped, then we don't need to do anything.
1939 ;;; If the constant hasn't been dumped, then we check to see whether
1940 ;;; we are in the process of creating it. We detect this by
1941 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
1942 ;;; the constants we are in the process of creating. Actually, each
1943 ;;; entry is a list of the constant and any init forms that need to be
1944 ;;; processed on behalf of that constant.
1946 ;;; It's not necessarily an error for this to happen. If we are
1947 ;;; processing the init form for some object that showed up *after*
1948 ;;; the original reference to this constant, then we just need to
1949 ;;; defer the processing of that init form. To detect this, we
1950 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
1951 ;;; constants created since the last time we started processing an
1952 ;;; init form. If the constant passed to emit-make-load-form shows up
1953 ;;; in this list, then there is a circular chain through creation
1954 ;;; forms, which is an error.
1956 ;;; If there is some intervening init form, then we blow out of
1957 ;;; processing it by throwing to the tag PENDING-INIT. The value we
1958 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
1959 ;;; offending init form can be tacked onto the init forms for the
1960 ;;; circular object.
1962 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
1963 ;;; we have to create it. We call MAKE-LOAD-FORM and check to see
1964 ;;; whether the creation form is the magic value
1965 ;;; :SB-JUST-DUMP-IT-NORMALLY. If it is, then we don't do anything. The
1966 ;;; dumper will eventually get its hands on the object and use the
1967 ;;; normal structure dumping noise on it.
1969 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
1970 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
1971 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
1972 ;;; dumper to use that result instead whenever it sees this constant.
1974 ;;; Now we try to compile the init form. We bind
1975 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
1976 ;;; form (and any init forms that were added because of circularity
1977 ;;; detection). If this works, great. If not, we add the init forms to
1978 ;;; the init forms for the object that caused the problems and let it
1979 ;;; deal with it.
1980 (defvar *constants-being-created* nil)
1981 (defvar *constants-created-since-last-init* nil)
1982 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
1983 (defun emit-make-load-form (constant &optional (name nil namep))
1984 (aver (fasl-output-p *compile-object*))
1985 (unless (or (fasl-constant-already-dumped-p constant *compile-object*)
1986 ;; KLUDGE: This special hack is because I was too lazy
1987 ;; to rework DEF!STRUCT so that the MAKE-LOAD-FORM
1988 ;; function of LAYOUT returns nontrivial forms when
1989 ;; building the cross-compiler but :IGNORE-IT when
1990 ;; cross-compiling or running under the target Lisp. --
1991 ;; WHN 19990914
1992 #+sb-xc-host (typep constant 'layout))
1993 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
1994 (when circular-ref
1995 (when (find constant *constants-created-since-last-init* :test #'eq)
1996 (throw constant t))
1997 (throw 'pending-init circular-ref)))
1998 (multiple-value-bind (creation-form init-form)
1999 (if namep
2000 ;; If the constant is a reference to a named constant, we can
2001 ;; just use SYMBOL-VALUE during LOAD.
2002 (values `(symbol-value ',name) nil)
2003 (handler-case
2004 (sb!xc:make-load-form constant (make-null-lexenv))
2005 (error (condition)
2006 (compiler-error condition))))
2007 #-sb-xc-host
2008 (when (and (not namep)
2009 (listp creation-form) ; skip if already a magic keyword
2010 (typep constant 'structure-object)
2011 (sb!kernel::canonical-slot-saving-forms-p
2012 constant creation-form init-form))
2013 (setq creation-form :sb-just-dump-it-normally))
2014 (case creation-form
2015 (:sb-just-dump-it-normally
2016 (fasl-validate-structure constant *compile-object*)
2018 (:ignore-it
2019 nil)
2021 (let* ((name (write-to-string constant :level 1 :length 2))
2022 (info (if init-form
2023 (list constant name init-form)
2024 (list constant))))
2025 (let ((*constants-being-created*
2026 (cons info *constants-being-created*))
2027 (*constants-created-since-last-init*
2028 (cons constant *constants-created-since-last-init*)))
2029 (when
2030 (catch constant
2031 (fasl-note-handle-for-constant
2032 constant
2033 (or (fopcompile-allocate-instance creation-form)
2034 (compile-load-time-value creation-form))
2035 *compile-object*)
2036 nil)
2037 (compiler-error "circular references in creation form for ~S"
2038 constant)))
2039 (when (cdr info)
2040 (let* ((*constants-created-since-last-init* nil)
2041 (circular-ref
2042 (catch 'pending-init
2043 (loop for (name form) on (cdr info) by #'cddr
2044 collect name into names
2045 collect form into forms
2046 finally (or (fopcompile-constant-init-forms forms)
2047 (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)))))))
2052 nil)))))
2055 ;;;; Host compile time definitions
2056 #+sb-xc-host
2057 (defun compile-in-lexenv (name lambda lexenv)
2058 (declare (ignore lexenv))
2059 (compile name lambda))
2061 #+sb-xc-host
2062 (defun eval-tlf (form index &optional lexenv)
2063 (declare (ignore index lexenv))
2064 (eval form))