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