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