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