x86-64: Better printing of FS: prefix
[sbcl.git] / src / compiler / main.lisp
blobee82c8bba2e256842a2b4a5846cd61bd1eb6dfe3
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 (defvar *check-consistency* nil)
18 ;;; Set to NIL to disable loop analysis for register allocation.
19 (defvar *loop-analyze* t)
21 ;;; Bind this to a stream to capture various internal debugging output.
22 (defvar *compiler-trace-output* nil)
24 ;;; The current block compilation state. These are initialized to the
25 ;;; :BLOCK-COMPILE and :ENTRY-POINTS arguments that COMPILE-FILE was
26 ;;; called with.
27 ;;;
28 ;;; *BLOCK-COMPILE-ARG* holds the original value of the :BLOCK-COMPILE
29 ;;; argument, which overrides any internal declarations.
30 (defvar *block-compile*)
31 (defvar *block-compile-arg*)
32 (declaim (type (member nil t :specified) *block-compile* *block-compile-arg*))
33 (defvar *entry-points*)
34 (declaim (list *entry-points*))
36 ;;; When block compiling, used by PROCESS-FORM to accumulate top level
37 ;;; lambdas resulting from compiling subforms. (In reverse order.)
38 (defvar *toplevel-lambdas*)
39 (declaim (list *toplevel-lambdas*))
41 ;;; The current non-macroexpanded toplevel form as printed when
42 ;;; *compile-print* is true.
43 (defvar *top-level-form-noted* nil)
45 (defvar sb!xc:*compile-verbose* t
46 "The default for the :VERBOSE argument to COMPILE-FILE.")
47 (defvar sb!xc:*compile-print* t
48 "The default for the :PRINT argument to COMPILE-FILE.")
49 (defvar *compile-progress* nil
50 "When this is true, the compiler prints to *STANDARD-OUTPUT* progress
51 information about the phases of compilation of each function. (This
52 is useful mainly in large block compilations.)")
54 (defvar sb!xc:*compile-file-pathname* nil
55 "The defaulted pathname of the file currently being compiled, or NIL if not
56 compiling.")
57 (defvar sb!xc:*compile-file-truename* nil
58 "The TRUENAME of the file currently being compiled, or NIL if not
59 compiling.")
61 (declaim (type (or pathname null)
62 sb!xc:*compile-file-pathname*
63 sb!xc:*compile-file-truename*))
65 ;;; the SOURCE-INFO structure for the current compilation. This is
66 ;;; null globally to indicate that we aren't currently in any
67 ;;; identifiable compilation.
68 (defvar *source-info* nil)
70 ;;; This is true if we are within a WITH-COMPILATION-UNIT form (which
71 ;;; normally causes nested uses to be no-ops).
72 (defvar *in-compilation-unit* nil)
74 ;;; Count of the number of compilation units dynamically enclosed by
75 ;;; the current active WITH-COMPILATION-UNIT that were unwound out of.
76 (defvar *aborted-compilation-unit-count*)
78 ;;; Mumble conditional on *COMPILE-PROGRESS*.
79 (defun maybe-mumble (&rest foo)
80 (when *compile-progress*
81 (compiler-mumble "~&")
82 (pprint-logical-block (*standard-output* nil :per-line-prefix "; ")
83 (apply #'compiler-mumble foo))))
86 (deftype object () '(or fasl-output core-object null))
87 (declaim (type object *compile-object*))
88 (defvar *compile-toplevel-object* nil)
90 (defvar *emit-cfasl* nil)
92 (defvar *fopcompile-label-counter*)
94 (defvar *compiler-coverage-metadata*)
95 (declaim (type (or (cons hash-table hash-table) null) *compiler-coverage-metadata*))
96 (declaim (inline code-coverage-records code-coverage-blocks))
97 ;; Used during compilation to map code paths to the matching
98 ;; instrumentation conses.
99 (defun code-coverage-records (x) (car x))
100 ;; Used during compilation to keep track of with source paths have been
101 ;; instrumented in which blocks.
102 (defun code-coverage-blocks (x) (cdr x))
103 ;; Stores the code coverage instrumentation results. Keys are namestrings, the
104 ;; value is a list of (CONS PATH STATE), where STATE is +CODE-COVERAGE-UNMARKED+
105 ;; for a path that has not been visited, and T for one that has.
106 (defvar *code-coverage-info* (make-hash-table :test 'equal))
109 ;;;; WITH-COMPILATION-UNIT and WITH-COMPILATION-VALUES
111 (defmacro sb!xc:with-compilation-unit (options &body body)
112 "Affects compilations that take place within its dynamic extent. It is
113 intended to be eg. wrapped around the compilation of all files in the same system.
115 Following options are defined:
117 :OVERRIDE Boolean-Form
118 One of the effects of this form is to delay undefined warnings until the
119 end of the form, instead of giving them at the end of each compilation.
120 If OVERRIDE is NIL (the default), then the outermost
121 WITH-COMPILATION-UNIT form grabs the undefined warnings. Specifying
122 OVERRIDE true causes that form to grab any enclosed warnings, even if it
123 is enclosed by another WITH-COMPILATION-UNIT.
125 :POLICY Optimize-Declaration-Form
126 Provides dynamic scoping for global compiler optimization qualities and
127 restrictions, limiting effects of subsequent OPTIMIZE proclamations and
128 calls to SB-EXT:RESTRICT-COMPILER-POLICY to the dynamic scope of BODY.
130 If OVERRIDE is false, specified POLICY is merged with current global
131 policy. If OVERRIDE is true, current global policy, including any
132 restrictions, is discarded in favor of the specified POLICY.
134 Supplying POLICY NIL is equivalent to the option not being supplied at
135 all, ie. dynamic scoping of policy does not take place.
137 This option is an SBCL-specific experimental extension: Interface
138 subject to change.
140 :SOURCE-NAMESTRING Namestring-Form
141 Attaches the value returned by the Namestring-Form to the internal
142 debug-source information as the namestring of the source file. Normally
143 the namestring of the input-file for COMPILE-FILE is used: this option
144 can be used to provide source-file information for functions compiled
145 using COMPILE, or to override the input-file of COMPILE-FILE.
147 If both an outer and an inner WITH-COMPILATION-UNIT provide a
148 SOURCE-NAMESTRING, the inner one takes precedence. Unaffected
149 by :OVERRIDE.
151 This is an SBCL-specific extension.
153 :SOURCE-PLIST Plist-Form
154 Attaches the value returned by the Plist-Form to internal debug-source
155 information of functions compiled in within the dynamic extent of BODY.
157 Primarily for use by development environments, in order to eg. associate
158 function definitions with editor-buffers. Can be accessed using
159 SB-INTROSPECT:DEFINITION-SOURCE-PLIST.
161 If an outer WITH-COMPILATION-UNIT form also provide a SOURCE-PLIST, it
162 is appended to the end of the provided SOURCE-PLIST. Unaffected
163 by :OVERRIDE.
165 This is an SBCL-specific extension.
167 Examples:
169 ;; Prevent proclamations from the file leaking, and restrict
170 ;; SAFETY to 3 -- otherwise uses the current global policy.
171 (with-compilation-unit (:policy '(optimize))
172 (restrict-compiler-policy 'safety 3)
173 (load \"foo.lisp\"))
175 ;; Using default policy instead of the current global one,
176 ;; except for DEBUG 3.
177 (with-compilation-unit (:policy '(optimize debug)
178 :override t)
179 (load \"foo.lisp\"))
181 ;; Same as if :POLICY had not been specified at all: SAFETY 3
182 ;; proclamation leaks out from WITH-COMPILATION-UNIT.
183 (with-compilation-unit (:policy nil)
184 (declaim (optimize safety))
185 (load \"foo.lisp\"))
187 `(%with-compilation-unit (lambda () ,@body) ,@options))
189 (defvar *source-plist* nil)
190 (defvar *source-namestring* nil)
192 (defun %with-compilation-unit (fn &key override policy source-plist source-namestring)
193 (declare (type function fn))
194 (flet ((with-it ()
195 (let ((succeeded-p nil)
196 (*source-plist* (append source-plist *source-plist*))
197 (*source-namestring*
198 (awhen (or source-namestring *source-namestring*)
199 (possibly-base-stringize it))))
200 (if (and *in-compilation-unit* (not override))
201 ;; Inside another WITH-COMPILATION-UNIT, a WITH-COMPILATION-UNIT is
202 ;; ordinarily (unless OVERRIDE) basically a no-op.
203 (unwind-protect
204 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
205 (unless succeeded-p
206 (incf *aborted-compilation-unit-count*)))
207 (let ((*aborted-compilation-unit-count* 0)
208 (*compiler-error-count* 0)
209 (*compiler-warning-count* 0)
210 (*compiler-style-warning-count* 0)
211 (*compiler-note-count* 0)
212 (*undefined-warnings* nil)
213 (*in-compilation-unit* t))
214 (handler-bind ((parse-unknown-type
215 (lambda (c)
216 (note-undefined-reference
217 (parse-unknown-type-specifier c)
218 :type))))
219 (unwind-protect
220 (multiple-value-prog1 (funcall fn) (setf succeeded-p t))
221 (unless succeeded-p
222 (incf *aborted-compilation-unit-count*))
223 (summarize-compilation-unit (not succeeded-p)))))))))
224 (if policy
225 (let ((*policy* (process-optimize-decl policy (unless override *policy*)))
226 (*policy-min* (unless override *policy-min*))
227 (*policy-max* (unless override *policy-max*)))
228 (with-it))
229 (with-it))))
231 ;;; Is NAME something that no conforming program can rely on
232 ;;; defining?
233 (defun name-reserved-by-ansi-p (name kind)
234 (ecase kind
235 (:function
236 (eq (symbol-package (fun-name-block-name name))
237 *cl-package*))
238 (:type
239 (let ((symbol (typecase name
240 (symbol name)
241 ((cons symbol) (car name))
242 (t (return-from name-reserved-by-ansi-p nil)))))
243 (eq (symbol-package symbol) *cl-package*)))))
245 ;;; This is to be called at the end of a compilation unit. It signals
246 ;;; any residual warnings about unknown stuff, then prints the total
247 ;;; error counts. ABORT-P should be true when the compilation unit was
248 ;;; aborted by throwing out. ABORT-COUNT is the number of dynamically
249 ;;; enclosed nested compilation units that were aborted.
250 (defun summarize-compilation-unit (abort-p)
251 (let (summary)
252 (unless abort-p
253 (handler-bind ((style-warning #'compiler-style-warning-handler)
254 (warning #'compiler-warning-handler))
256 (let ((undefs (sort *undefined-warnings* #'string<
257 :key (lambda (x)
258 (let ((x (undefined-warning-name x)))
259 (if (symbolp x)
260 (symbol-name x)
261 (prin1-to-string x)))))))
262 (dolist (kind '(:variable :function :type))
263 (let ((names (mapcar #'undefined-warning-name
264 (remove kind undefs :test #'neq
265 :key #'undefined-warning-kind))))
266 (when names (push (cons kind names) summary))))
267 (dolist (undef undefs)
268 (let ((name (undefined-warning-name undef))
269 (kind (undefined-warning-kind undef))
270 (warnings (undefined-warning-warnings undef))
271 (undefined-warning-count (undefined-warning-count undef)))
272 (dolist (*compiler-error-context* warnings)
273 (if (and (member kind '(:function :type))
274 (name-reserved-by-ansi-p name kind))
275 (ecase kind
276 (:function
277 (compiler-warn
278 "~@<The function ~S is undefined, and its name is ~
279 reserved by ANSI CL so that even if it were ~
280 defined later, the code doing so would not be ~
281 portable.~:@>" name))
282 (:type
283 (if (and (consp name) (eq 'quote (car name)))
284 (compiler-warn
285 "~@<Undefined type ~S. The name starts with ~S: ~
286 probably use of a quoted type name in a context ~
287 where the name is not evaluated.~:@>"
288 name 'quote)
289 (compiler-warn
290 "~@<Undefined type ~S. Note that name ~S is ~
291 reserved by ANSI CL, so code defining a type with ~
292 that name would not be portable.~:@>" name
293 name))))
294 (if (eq kind :variable)
295 (compiler-warn "undefined ~(~A~): ~S" kind name)
296 (compiler-style-warn "undefined ~(~A~): ~S" kind name))))
297 (let ((warn-count (length warnings)))
298 (when (and warnings (> undefined-warning-count warn-count))
299 (let ((more (- undefined-warning-count warn-count)))
300 (if (eq kind :variable)
301 (compiler-warn
302 "~W more use~:P of undefined ~(~A~) ~S"
303 more kind name)
304 (compiler-style-warn
305 "~W more use~:P of undefined ~(~A~) ~S"
306 more kind name))))))))))
308 (unless (and (not abort-p)
309 (zerop *aborted-compilation-unit-count*)
310 (zerop *compiler-error-count*)
311 (zerop *compiler-warning-count*)
312 (zerop *compiler-style-warning-count*)
313 (zerop *compiler-note-count*))
314 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
315 (format *error-output* "~&compilation unit ~:[finished~;aborted~]"
316 abort-p)
317 (dolist (cell summary)
318 (destructuring-bind (kind &rest names) cell
319 (format *error-output*
320 "~& Undefined ~(~A~)~p:~
321 ~% ~{~<~% ~1:;~S~>~^ ~}"
322 kind (length names) names)))
323 (format *error-output* "~[~:;~:*~& caught ~W fatal ERROR condition~:P~]~
324 ~[~:;~:*~& caught ~W ERROR condition~:P~]~
325 ~[~:;~:*~& caught ~W WARNING condition~:P~]~
326 ~[~:;~:*~& caught ~W STYLE-WARNING condition~:P~]~
327 ~[~:;~:*~& printed ~W note~:P~]"
328 *aborted-compilation-unit-count*
329 *compiler-error-count*
330 *compiler-warning-count*
331 *compiler-style-warning-count*
332 *compiler-note-count*))
333 (terpri *error-output*)
334 (force-output *error-output*))))
336 ;; Bidrectional map between IR1/IR2/assembler abstractions
337 ;; and a corresponding small integer identifier. One direction could be done
338 ;; by adding the integer ID as an object slot, but we want both directions.
339 (defstruct (compiler-ir-obj-map (:conc-name objmap-)
340 (:constructor make-compiler-ir-obj-map ())
341 (:copier nil)
342 (:predicate nil))
343 (obj-to-id (make-hash-table :test 'eq) :read-only t)
344 (id-to-cont (make-array 10) :type simple-vector) ; number -> CTRAN or LVAR
345 (id-to-tn (make-array 10) :type simple-vector) ; number -> TN
346 (id-to-label (make-array 10) :type simple-vector) ; number -> LABEL
347 (cont-num 0 :type fixnum)
348 (tn-id 0 :type fixnum)
349 (label-id 0 :type fixnum))
351 (declaim (type compiler-ir-obj-map *compiler-ir-obj-map*))
352 (defvar *compiler-ir-obj-map*)
354 ;;; Evaluate BODY, then return (VALUES BODY-VALUE WARNINGS-P
355 ;;; FAILURE-P), where BODY-VALUE is the first value of the body, and
356 ;;; WARNINGS-P and FAILURE-P are as in CL:COMPILE or CL:COMPILE-FILE.
357 (defmacro with-compilation-values (&body body)
358 ;; This binding could just as well be in WITH-IR1-NAMESPACE, but
359 ;; since it's primarily a debugging tool, it's nicer to have
360 ;; a wider unique scope by ID.
361 `(let ((*compiler-ir-obj-map* (make-compiler-ir-obj-map))
362 (*finite-sbs* ,(finite-sbs-ctor-form)))
363 (unwind-protect
364 (let ((*warnings-p* nil)
365 (*failure-p* nil))
366 (handler-bind ((compiler-error #'compiler-error-handler)
367 (style-warning #'compiler-style-warning-handler)
368 (warning #'compiler-warning-handler))
369 (values (progn ,@body) *warnings-p* *failure-p*)))
370 (let ((map *compiler-ir-obj-map*))
371 (clrhash (objmap-obj-to-id map))
372 (fill (objmap-id-to-cont map) nil)
373 (fill (objmap-id-to-tn map) nil)
374 (fill (objmap-id-to-label map) nil)))))
376 ;;; THING is a kind of thing about which we'd like to issue a warning,
377 ;;; but showing at most one warning for a given set of <THING,FMT,ARGS>.
378 ;;; The compiler does a good job of making sure not to print repetitive
379 ;;; warnings for code that it compiles, but this solves a different problem.
380 ;;; Specifically, for a warning from PARSE-LAMBDA-LIST, there are three calls:
381 ;;; - once in the expander for defmacro itself, as it calls MAKE-MACRO-LAMBDA
382 ;;; which calls PARSE-LAMBDA-LIST. This is the toplevel form processing.
383 ;;; - again for :compile-toplevel, where the DS-BIND calls PARSE-LAMBDA-LIST.
384 ;;; If compiling in compile-toplevel, then *COMPILE-OBJECT* is a core object,
385 ;;; but if interpreting, then it is still a fasl.
386 ;;; - once for compiling to fasl. *COMPILE-OBJECT* is a fasl.
387 ;;; I'd have liked the data to be associated with the fasl, except that
388 ;;; as indicated above, the second line hides some information.
389 (defun style-warn-once (thing fmt &rest args)
390 (declare (special *compile-object*))
391 (declare (notinline style-warn)) ; See COMPILER-STYLE-WARN for rationale
392 (let* ((source-info *source-info*)
393 (file-info (and (source-info-p source-info)
394 (source-info-file-info source-info)))
395 (file-compiling-p (file-info-p file-info)))
396 (flet ((match-p (entry &aux (rest (cdr entry)))
397 ;; THING is compared by EQ, FMT by STRING=.
398 (and (eq (car entry) thing)
399 (string= (car rest) fmt)
400 ;; We don't want to walk into default values,
401 ;; e.g. (&optional (b #<insane-struct))
402 ;; because #<insane-struct> might be circular.
403 (equal-but-no-car-recursion (cdr rest) args))))
404 (unless (and file-compiling-p
405 (find-if #'match-p
406 (file-info-style-warning-tracker file-info)))
407 (when file-compiling-p
408 (push (list* thing fmt args)
409 (file-info-style-warning-tracker file-info)))
410 (apply 'style-warn fmt args)))))
412 ;;;; component compilation
414 (defparameter *max-optimize-iterations* 3 ; ARB
415 "The upper limit on the number of times that we will consecutively do IR1
416 optimization that doesn't introduce any new code. A finite limit is
417 necessary, since type inference may take arbitrarily long to converge.")
419 (defevent ir1-optimize-until-done "IR1-OPTIMIZE-UNTIL-DONE called")
420 (defevent ir1-optimize-maxed-out "hit *MAX-OPTIMIZE-ITERATIONS* limit")
422 ;;; Repeatedly optimize COMPONENT until no further optimizations can
423 ;;; be found or we hit our iteration limit. When we hit the limit, we
424 ;;; clear the component and block REOPTIMIZE flags to discourage the
425 ;;; next optimization attempt from pounding on the same code.
426 (defun ir1-optimize-until-done (component)
427 (declare (type component component))
428 (maybe-mumble "opt")
429 (event ir1-optimize-until-done)
430 (let ((count 0)
431 (cleared-reanalyze nil)
432 (fastp nil))
433 (loop
434 (when (component-reanalyze component)
435 (setq count 0)
436 (setq cleared-reanalyze t)
437 (setf (component-reanalyze component) nil))
438 (setf (component-reoptimize component) nil)
439 (ir1-optimize component fastp)
440 (cond ((component-reoptimize component)
441 (incf count)
442 (when (and (>= count *max-optimize-iterations*)
443 (not (component-reanalyze component))
444 (eq (component-reoptimize component) :maybe))
445 (maybe-mumble "*")
446 (cond ((retry-delayed-ir1-transforms :optimize)
447 (maybe-mumble "+")
448 (setq count 0))
450 (event ir1-optimize-maxed-out)
451 (setf (component-reoptimize component) nil)
452 (do-blocks (block component)
453 (setf (block-reoptimize block) nil))
454 (return)))))
455 ((retry-delayed-ir1-transforms :optimize)
456 (setf count 0)
457 (maybe-mumble "+"))
459 (maybe-mumble " ")
460 (return)))
461 (setq fastp (>= count *max-optimize-iterations*))
462 (maybe-mumble (if fastp "-" ".")))
463 (when cleared-reanalyze
464 (setf (component-reanalyze component) t)))
465 (values))
467 (defparameter *constraint-propagate* t)
469 ;;; KLUDGE: This was bumped from 5 to 10 in a DTC patch ported by MNA
470 ;;; from CMU CL into sbcl-0.6.11.44, the same one which allowed IR1
471 ;;; transforms to be delayed. Either DTC or MNA or both didn't explain
472 ;;; why, and I don't know what the rationale was. -- WHN 2001-04-28
474 ;;; FIXME: It would be good to document why it's important to have a
475 ;;; large value here, and what the drawbacks of an excessively large
476 ;;; value are; and it might also be good to make it depend on
477 ;;; optimization policy.
478 (defparameter *reoptimize-after-type-check-max* 10)
480 (defevent reoptimize-maxed-out
481 "*REOPTIMIZE-AFTER-TYPE-CHECK-MAX* exceeded.")
483 ;;; Iterate doing FIND-DFO until no new dead code is discovered.
484 (defun dfo-as-needed (component)
485 (declare (type component component))
486 (when (component-reanalyze component)
487 (maybe-mumble "DFO")
488 (loop
489 (find-dfo component)
490 (unless (component-reanalyze component)
491 (maybe-mumble " ")
492 (return))
493 (maybe-mumble ".")))
494 (values))
496 ;;; Do all the IR1 phases for a non-top-level component.
497 (defun ir1-phases (component)
498 (declare (type component component))
499 (aver-live-component component)
500 (let ((*constraint-universe* (make-array 64 ; arbitrary, but don't
501 ;make this 0.
502 :fill-pointer 0 :adjustable t))
503 (loop-count 1)
504 (*delayed-ir1-transforms* nil))
505 (declare (special *constraint-universe* *delayed-ir1-transforms*))
506 (loop
507 (ir1-optimize-until-done component)
508 (when (or (component-new-functionals component)
509 (component-reanalyze-functionals component))
510 (maybe-mumble "locall ")
511 (locall-analyze-component component))
512 (dfo-as-needed component)
513 (when *constraint-propagate*
514 (maybe-mumble "constraint ")
515 (constraint-propagate component))
516 (when (retry-delayed-ir1-transforms :constraint)
517 (maybe-mumble "Rtran "))
518 (flet ((want-reoptimization-p ()
519 (or (component-reoptimize component)
520 (component-reanalyze component)
521 (component-new-functionals component)
522 (component-reanalyze-functionals component))))
523 (unless (and (want-reoptimization-p)
524 ;; We delay the generation of type checks until
525 ;; the type constraints have had time to
526 ;; propagate, else the compiler can confuse itself.
527 (< loop-count (- *reoptimize-after-type-check-max* 4)))
528 (maybe-mumble "type ")
529 (generate-type-checks component)
530 (unless (want-reoptimization-p)
531 (return))))
532 (when (>= loop-count *reoptimize-after-type-check-max*)
533 (maybe-mumble "[reoptimize limit]")
534 (event reoptimize-maxed-out)
535 (return))
536 (incf loop-count)))
538 (when *check-consistency*
539 (do-blocks-backwards (block component)
540 (awhen (flush-dead-code block)
541 (let ((*compiler-error-context* it))
542 (compiler-warn "dead code detected at the end of ~S"
543 'ir1-phases)))))
545 (ir1-finalize component)
546 (values))
548 #!+immobile-code
549 (progn
550 (declaim (type (member :immobile :dynamic) *compile-to-memory-space*))
551 ;; COMPILE-FILE puts all nontoplevel code in immobile space, but COMPILE
552 ;; offers a choice. Because the collector does not run often enough (yet),
553 ;; COMPILE usually places code in the dynamic space managed by our copying GC.
554 ;; Change this variable if your application always demands immobile code.
555 ;; The real default is set to :DYNAMIC in make-target-2-load.lisp
556 (defvar *compile-to-memory-space* :immobile) ; BUILD-TIME default
557 (defun code-immobile-p (node-or-component)
558 (if (fasl-output-p *compile-object*)
559 (neq (component-kind (if (node-p node-or-component)
560 (node-component node-or-component)
561 node-or-component))
562 :toplevel)
563 (eq *compile-to-memory-space* :immobile))))
565 (defun %compile-component (component)
566 (let ((*code-segment* nil)
567 (*elsewhere* nil)
568 (*elsewhere-label* nil)
569 #!+inline-constants (*unboxed-constants* nil))
570 (maybe-mumble "GTN ")
571 (gtn-analyze component)
572 (maybe-mumble "LTN ")
573 (ltn-analyze component)
574 (dfo-as-needed component)
575 (maybe-mumble "control ")
576 (control-analyze component #'make-ir2-block)
578 (when (or (ir2-component-values-receivers (component-info component))
579 (component-dx-lvars component))
580 (maybe-mumble "stack ")
581 ;; STACK only uses dominance information for DX LVAR back
582 ;; propagation (see BACK-PROPAGATE-ONE-DX-LVAR).
583 (when (component-dx-lvars component)
584 (find-dominators component))
585 (stack-analyze component)
586 ;; Assign BLOCK-NUMBER for any cleanup blocks introduced by
587 ;; stack analysis. There shouldn't be any unreachable code after
588 ;; control, so this won't delete anything.
589 (dfo-as-needed component))
591 (unwind-protect
592 (progn
593 (maybe-mumble "IR2tran ")
594 (init-assembler)
595 (entry-analyze component)
596 (ir2-convert component)
598 (when (policy *lexenv* (>= speed compilation-speed))
599 (maybe-mumble "copy ")
600 (copy-propagate component))
602 (ir2-optimize component)
604 (select-representations component)
606 (when *check-consistency*
607 (maybe-mumble "check2 ")
608 (check-ir2-consistency component))
610 (delete-unreferenced-tns component)
612 (maybe-mumble "life ")
613 (lifetime-analyze component)
615 (when *compile-progress*
616 (compiler-mumble "") ; Sync before doing more output.
617 (pre-pack-tn-stats component *standard-output*))
619 (when *check-consistency*
620 (maybe-mumble "check-life ")
621 (check-life-consistency component))
623 (maybe-mumble "pack ")
624 (sb!regalloc:pack component)
626 (when *check-consistency*
627 (maybe-mumble "check-pack ")
628 (check-pack-consistency component))
630 (delete-no-op-vops component)
631 (ir2-optimize-jumps component)
632 (optimize-constant-loads component)
633 (when *compiler-trace-output*
634 (describe-component component *compiler-trace-output*)
635 (describe-ir2-component component *compiler-trace-output*))
637 (maybe-mumble "code ")
639 (multiple-value-bind (code-length fixup-notes)
640 (let (#!+immobile-code
641 (*code-is-immobile* (code-immobile-p component)))
642 (generate-code component))
644 #-sb-xc-host
645 (when *compiler-trace-output*
646 (format *compiler-trace-output*
647 "~|~%disassembly of code for ~S~2%" component)
648 (sb!disassem:disassemble-assem-segment *code-segment*
649 *compiler-trace-output*))
651 (etypecase *compile-object*
652 (fasl-output
653 (maybe-mumble "fasl")
654 (fasl-dump-component component
655 *code-segment*
656 code-length
657 fixup-notes
658 *compile-object*))
659 #-sb-xc-host ; no compiling to core
660 (core-object
661 (maybe-mumble "core")
662 (make-core-component component
663 *code-segment*
664 code-length
665 fixup-notes
666 *compile-object*))
667 (null))))))
669 ;; We're done, so don't bother keeping anything around.
670 (setf (component-info component) :dead)
672 (values))
674 ;;; Delete components with no external entry points before we try to
675 ;;; generate code. Unreachable closures can cause IR2 conversion to
676 ;;; puke on itself, since it is the reference to the closure which
677 ;;; normally causes the components to be combined.
678 (defun delete-if-no-entries (component)
679 (dolist (fun (component-lambdas component) (delete-component component))
680 (when (functional-has-external-references-p fun)
681 (return))
682 (case (functional-kind fun)
683 (:toplevel (return))
684 (:external
685 (unless (every (lambda (ref)
686 (eq (node-component ref) component))
687 (leaf-refs fun))
688 (return))))))
690 (defun compile-component (component)
692 ;; miscellaneous sanity checks
694 ;; FIXME: These are basically pretty wimpy compared to the checks done
695 ;; by the old CHECK-IR1-CONSISTENCY code. It would be really nice to
696 ;; make those internal consistency checks work again and use them.
697 (aver-live-component component)
698 (do-blocks (block component)
699 (aver (eql (block-component block) component)))
700 (dolist (lambda (component-lambdas component))
701 ;; sanity check to prevent weirdness from propagating insidiously as
702 ;; far from its root cause as it did in bug 138: Make sure that
703 ;; thing-to-COMPONENT links are consistent.
704 (aver (eql (lambda-component lambda) component))
705 (aver (eql (node-component (lambda-bind lambda)) component)))
707 (let* ((*component-being-compiled* component))
709 ;; Record xref information before optimization. This way the
710 ;; stored xref data reflects the real source as closely as
711 ;; possible.
712 (record-component-xrefs component)
714 (ir1-phases component)
716 (when *loop-analyze*
717 (dfo-as-needed component)
718 (find-dominators component)
719 (loop-analyze component))
722 (when (and *loop-analyze* *compiler-trace-output*)
723 (labels ((print-blocks (block)
724 (format *compiler-trace-output* " ~A~%" block)
725 (when (block-loop-next block)
726 (print-blocks (block-loop-next block))))
727 (print-loop (loop)
728 (format *compiler-trace-output* "loop=~A~%" loop)
729 (print-blocks (loop-blocks loop))
730 (dolist (l (loop-inferiors loop))
731 (print-loop l))))
732 (print-loop (component-outer-loop component))))
735 ;; This should happen at some point before PHYSENV-ANALYZE, and
736 ;; after RECORD-COMPONENT-XREFS. Beyond that, I haven't really
737 ;; thought things through. -- AJB, 2014-Jun-08
738 (eliminate-dead-code component)
740 ;; FIXME: What is MAYBE-MUMBLE for? Do we need it any more?
741 (maybe-mumble "env ")
742 (physenv-analyze component)
743 (dfo-as-needed component)
745 (delete-if-no-entries component)
747 (unless (eq (block-next (component-head component))
748 (component-tail component))
749 (%compile-component component)))
751 (clear-constant-info)
753 (values))
755 ;;;; clearing global data structures
756 ;;;;
757 ;;;; FIXME: Is it possible to get rid of this stuff, getting rid of
758 ;;;; global data structures entirely when possible and consing up the
759 ;;;; others from scratch instead of clearing and reusing them?
761 ;;; Clear the INFO in constants in the *FREE-VARS*, etc. In
762 ;;; addition to allowing stuff to be reclaimed, this is required for
763 ;;; correct assignment of constant offsets, since we need to assign a
764 ;;; new offset for each component. We don't clear the FUNCTIONAL-INFO
765 ;;; slots, since they are used to keep track of functions across
766 ;;; component boundaries.
767 (defun clear-constant-info ()
768 (maphash (lambda (k v)
769 (declare (ignore k))
770 (setf (leaf-info 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 *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) ; becomes *C-F-TRUENAME*
828 :untruename #+sb-xc-host file ; becomes *C-F-PATHNAME*
829 #-sb-xc-host (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 (*compiler-coverage-metadata* (cons (make-hash-table :test 'equal)
1702 (make-hash-table :test 'equal)))
1703 ;; Whether to emit msan unpoisoning code depends on the runtime
1704 ;; value of the feature, not "#+msan", because we can use the target
1705 ;; compiler to compile code for itself which isn't sanitized,
1706 ;; *or* code for another image which is sanitized.
1707 ;; And we can also cross-compile assuming msan.
1708 (*msan-compatible-stack-unpoison*
1709 (member :msan (sb!fasl::fasl-target-features)))
1710 (*handled-conditions* *handled-conditions*)
1711 (*disabled-package-locks* *disabled-package-locks*)
1712 (*lexenv* (make-null-lexenv))
1713 (*block-compile* *block-compile-arg*)
1714 (*toplevel-lambdas* ())
1715 (*fun-names-in-this-file* ())
1716 (*allow-instrumenting* nil)
1717 (*compiler-error-bailout*
1718 (lambda (&optional error)
1719 (declare (ignore error))
1720 (return-from sub-compile-file (values t t t))))
1721 (*current-path* nil)
1722 (*last-format-string* nil)
1723 (*last-format-args* nil)
1724 (*last-message-count* 0)
1725 (*compiler-sset-counter* 0)
1726 (sb!xc:*gensym-counter* 0))
1727 (handler-case
1728 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
1729 (with-compilation-values
1730 (sb!xc:with-compilation-unit ()
1731 (with-world-lock ()
1732 (setf (sb!fasl::fasl-output-source-info *compile-object*)
1733 (debug-source-for-info info))
1734 (sub-sub-compile-file info)
1735 (let ((code-coverage-records (code-coverage-records *compiler-coverage-metadata*)))
1736 (unless (zerop (hash-table-count code-coverage-records))
1737 ;; Dump the code coverage records into the fasl.
1738 (with-source-paths
1739 (fopcompile `(record-code-coverage
1740 ',(namestring *compile-file-pathname*)
1741 ',(let (list)
1742 (maphash (lambda (k v)
1743 (declare (ignore k))
1744 (push v list))
1745 code-coverage-records)
1746 list))
1748 nil))))
1749 (finish-block-compilation)
1750 nil))))
1751 ;; Some errors are sufficiently bewildering that we just fail
1752 ;; immediately, without trying to recover and compile more of
1753 ;; the input file.
1754 (fatal-compiler-error (condition)
1755 (signal condition)
1756 (fresh-line *error-output*)
1757 (pprint-logical-block (*error-output* nil :per-line-prefix "; ")
1758 (format *error-output*
1759 "~@<~@:_compilation aborted because of fatal error: ~2I~_~A~@:_~:>"
1760 (encapsulated-condition condition)))
1761 (finish-output *error-output*)
1762 (values t t t)))))
1764 ;;; Return a pathname for the named file. The file must exist.
1765 (defun verify-source-file (pathname-designator)
1766 (let* ((pathname (pathname pathname-designator))
1767 (default-host (make-pathname :host (pathname-host pathname))))
1768 (flet ((try-with-type (path type error-p)
1769 (let ((new (merge-pathnames
1770 path (make-pathname :type type
1771 :defaults default-host))))
1772 (if (probe-file new)
1774 (and error-p (truename new))))))
1775 (cond ((typep pathname 'logical-pathname)
1776 (try-with-type pathname "LISP" t))
1777 ((probe-file pathname) pathname)
1778 ((try-with-type pathname "lisp" nil))
1779 ((try-with-type pathname "lisp" t))))))
1781 (defun elapsed-time-to-string (internal-time-delta)
1782 (multiple-value-bind (tsec remainder)
1783 (truncate internal-time-delta internal-time-units-per-second)
1784 (let ((ms (truncate remainder (/ internal-time-units-per-second 1000))))
1785 (multiple-value-bind (tmin sec) (truncate tsec 60)
1786 (multiple-value-bind (thr min) (truncate tmin 60)
1787 (format nil "~D:~2,'0D:~2,'0D.~3,'0D" thr min sec ms))))))
1789 ;;; Print some junk at the beginning and end of compilation.
1790 (defun print-compile-start-note (source-info)
1791 (declare (type source-info source-info))
1792 (let ((file-info (source-info-file-info source-info)))
1793 (compiler-mumble #+sb-xc-host "~&; ~A file ~S (written ~A):~%"
1794 #+sb-xc-host (if sb-cold::*compile-for-effect-only*
1795 "preloading"
1796 "cross-compiling")
1797 #-sb-xc-host "~&; compiling file ~S (written ~A):~%"
1798 (namestring (file-info-name file-info))
1799 (sb!int:format-universal-time nil
1800 (file-info-write-date
1801 file-info)
1802 :style :government
1803 :print-weekday nil
1804 :print-timezone nil)))
1805 (values))
1807 (defun print-compile-end-note (source-info won)
1808 (declare (type source-info source-info))
1809 (compiler-mumble "~&; compilation ~:[aborted after~;finished in~] ~A~&"
1811 (elapsed-time-to-string
1812 (- (get-internal-real-time)
1813 (source-info-start-real-time source-info))))
1814 (values))
1816 ;;; Open some files and call SUB-COMPILE-FILE. If something unwinds
1817 ;;; out of the compile, then abort the writing of the output file, so
1818 ;;; that we don't overwrite it with known garbage.
1819 (defun sb!xc:compile-file
1820 (input-file
1821 &key
1823 ;; ANSI options
1824 (output-file (cfp-output-file-default input-file))
1825 ;; FIXME: ANSI doesn't seem to say anything about
1826 ;; *COMPILE-VERBOSE* and *COMPILE-PRINT* being rebound by this
1827 ;; function..
1828 ((:verbose sb!xc:*compile-verbose*) sb!xc:*compile-verbose*)
1829 ((:print sb!xc:*compile-print*) sb!xc:*compile-print*)
1830 (external-format :default)
1832 ;; extensions
1833 (trace-file nil)
1834 ((:block-compile *block-compile-arg*) nil)
1835 (emit-cfasl *emit-cfasl*))
1836 "Compile INPUT-FILE, producing a corresponding fasl file and
1837 returning its filename.
1839 :PRINT
1840 If true, a message per non-macroexpanded top level form is printed
1841 to *STANDARD-OUTPUT*. Top level forms that whose subforms are
1842 processed as top level forms (eg. EVAL-WHEN, MACROLET, PROGN) receive
1843 no such message, but their subforms do.
1845 As an extension to ANSI, if :PRINT is :top-level-forms, a message
1846 per top level form after macroexpansion is printed to *STANDARD-OUTPUT*.
1847 For example, compiling an IN-PACKAGE form will result in a message about
1848 a top level SETQ in addition to the message about the IN-PACKAGE form'
1849 itself.
1851 Both forms of reporting obey the SB-EXT:*COMPILER-PRINT-VARIABLE-ALIST*.
1853 :BLOCK-COMPILE
1854 Though COMPILE-FILE accepts an additional :BLOCK-COMPILE
1855 argument, it is not currently supported. (non-standard)
1857 :TRACE-FILE
1858 If given, internal data structures are dumped to the specified
1859 file, or if a value of T is given, to a file of *.trace type
1860 derived from the input file name. (non-standard)
1862 :EMIT-CFASL
1863 (Experimental). If true, outputs the toplevel compile-time effects
1864 of this file into a separate .cfasl file."
1865 ;;; Block compilation is currently broken.
1867 "Also, as a workaround for vaguely-non-ANSI behavior, the
1868 :BLOCK-COMPILE argument is quasi-supported, to determine whether
1869 multiple functions are compiled together as a unit, resolving function
1870 references at compile time. NIL means that global function names are
1871 never resolved at compilation time. Currently NIL is the default
1872 behavior, because although section 3.2.2.3, \"Semantic Constraints\",
1873 of the ANSI spec allows this behavior under all circumstances, the
1874 compiler's runtime scales badly when it tries to do this for large
1875 files. If/when this performance problem is fixed, the block
1876 compilation default behavior will probably be made dependent on the
1877 SPEED and COMPILATION-SPEED optimization values, and the
1878 :BLOCK-COMPILE argument will probably become deprecated."
1880 (let* ((fasl-output nil)
1881 (cfasl-output nil)
1882 (output-file-name nil)
1883 (coutput-file-name nil)
1884 (abort-p t)
1885 (warnings-p nil)
1886 (failure-p t) ; T in case error keeps this from being set later
1887 (input-pathname (verify-source-file input-file))
1888 (source-info
1889 (make-file-source-info input-pathname external-format
1890 #-sb-xc-host t)) ; can't track, no SBCL streams
1891 (*compiler-trace-output* nil)) ; might be modified below
1893 (unwind-protect
1894 (progn
1895 (when output-file
1896 (setq output-file-name
1897 (sb!xc:compile-file-pathname input-file
1898 :output-file output-file))
1899 (setq fasl-output
1900 (open-fasl-output output-file-name
1901 (namestring input-pathname))))
1902 (when emit-cfasl
1903 (setq coutput-file-name
1904 (make-pathname :type "cfasl"
1905 :defaults output-file-name))
1906 (setq cfasl-output
1907 (open-fasl-output coutput-file-name
1908 (namestring input-pathname))))
1909 (when trace-file
1910 (if (streamp trace-file)
1911 (setf *compiler-trace-output* trace-file)
1912 (let* ((default-trace-file-pathname
1913 (make-pathname :type "trace" :defaults input-pathname))
1914 (trace-file-pathname
1915 (if (eql trace-file t)
1916 default-trace-file-pathname
1917 (merge-pathnames trace-file
1918 default-trace-file-pathname))))
1919 (setf *compiler-trace-output*
1920 (open trace-file-pathname
1921 :if-exists :supersede
1922 :direction :output)))))
1924 (when sb!xc:*compile-verbose*
1925 (print-compile-start-note source-info))
1927 (let ((*compile-object* fasl-output)
1928 (*compile-toplevel-object* cfasl-output))
1929 (setf (values abort-p warnings-p failure-p)
1930 (sub-compile-file source-info))))
1932 (close-source-info source-info)
1934 (when fasl-output
1935 (close-fasl-output fasl-output abort-p)
1936 (setq output-file-name
1937 (pathname (fasl-output-stream fasl-output)))
1938 (when (and (not abort-p) sb!xc:*compile-verbose*)
1939 (compiler-mumble "~2&; ~A written~%" (namestring output-file-name))))
1941 (when cfasl-output
1942 (close-fasl-output cfasl-output abort-p)
1943 (when (and (not abort-p) sb!xc:*compile-verbose*)
1944 (compiler-mumble "; ~A written~%" (namestring coutput-file-name))))
1946 (when sb!xc:*compile-verbose*
1947 (print-compile-end-note source-info (not abort-p)))
1949 (when *compiler-trace-output*
1950 (close *compiler-trace-output*)))
1952 ;; CLHS says that the first value is NIL if the "file could not
1953 ;; be created". We interpret this to mean "a valid fasl could not
1954 ;; be created" -- which can happen if the compilation is aborted
1955 ;; before the whole file has been processed, due to eg. a reader
1956 ;; error.
1957 (values (when (and (not abort-p) output-file)
1958 ;; Hack around filesystem race condition...
1959 (or (probe-file output-file-name) output-file-name))
1960 warnings-p
1961 failure-p)))
1963 ;;; a helper function for COMPILE-FILE-PATHNAME: the default for
1964 ;;; the OUTPUT-FILE argument
1966 ;;; ANSI: The defaults for the OUTPUT-FILE are taken from the pathname
1967 ;;; that results from merging the INPUT-FILE with the value of
1968 ;;; *DEFAULT-PATHNAME-DEFAULTS*, except that the type component should
1969 ;;; default to the appropriate implementation-defined default type for
1970 ;;; compiled files.
1971 (defun cfp-output-file-default (input-file)
1972 (let* ((defaults (merge-pathnames input-file *default-pathname-defaults*))
1973 (retyped (make-pathname :type *fasl-file-type* :defaults defaults)))
1974 retyped))
1976 ;;; KLUDGE: Part of the ANSI spec for this seems contradictory:
1977 ;;; If INPUT-FILE is a logical pathname and OUTPUT-FILE is unsupplied,
1978 ;;; the result is a logical pathname. If INPUT-FILE is a logical
1979 ;;; pathname, it is translated into a physical pathname as if by
1980 ;;; calling TRANSLATE-LOGICAL-PATHNAME.
1981 ;;; So I haven't really tried to make this precisely ANSI-compatible
1982 ;;; at the level of e.g. whether it returns logical pathname or a
1983 ;;; physical pathname. Patches to make it more correct are welcome.
1984 ;;; -- WHN 2000-12-09
1985 (defun sb!xc:compile-file-pathname (input-file
1986 &key
1987 (output-file nil output-file-p)
1988 &allow-other-keys)
1989 "Return a pathname describing what file COMPILE-FILE would write to given
1990 these arguments."
1991 (if output-file-p
1992 (merge-pathnames output-file (cfp-output-file-default input-file))
1993 (cfp-output-file-default input-file)))
1995 ;;;; MAKE-LOAD-FORM stuff
1997 ;;; The entry point for MAKE-LOAD-FORM support. When IR1 conversion
1998 ;;; finds a constant structure, it invokes this to arrange for proper
1999 ;;; dumping. If it turns out that the constant has already been
2000 ;;; dumped, then we don't need to do anything.
2002 ;;; If the constant hasn't been dumped, then we check to see whether
2003 ;;; we are in the process of creating it. We detect this by
2004 ;;; maintaining the special *CONSTANTS-BEING-CREATED* as a list of all
2005 ;;; the constants we are in the process of creating. Actually, each
2006 ;;; entry is a list of the constant and any init forms that need to be
2007 ;;; processed on behalf of that constant.
2009 ;;; It's not necessarily an error for this to happen. If we are
2010 ;;; processing the init form for some object that showed up *after*
2011 ;;; the original reference to this constant, then we just need to
2012 ;;; defer the processing of that init form. To detect this, we
2013 ;;; maintain *CONSTANTS-CREATED-SINCE-LAST-INIT* as a list of the
2014 ;;; constants created since the last time we started processing an
2015 ;;; init form. If the constant passed to emit-make-load-form shows up
2016 ;;; in this list, then there is a circular chain through creation
2017 ;;; forms, which is an error.
2019 ;;; If there is some intervening init form, then we blow out of
2020 ;;; processing it by throwing to the tag PENDING-INIT. The value we
2021 ;;; throw is the entry from *CONSTANTS-BEING-CREATED*. This is so the
2022 ;;; offending init form can be tacked onto the init forms for the
2023 ;;; circular object.
2025 ;;; If the constant doesn't show up in *CONSTANTS-BEING-CREATED*, then
2026 ;;; we have to create it. We call %MAKE-LOAD-FORM and check
2027 ;;; if the result is 'FOP-STRUCT, and if so we don't do anything.
2028 ;;; The dumper will eventually get its hands on the object and use the
2029 ;;; normal structure dumping noise on it.
2031 ;;; Otherwise, we bind *CONSTANTS-BEING-CREATED* and
2032 ;;; *CONSTANTS-CREATED-SINCE- LAST-INIT* and compile the creation form
2033 ;;; much the way LOAD-TIME-VALUE does. When this finishes, we tell the
2034 ;;; dumper to use that result instead whenever it sees this constant.
2036 ;;; Now we try to compile the init form. We bind
2037 ;;; *CONSTANTS-CREATED-SINCE-LAST-INIT* to NIL and compile the init
2038 ;;; form (and any init forms that were added because of circularity
2039 ;;; detection). If this works, great. If not, we add the init forms to
2040 ;;; the init forms for the object that caused the problems and let it
2041 ;;; deal with it.
2042 (defvar *constants-being-created* nil)
2043 (defvar *constants-created-since-last-init* nil)
2044 ;;; FIXME: Shouldn't these^ variables be unbound outside LET forms?
2045 (defun emit-make-load-form (constant &optional (name nil namep)
2046 &aux (fasl *compile-object*))
2047 (aver (fasl-output-p fasl))
2048 (unless (fasl-constant-already-dumped-p constant fasl)
2049 (let ((circular-ref (assoc constant *constants-being-created* :test #'eq)))
2050 (when circular-ref
2051 (when (find constant *constants-created-since-last-init* :test #'eq)
2052 (throw constant t))
2053 (throw 'pending-init circular-ref)))
2054 ;; If this is a global constant reference, we can call SYMBOL-GLOBAL-VALUE
2055 ;; during LOAD as a fasl op, and not compile a lambda.
2056 (when namep
2057 (fopcompile `(symbol-global-value ',name) nil t nil)
2058 (fasl-note-handle-for-constant constant (sb!fasl::dump-pop fasl) fasl)
2059 (return-from emit-make-load-form nil))
2060 (multiple-value-bind (creation-form init-form) (%make-load-form constant)
2061 (case creation-form
2062 (sb!fasl::fop-struct
2063 (fasl-validate-structure constant fasl)
2065 (:ignore-it
2066 nil)
2068 (let* ((name (write-to-string constant :level 1 :length 2))
2069 (info (if init-form
2070 (list constant name init-form)
2071 (list constant))))
2072 (let ((*constants-being-created*
2073 (cons info *constants-being-created*))
2074 (*constants-created-since-last-init*
2075 (cons constant *constants-created-since-last-init*)))
2076 (when
2077 (catch constant
2078 (fasl-note-handle-for-constant
2079 constant
2080 (cond ((typep creation-form
2081 '(cons (eql sb!kernel::new-instance)
2082 (cons symbol null)))
2083 (dump-object (cadr creation-form) fasl)
2084 (dump-fop 'sb!fasl::fop-allocate-instance fasl)
2085 (let ((index (sb!fasl::fasl-output-table-free fasl)))
2086 (setf (sb!fasl::fasl-output-table-free fasl) (1+ index))
2087 index))
2089 (compile-load-time-value creation-form t)))
2090 fasl)
2091 nil)
2092 (compiler-error "circular references in creation form for ~S"
2093 constant)))
2094 (when (cdr info)
2095 (let* ((*constants-created-since-last-init* nil)
2096 (circular-ref
2097 (catch 'pending-init
2098 (loop for (nil form) on (cdr info) by #'cddr
2099 collect form into forms
2100 finally (compile-make-load-form-init-forms forms fasl))
2101 nil)))
2102 (when circular-ref
2103 (setf (cdr circular-ref)
2104 (append (cdr circular-ref) (cdr info)))))))
2105 nil)))))
2108 ;;;; Host compile time definitions
2109 #+sb-xc-host
2110 (defun compile-in-lexenv (lambda lexenv &rest rest)
2111 (declare (ignore lexenv))
2112 (aver (null rest))
2113 (compile nil lambda))
2115 #+sb-xc-host
2116 (defun eval-tlf (form index &optional lexenv)
2117 (declare (ignore index lexenv))
2118 (eval form))