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