1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 (declaim (special *compiler-error-bailout*))
17 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
18 ;;; taken through the source to reach the form. This provides a way to
19 ;;; keep track of the location of original source forms, even when
20 ;;; macroexpansions and other arbitary permutations of the code
21 ;;; happen. This table is initialized by calling FIND-SOURCE-PATHS on
22 ;;; the original source.
23 (declaim (hash-table *source-paths*))
24 (defvar *source-paths*)
26 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
27 ;;; blocks into as we generate them. This just serves to glue the
28 ;;; emitted blocks together until local call analysis and flow graph
29 ;;; canonicalization figure out what is really going on. We need to
30 ;;; keep track of all the blocks generated so that we can delete them
31 ;;; if they turn out to be unreachable.
33 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
34 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
35 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
36 ;;; which was also confusing.)
37 (declaim (type (or component null) *current-component*))
38 (defvar *current-component*)
40 ;;; *CURRENT-PATH* is the source path of the form we are currently
41 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
42 (declaim (list *current-path*))
43 (defvar *current-path*)
45 (defvar *derive-function-types* nil
46 "Should the compiler assume that function types will never change,
47 so that it can use type information inferred from current definitions
48 to optimize code which uses those definitions? Setting this true
49 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
50 the efficiency of stable code.")
52 (defvar *fun-names-in-this-file* nil)
54 ;;; *ALLOW-DEBUG-CATCH-TAG* controls whether we should allow the
55 ;;; insertion a (CATCH ...) around code to allow the debugger RETURN
56 ;;; command to function.
57 (defvar *allow-debug-catch-tag* t)
59 ;;;; namespace management utilities
61 (defun fun-lexically-notinline-p (name)
62 (let ((fun (lexenv-find name funs :test #'equal)))
63 ;; a declaration will trump a proclamation
64 (if (and fun (defined-fun-p fun))
65 (eq (defined-fun-inlinep fun) :notinline)
66 (eq (info :function :inlinep name) :notinline))))
68 ;;; Return a GLOBAL-VAR structure usable for referencing the global
70 (defun find-free-really-fun (name)
71 (unless (info :function :kind name)
72 (setf (info :function :kind name) :function)
73 (setf (info :function :where-from name) :assumed))
75 (let ((where (info :function :where-from name)))
76 (when (and (eq where :assumed)
77 ;; In the ordinary target Lisp, it's silly to report
78 ;; undefinedness when the function is defined in the
79 ;; running Lisp. But at cross-compile time, the current
80 ;; definedness of a function is irrelevant to the
81 ;; definedness at runtime, which is what matters.
82 #-sb-xc-host (not (fboundp name)))
83 (note-undefined-reference name :function))
85 :kind :global-function
87 :type (if (or *derive-function-types*
89 (and (member name *fun-names-in-this-file* :test #'equal)
90 (not (fun-lexically-notinline-p name))))
91 (info :function :type name)
92 (specifier-type 'function))
95 ;;; Has the *FREE-FUNS* entry FREE-FUN become invalid?
97 ;;; In CMU CL, the answer was implicitly always true, so this
98 ;;; predicate didn't exist.
100 ;;; This predicate was added to fix bug 138 in SBCL. In some obscure
101 ;;; circumstances, it was possible for a *FREE-FUNS* entry to contain a
102 ;;; DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object contained IR1
103 ;;; stuff (NODEs, BLOCKs...) referring to an already compiled (aka
104 ;;; "dead") component. When this IR1 stuff was reused in a new
105 ;;; component, under further obscure circumstances it could be used by
106 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
107 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since
108 ;;; IR1 conversion was sending code to a component which had already
109 ;;; been compiled and would never be compiled again.
110 (defun invalid-free-fun-p (free-fun)
111 ;; There might be other reasons that *FREE-FUN* entries could
112 ;; become invalid, but the only one we've been bitten by so far
113 ;; (sbcl-0.pre7.118) is this one:
114 (and (defined-fun-p free-fun)
115 (let ((functional (defined-fun-functional free-fun)))
117 (eql (functional-kind functional) :deleted))
118 (and (lambda-p functional)
120 ;; (The main reason for this first test is to bail
121 ;; out early in cases where the LAMBDA-COMPONENT
122 ;; call in the second test would fail because links
123 ;; it needs are uninitialized or invalid.)
125 ;; If the BIND node for this LAMBDA is null, then
126 ;; according to the slot comments, the LAMBDA has
127 ;; been deleted or its call has been deleted. In
128 ;; that case, it seems rather questionable to reuse
129 ;; it, and certainly it shouldn't be necessary to
130 ;; reuse it, so we cheerfully declare it invalid.
131 (null (lambda-bind functional))
132 ;; If this IR1 stuff belongs to a dead component,
133 ;; then we can't reuse it without getting into
134 ;; bizarre confusion.
135 (eql (component-info (lambda-component functional))
138 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
139 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
140 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
141 ;;; names a macro or special form, then we error out using the
142 ;;; supplied context which indicates what we were trying to do that
143 ;;; demanded a function.
144 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
145 (defun find-free-fun (name context)
146 (or (let ((old-free-fun (gethash name *free-funs*)))
147 (and (not (invalid-free-fun-p old-free-fun))
149 (ecase (info :function :kind name)
150 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
152 (compiler-error "The macro name ~S was found ~A." name context))
154 (compiler-error "The special form name ~S was found ~A."
158 (check-fun-name name)
159 (note-if-setf-fun-and-macro name)
160 (let ((expansion (fun-name-inline-expansion name))
161 (inlinep (info :function :inlinep name)))
162 (setf (gethash name *free-funs*)
163 (if (or expansion inlinep)
166 :inline-expansion expansion
168 :where-from (info :function :where-from name)
169 :type (if (eq inlinep :notinline)
170 (specifier-type 'function)
171 (info :function :type name)))
172 (find-free-really-fun name))))))))
174 ;;; Return the LEAF structure for the lexically apparent function
175 ;;; definition of NAME.
176 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
177 (defun find-lexically-apparent-fun (name context)
178 (let ((var (lexenv-find name funs :test #'equal)))
181 (aver (and (consp var) (eq (car var) 'macro)))
182 (compiler-error "found macro name ~S ~A" name context))
185 (find-free-fun name context)))))
187 ;;; Return the LEAF node for a global variable reference to NAME. If
188 ;;; NAME is already entered in *FREE-VARS*, then we just return the
189 ;;; corresponding value. Otherwise, we make a new leaf using
190 ;;; information from the global environment and enter it in
191 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
192 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
193 (defun find-free-var (name)
194 (unless (symbolp name)
195 (compiler-error "Variable name is not a symbol: ~S." name))
196 (or (gethash name *free-vars*)
197 (let ((kind (info :variable :kind name))
198 (type (info :variable :type name))
199 (where-from (info :variable :where-from name)))
200 (when (and (eq where-from :assumed) (eq kind :global))
201 (note-undefined-reference name :variable))
202 (setf (gethash name *free-vars*)
205 (info :variable :alien-info name))
206 ;; FIXME: The return value in this case should really be
207 ;; of type SB!C::LEAF. I don't feel too badly about it,
208 ;; because the MACRO idiom is scattered throughout this
209 ;; file, but it should be cleaned up so we're not
210 ;; throwing random conses around. --njf 2002-03-23
212 (let ((expansion (info :variable :macro-expansion name))
213 (type (type-specifier (info :variable :type name))))
214 `(MACRO . (the ,type ,expansion))))
216 (let ((value (info :variable :constant-value name)))
217 (make-constant :value value
219 :type (ctype-of value)
220 :where-from where-from)))
222 (make-global-var :kind kind
225 :where-from where-from)))))))
227 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
228 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
229 ;;; CONSTANT might be circular. We also check that the constant (and
230 ;;; any subparts) are dumpable at all.
231 (eval-when (:compile-toplevel :load-toplevel :execute)
232 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
233 ;; below. -- AL 20010227
234 (def!constant list-to-hash-table-threshold 32))
235 (defun maybe-emit-make-load-forms (constant)
236 (let ((things-processed nil)
238 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
239 (declare (type (or list hash-table) things-processed)
240 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
242 (labels ((grovel (value)
243 ;; Unless VALUE is an object which which obviously
244 ;; can't contain other objects
246 '(or #-sb-xc-host unboxed-array
251 (etypecase things-processed
253 (when (member value things-processed :test #'eq)
254 (return-from grovel nil))
255 (push value things-processed)
257 (when (> count list-to-hash-table-threshold)
258 (let ((things things-processed))
259 (setf things-processed
260 (make-hash-table :test 'eq))
261 (dolist (thing things)
262 (setf (gethash thing things-processed) t)))))
264 (when (gethash value things-processed)
265 (return-from grovel nil))
266 (setf (gethash value things-processed) t)))
270 (grovel (cdr value)))
272 (dotimes (i (length value))
273 (grovel (svref value i))))
275 (dotimes (i (length value))
276 (grovel (aref value i))))
278 ;; Even though the (ARRAY T) branch does the exact
279 ;; same thing as this branch we do this separately
280 ;; so that the compiler can use faster versions of
281 ;; array-total-size and row-major-aref.
282 (dotimes (i (array-total-size value))
283 (grovel (row-major-aref value i))))
285 (dotimes (i (array-total-size value))
286 (grovel (row-major-aref value i))))
287 (;; In the target SBCL, we can dump any instance,
288 ;; but in the cross-compilation host,
289 ;; %INSTANCE-FOO functions don't work on general
290 ;; instances, only on STRUCTURE!OBJECTs.
291 #+sb-xc-host structure!object
292 #-sb-xc-host instance
293 (when (emit-make-load-form value)
294 (dotimes (i (%instance-length value))
295 (grovel (%instance-ref value i)))))
298 "Objects of type ~S can't be dumped into fasl files."
299 (type-of value)))))))
303 ;;;; some flow-graph hacking utilities
305 ;;; This function sets up the back link between the node and the
306 ;;; continuation which continues at it.
307 (defun link-node-to-previous-continuation (node cont)
308 (declare (type node node) (type continuation cont))
309 (aver (not (continuation-next cont)))
310 (setf (continuation-next cont) node)
311 (setf (node-prev node) cont))
313 ;;; This function is used to set the continuation for a node, and thus
314 ;;; determine what receives the value and what is evaluated next. If
315 ;;; the continuation has no block, then we make it be in the block
316 ;;; that the node is in. If the continuation heads its block, we end
317 ;;; our block and link it to that block. If the continuation is not
318 ;;; currently used, then we set the DERIVED-TYPE for the continuation
319 ;;; to that of the node, so that a little type propagation gets done.
320 #!-sb-fluid (declaim (inline use-continuation))
321 (defun use-continuation (node cont)
322 (declare (type node node) (type continuation cont))
323 (let ((node-block (continuation-block (node-prev node))))
324 (case (continuation-kind cont)
326 (setf (continuation-block cont) node-block)
327 (setf (continuation-kind cont) :inside-block)
328 (setf (continuation-use cont) node)
329 (setf (node-cont node) cont))
331 (%use-continuation node cont)))))
332 (defun %use-continuation (node cont)
333 (declare (type node node) (type continuation cont) (inline member))
334 (let ((block (continuation-block cont))
335 (node-block (continuation-block (node-prev node))))
336 (aver (eq (continuation-kind cont) :block-start))
337 (when (block-last node-block)
338 (error "~S has already ended." node-block))
339 (setf (block-last node-block) node)
340 (when (block-succ node-block)
341 (error "~S already has successors." node-block))
342 (setf (block-succ node-block) (list block))
343 (when (memq node-block (block-pred block))
344 (error "~S is already a predecessor of ~S." node-block block))
345 (push node-block (block-pred block))
346 (add-continuation-use node cont)
347 (reoptimize-continuation cont)))
349 ;;;; exported functions
351 ;;; This function takes a form and the top level form number for that
352 ;;; form, and returns a lambda representing the translation of that
353 ;;; form in the current global environment. The returned lambda is a
354 ;;; top level lambda that can be called to cause evaluation of the
355 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
356 ;;; then the value of the form is returned from the function,
357 ;;; otherwise NIL is returned.
359 ;;; This function may have arbitrary effects on the global environment
360 ;;; due to processing of EVAL-WHENs. All syntax error checking is
361 ;;; done, with erroneous forms being replaced by a proxy which signals
362 ;;; an error if it is evaluated. Warnings about possibly inconsistent
363 ;;; or illegal changes to the global environment will also be given.
365 ;;; We make the initial component and convert the form in a PROGN (and
366 ;;; an optional NIL tacked on the end.) We then return the lambda. We
367 ;;; bind all of our state variables here, rather than relying on the
368 ;;; global value (if any) so that IR1 conversion will be reentrant.
369 ;;; This is necessary for EVAL-WHEN processing, etc.
371 ;;; The hashtables used to hold global namespace info must be
372 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
373 ;;; that local macro definitions can be introduced by enclosing code.
374 (defun ir1-toplevel (form path for-value)
375 (declare (list path))
376 (let* ((*current-path* path)
377 (component (make-empty-component))
378 (*current-component* component))
379 (setf (component-name component) "initial component")
380 (setf (component-kind component) :initial)
381 (let* ((forms (if for-value `(,form) `(,form nil)))
382 (res (ir1-convert-lambda-body
384 :debug-name (debug-namify "top level form ~S" form))))
385 (setf (functional-entry-fun res) res
386 (functional-arg-documentation res) ()
387 (functional-kind res) :toplevel)
390 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
391 ;;; form number to associate with a source path. This should be bound
392 ;;; to an initial value of 0 before the processing of each truly
394 (declaim (type index *current-form-number*))
395 (defvar *current-form-number*)
397 ;;; This function is called on freshly read forms to record the
398 ;;; initial location of each form (and subform.) Form is the form to
399 ;;; find the paths in, and TLF-NUM is the top level form number of the
400 ;;; truly top level form.
402 ;;; This gets a bit interesting when the source code is circular. This
403 ;;; can (reasonably?) happen in the case of circular list constants.
404 (defun find-source-paths (form tlf-num)
405 (declare (type index tlf-num))
406 (let ((*current-form-number* 0))
407 (sub-find-source-paths form (list tlf-num)))
409 (defun sub-find-source-paths (form path)
410 (unless (gethash form *source-paths*)
411 (setf (gethash form *source-paths*)
412 (list* 'original-source-start *current-form-number* path))
413 (incf *current-form-number*)
417 (declare (fixnum pos))
420 (when (atom subform) (return))
421 (let ((fm (car subform)))
423 (sub-find-source-paths fm (cons pos path)))
425 (setq subform (cdr subform))
426 (when (eq subform trail) (return)))))
430 (setq trail (cdr trail)))))))
432 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
434 (declaim (ftype (sfunction (continuation continuation t) (values))
436 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
437 ;; out of the body and converts a proxy form instead.
438 (ir1-error-bailout ((start
442 (proxy ``(error 'simple-program-error
443 :format-control "execution of a form compiled with errors:~% ~S"
444 :format-arguments (list ',,form))))
446 (with-unique-names (skip)
448 (catch 'ir1-error-abort
449 (let ((*compiler-error-bailout*
451 (throw 'ir1-error-abort nil))))
453 (return-from ,skip nil)))
454 (ir1-convert ,start ,cont ,proxy)))))
456 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
457 ;; continuation START. CONT is the continuation which receives the
458 ;; value of the FORM to be translated. The translators call this
459 ;; function recursively to translate their subnodes.
461 ;; As a special hack to make life easier in the compiler, a LEAF
462 ;; IR1-converts into a reference to that LEAF structure. This allows
463 ;; the creation using backquote of forms that contain leaf
464 ;; references, without having to introduce dummy names into the
466 (defun ir1-convert (start cont form)
467 (ir1-error-bailout (start cont form)
468 (let ((*current-path* (or (gethash form *source-paths*)
469 (cons form *current-path*))))
471 (cond ((and (symbolp form) (not (keywordp form)))
472 (ir1-convert-var start cont form))
474 (reference-leaf start cont form))
476 (reference-constant start cont form)))
477 (let ((opname (car form)))
478 (cond ((or (symbolp opname) (leaf-p opname))
479 (let ((lexical-def (if (leaf-p opname)
481 (lexenv-find opname funs))))
482 (typecase lexical-def
483 (null (ir1-convert-global-functoid start cont form))
485 (ir1-convert-local-combination start
490 (ir1-convert-srctran start cont lexical-def form))
492 (aver (and (consp lexical-def)
493 (eq (car lexical-def) 'macro)))
494 (ir1-convert start cont
495 (careful-expand-macro (cdr lexical-def)
497 ((or (atom opname) (not (eq (car opname) 'lambda)))
498 (compiler-error "illegal function call"))
500 ;; implicitly (LAMBDA ..) because the LAMBDA
501 ;; expression is the CAR of an executed form
502 (ir1-convert-combination start
507 :debug-name (debug-namify
510 :allow-debug-catch-tag t))))))))
513 ;; Generate a reference to a manifest constant, creating a new leaf
514 ;; if necessary. If we are producing a fasl file, make sure that
515 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
517 (defun reference-constant (start cont value)
518 (declare (type continuation start cont)
519 (inline find-constant))
521 (start cont value '(error "attempt to reference undumpable constant"))
522 (when (producing-fasl-file)
523 (maybe-emit-make-load-forms value))
524 (let* ((leaf (find-constant value))
525 (res (make-ref leaf)))
526 (push res (leaf-refs leaf))
527 (link-node-to-previous-continuation res start)
528 (use-continuation res cont)))
531 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
532 ;;; some trivial type for which reanalysis is a trivial no-op, or
533 ;;; unless it doesn't belong in this component at all.
535 ;;; FUNCTIONAL is returned.
536 (defun maybe-reanalyze-functional (functional)
538 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
539 (aver-live-component *current-component*)
541 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
543 (when (typep functional '(or optional-dispatch clambda))
545 ;; When FUNCTIONAL knows its component
546 (when (lambda-p functional)
547 (aver (eql (lambda-component functional) *current-component*)))
550 (component-reanalyze-functionals *current-component*)))
554 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
555 ;;; needed. If LEAF represents a defined function which has already
556 ;;; been converted, and is not :NOTINLINE, then reference the
557 ;;; functional instead.
558 (defun reference-leaf (start cont leaf)
559 (declare (type continuation start cont) (type leaf leaf))
560 (when (functional-p leaf)
561 (assure-functional-live-p leaf))
562 (let* ((type (lexenv-find leaf type-restrictions))
563 (leaf (or (and (defined-fun-p leaf)
564 (not (eq (defined-fun-inlinep leaf)
566 (let ((functional (defined-fun-functional leaf)))
567 (when (and functional
568 (not (functional-kind functional)))
569 (maybe-reanalyze-functional functional))))
570 (when (and (lambda-p leaf)
571 (memq (functional-kind leaf)
573 (maybe-reanalyze-functional leaf))
575 (ref (make-ref leaf)))
576 (push ref (leaf-refs leaf))
577 (setf (leaf-ever-used leaf) t)
578 (link-node-to-previous-continuation ref start)
579 (cond (type (let* ((ref-cont (make-continuation))
580 (cast (make-cast ref-cont
581 (make-single-value-type type)
582 (lexenv-policy *lexenv*))))
583 (setf (continuation-dest ref-cont) cast)
584 (use-continuation ref ref-cont)
585 (link-node-to-previous-continuation cast ref-cont)
586 (use-continuation cast cont)))
587 (t (use-continuation ref cont)))))
589 ;;; Convert a reference to a symbolic constant or variable. If the
590 ;;; symbol is entered in the LEXENV-VARS we use that definition,
591 ;;; otherwise we find the current global definition. This is also
592 ;;; where we pick off symbol macro and alien variable references.
593 (defun ir1-convert-var (start cont name)
594 (declare (type continuation start cont) (symbol name))
595 (let ((var (or (lexenv-find name vars) (find-free-var name))))
598 (when (lambda-var-p var)
599 (let ((home (continuation-home-lambda-or-null start)))
601 (pushnew var (lambda-calls-or-closes home))))
602 (when (lambda-var-ignorep var)
603 ;; (ANSI's specification for the IGNORE declaration requires
604 ;; that this be a STYLE-WARNING, not a full WARNING.)
605 (compiler-style-warn "reading an ignored variable: ~S" name)))
606 (reference-leaf start cont var))
608 (aver (eq (car var) 'MACRO))
609 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
610 (ir1-convert start cont (cdr var)))
612 (ir1-convert start cont `(%heap-alien ',var)))))
615 ;;; Convert anything that looks like a special form, global function
616 ;;; or compiler-macro call.
617 (defun ir1-convert-global-functoid (start cont form)
618 (declare (type continuation start cont) (list form))
619 (let* ((fun-name (first form))
620 (translator (info :function :ir1-convert fun-name))
621 (cmacro-fun (sb!xc:compiler-macro-function fun-name *lexenv*)))
624 (compiler-warn "ignoring compiler macro for special form"))
625 (funcall translator start cont form))
627 ;; gotcha: If you look up the DEFINE-COMPILER-MACRO
628 ;; macro in the ANSI spec, you might think that
629 ;; suppressing compiler-macro expansion when NOTINLINE
630 ;; is some pre-ANSI hack. However, if you look up the
631 ;; NOTINLINE declaration, you'll find that ANSI
632 ;; requires this behavior after all.
633 (not (eq (info :function :inlinep fun-name) :notinline)))
634 (let ((res (careful-expand-macro cmacro-fun form)))
636 (ir1-convert-global-functoid-no-cmacro
637 start cont form fun-name)
638 (ir1-convert start cont res))))
640 (ir1-convert-global-functoid-no-cmacro start cont form fun-name)))))
642 ;;; Handle the case of where the call was not a compiler macro, or was
643 ;;; a compiler macro and passed.
644 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
645 (declare (type continuation start cont) (list form))
646 ;; FIXME: Couldn't all the INFO calls here be converted into
647 ;; standard CL functions, like MACRO-FUNCTION or something?
648 ;; And what happens with lexically-defined (MACROLET) macros
650 (ecase (info :function :kind fun)
654 (careful-expand-macro (info :function :macro-function fun)
657 (ir1-convert-srctran start
659 (find-free-fun fun "shouldn't happen! (no-cmacro)")
662 (defun muffle-warning-or-die ()
664 (bug "no MUFFLE-WARNING restart"))
666 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
667 ;;; errors which occur during the macroexpansion.
668 (defun careful-expand-macro (fun form)
669 (let (;; a hint I (WHN) wish I'd known earlier
670 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
671 (flet (;; Return a string to use as a prefix in error reporting,
672 ;; telling something about which form caused the problem.
674 (let ((*print-pretty* nil)
675 ;; We rely on the printer to abbreviate FORM.
680 #-sb-xc-host "(in macroexpansion of ~S)"
681 ;; longer message to avoid ambiguity "Was it the xc host
682 ;; or the cross-compiler which encountered the problem?"
683 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
685 (handler-bind ((style-warning (lambda (c)
687 "~@<~A~:@_~A~@:_~A~:>"
688 (wherestring) hint c)
689 (muffle-warning-or-die)))
690 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
691 ;; Debian Linux, anyway) raises a CL:WARNING
692 ;; condition (not a CL:STYLE-WARNING) for undefined
693 ;; symbols when converting interpreted functions,
694 ;; causing COMPILE-FILE to think the file has a real
695 ;; problem, causing COMPILE-FILE to return FAILURE-P
696 ;; set (not just WARNINGS-P set). Since undefined
697 ;; symbol warnings are often harmless forward
698 ;; references, and since it'd be inordinately painful
699 ;; to try to eliminate all such forward references,
700 ;; these warnings are basically unavoidable. Thus, we
701 ;; need to coerce the system to work through them,
702 ;; and this code does so, by crudely suppressing all
703 ;; warnings in cross-compilation macroexpansion. --
705 #+(and cmu sb-xc-host)
710 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
711 Ordinarily that would cause compilation to ~
712 fail. However, since we're running under ~
713 CMU CL, and since CMU CL emits non-STYLE ~
714 warnings for safe, hard-to-fix things (e.g. ~
715 references to not-yet-defined functions) ~
716 we're going to have to ignore it and ~
717 proceed anyway. Hopefully we're not ~
718 ignoring anything horrible here..)~:@>~:>"
721 (muffle-warning-or-die)))
722 #-(and cmu sb-xc-host)
724 (compiler-warn "~@<~A~:@_~A~@:_~A~:>"
725 (wherestring) hint c)
726 (muffle-warning-or-die)))
728 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
729 (wherestring) hint c))))
730 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
732 ;;;; conversion utilities
734 ;;; Convert a bunch of forms, discarding all the values except the
735 ;;; last. If there aren't any forms, then translate a NIL.
736 (declaim (ftype (sfunction (continuation continuation list) (values))
737 ir1-convert-progn-body))
738 (defun ir1-convert-progn-body (start cont body)
740 (reference-constant start cont nil)
741 (let ((this-start start)
744 (let ((form (car forms)))
745 (when (endp (cdr forms))
746 (ir1-convert this-start cont form)
748 (let ((this-cont (make-continuation)))
749 (ir1-convert this-start this-cont form)
750 (setq this-start this-cont
751 forms (cdr forms)))))))
754 ;;;; converting combinations
756 ;;; Convert a function call where the function FUN is a LEAF. FORM is
757 ;;; the source for the call. We return the COMBINATION node so that
758 ;;; the caller can poke at it if it wants to.
759 (declaim (ftype (sfunction (continuation continuation list leaf) combination)
760 ir1-convert-combination))
761 (defun ir1-convert-combination (start cont form fun)
762 (let ((fun-cont (make-continuation)))
763 (ir1-convert start fun-cont `(the (or function symbol) ,fun))
764 (ir1-convert-combination-args fun-cont cont (cdr form))))
766 ;;; Convert the arguments to a call and make the COMBINATION
767 ;;; node. FUN-CONT is the continuation which yields the function to
768 ;;; call. ARGS is the list of arguments for the call, which defaults
769 ;;; to the cdr of source. We return the COMBINATION node.
770 (defun ir1-convert-combination-args (fun-cont cont args)
771 (declare (type continuation fun-cont cont) (list args))
772 (let ((node (make-combination fun-cont)))
773 (setf (continuation-dest fun-cont) node)
774 (collect ((arg-conts))
775 (let ((this-start fun-cont))
777 (let ((this-cont (make-continuation node)))
778 (ir1-convert this-start this-cont arg)
779 (setq this-start this-cont)
780 (arg-conts this-cont)))
781 (link-node-to-previous-continuation node this-start)
782 (use-continuation node cont)
783 (setf (combination-args node) (arg-conts))))
786 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
787 ;;; source transforms and try out any inline expansion. If there is no
788 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
789 ;;; known function which will quite possibly be open-coded.) Next, we
790 ;;; go to ok-combination conversion.
791 (defun ir1-convert-srctran (start cont var form)
792 (declare (type continuation start cont) (type global-var var))
793 (let ((inlinep (when (defined-fun-p var)
794 (defined-fun-inlinep var))))
795 (if (eq inlinep :notinline)
796 (ir1-convert-combination start cont form var)
797 (let ((transform (info :function
799 (leaf-source-name var))))
801 (multiple-value-bind (result pass) (funcall transform form)
803 (ir1-convert-maybe-predicate start cont form var)
804 (ir1-convert start cont result)))
805 (ir1-convert-maybe-predicate start cont form var))))))
807 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
808 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
809 ;;; predicate always appears in a conditional context.
811 ;;; If the function isn't a predicate, then we call
812 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
813 (defun ir1-convert-maybe-predicate (start cont form var)
814 (declare (type continuation start cont) (list form) (type global-var var))
815 (let ((info (info :function :info (leaf-source-name var))))
817 (ir1-attributep (fun-info-attributes info) predicate)
818 (not (if-p (continuation-dest cont))))
819 (ir1-convert start cont `(if ,form t nil))
820 (ir1-convert-combination-checking-type start cont form var))))
822 ;;; Actually really convert a global function call that we are allowed
825 ;;; If we know the function type of the function, then we check the
826 ;;; call for syntactic legality with respect to the declared function
827 ;;; type. If it is impossible to determine whether the call is correct
828 ;;; due to non-constant keywords, then we give up, marking the call as
829 ;;; :FULL to inhibit further error messages. We return true when the
832 ;;; If the call is legal, we also propagate type assertions from the
833 ;;; function type to the arg and result continuations. We do this now
834 ;;; so that IR1 optimize doesn't have to redundantly do the check
835 ;;; later so that it can do the type propagation.
836 (defun ir1-convert-combination-checking-type (start cont form var)
837 (declare (type continuation start cont) (list form) (type leaf var))
838 (let* ((node (ir1-convert-combination start cont form var))
839 (fun-cont (basic-combination-fun node))
840 (type (leaf-type var)))
841 (when (validate-call-type node type t)
842 (setf (continuation-%derived-type fun-cont)
843 (make-single-value-type type))
844 (setf (continuation-reoptimize fun-cont) nil)))
847 ;;; Convert a call to a local function, or if the function has already
848 ;;; been LET converted, then throw FUNCTIONAL to
849 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
850 ;;; are converting inline expansions for local functions during
852 (defun ir1-convert-local-combination (start cont form functional)
853 (assure-functional-live-p functional)
854 (ir1-convert-combination start
857 (maybe-reanalyze-functional functional)))
861 ;;; Given a list of LAMBDA-VARs and a variable name, return the
862 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
863 ;;; *last* variable with that name, since LET* bindings may be
864 ;;; duplicated, and declarations always apply to the last.
865 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
867 (defun find-in-bindings (vars name)
871 (when (eq (leaf-source-name var) name)
873 (let ((info (lambda-var-arg-info var)))
875 (let ((supplied-p (arg-info-supplied-p info)))
876 (when (and supplied-p
877 (eq (leaf-source-name supplied-p) name))
878 (setq found supplied-p))))))
879 ((and (consp var) (eq (car var) name))
880 (setf found (cdr var)))))
883 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
884 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
885 ;;; type, otherwise we add a type restriction on the var. If a symbol
886 ;;; macro, we just wrap a THE around the expansion.
887 (defun process-type-decl (decl res vars)
888 (declare (list decl vars) (type lexenv res))
889 (let ((type (compiler-specifier-type (first decl))))
890 (collect ((restr nil cons)
892 (dolist (var-name (rest decl))
893 (let* ((bound-var (find-in-bindings vars var-name))
895 (lexenv-find var-name vars)
896 (find-free-var var-name))))
899 (flet ((process-var (var bound-var)
900 (let* ((old-type (or (lexenv-find var type-restrictions)
902 (int (if (or (fun-type-p type)
903 (fun-type-p old-type))
905 (type-approx-intersection2 old-type type))))
906 (cond ((eq int *empty-type*)
907 (unless (policy *lexenv* (= inhibit-warnings 3))
909 "The type declarations ~S and ~S for ~S conflict."
910 (type-specifier old-type) (type-specifier type)
912 (bound-var (setf (leaf-type bound-var) int))
914 (restr (cons var int)))))))
915 (process-var var bound-var)
916 (awhen (and (lambda-var-p var)
917 (lambda-var-specvar var))
918 (process-var it nil))))
920 ;; FIXME: non-ANSI weirdness
921 (aver (eq (car var) 'MACRO))
922 (new-vars `(,var-name . (MACRO . (the ,(first decl)
926 "~S is an alien variable, so its type can't be declared."
929 (if (or (restr) (new-vars))
930 (make-lexenv :default res
931 :type-restrictions (restr)
935 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
936 ;;; declarations for function variables. In addition to allowing
937 ;;; declarations for functions being bound, we must also deal with
938 ;;; declarations that constrain the type of lexically apparent
940 (defun process-ftype-decl (spec res names fvars)
941 (declare (type list names fvars)
943 (let ((type (compiler-specifier-type spec)))
944 (collect ((res nil cons))
946 (let ((found (find name fvars
947 :key #'leaf-source-name
951 (setf (leaf-type found) type)
952 (assert-definition-type found type
953 :unwinnage-fun #'compiler-notify
954 :where "FTYPE declaration"))
956 (res (cons (find-lexically-apparent-fun
957 name "in a function type declaration")
960 (make-lexenv :default res :type-restrictions (res))
963 ;;; Process a special declaration, returning a new LEXENV. A non-bound
964 ;;; special declaration is instantiated by throwing a special variable
965 ;;; into the variables.
966 (defun process-special-decl (spec res vars)
967 (declare (list spec vars) (type lexenv res))
968 (collect ((new-venv nil cons))
969 (dolist (name (cdr spec))
970 (let ((var (find-in-bindings vars name)))
973 (aver (eq (car var) 'MACRO))
975 "~S is a symbol-macro and thus can't be declared special."
978 (when (lambda-var-ignorep var)
979 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
980 ;; requires that this be a STYLE-WARNING, not a full WARNING.
982 "The ignored variable ~S is being declared special."
984 (setf (lambda-var-specvar var)
985 (specvar-for-binding name)))
987 (unless (assoc name (new-venv) :test #'eq)
988 (new-venv (cons name (specvar-for-binding name))))))))
990 (make-lexenv :default res :vars (new-venv))
993 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
994 ;;; (and TYPE if notinline).
995 (defun make-new-inlinep (var inlinep)
996 (declare (type global-var var) (type inlinep inlinep))
997 (let ((res (make-defined-fun
998 :%source-name (leaf-source-name var)
999 :where-from (leaf-where-from var)
1000 :type (if (and (eq inlinep :notinline)
1001 (not (eq (leaf-where-from var) :declared)))
1002 (specifier-type 'function)
1005 (when (defined-fun-p var)
1006 (setf (defined-fun-inline-expansion res)
1007 (defined-fun-inline-expansion var))
1008 (setf (defined-fun-functional res)
1009 (defined-fun-functional var)))
1012 ;;; Parse an inline/notinline declaration. If it's a local function we're
1013 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1014 (defun process-inline-decl (spec res fvars)
1015 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1017 (dolist (name (rest spec))
1018 (let ((fvar (find name fvars
1019 :key #'leaf-source-name
1022 (setf (functional-inlinep fvar) sense)
1024 (find-lexically-apparent-fun
1025 name "in an inline or notinline declaration")))
1028 (when (policy *lexenv* (>= speed inhibit-warnings))
1029 (compiler-notify "ignoring ~A declaration not at ~
1030 definition of local function:~% ~S"
1033 (push (cons name (make-new-inlinep found sense))
1037 (make-lexenv :default res :funs new-fenv)
1040 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1041 (defun find-in-bindings-or-fbindings (name vars fvars)
1042 (declare (list vars fvars))
1044 (destructuring-bind (wot fn-name) name
1045 (unless (eq wot 'function)
1046 (compiler-error "The function or variable name ~S is unrecognizable."
1048 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1049 (find-in-bindings vars name)))
1051 ;;; Process an ignore/ignorable declaration, checking for various losing
1053 (defun process-ignore-decl (spec vars fvars)
1054 (declare (list spec vars fvars))
1055 (dolist (name (rest spec))
1056 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1059 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1060 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1061 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1063 ;; FIXME: This special case looks like non-ANSI weirdness.
1064 ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
1065 ;; Just ignore the IGNORE decl.
1068 (setf (leaf-ever-used var) t))
1069 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1070 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1071 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1072 (compiler-style-warn "declaring special variable ~S to be ignored"
1074 ((eq (first spec) 'ignorable)
1075 (setf (leaf-ever-used var) t))
1077 (setf (lambda-var-ignorep var) t)))))
1080 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1081 ;;; go away, I think.
1082 (defvar *suppress-values-declaration* nil
1084 "If true, processing of the VALUES declaration is inhibited.")
1086 ;;; Process a single declaration spec, augmenting the specified LEXENV
1087 ;;; RES and returning it as a result. VARS and FVARS are as described in
1089 (defun process-1-decl (raw-spec res vars fvars cont)
1090 (declare (type list raw-spec vars fvars))
1091 (declare (type lexenv res))
1092 (declare (type continuation cont))
1093 (let ((spec (canonized-decl-spec raw-spec)))
1095 (special (process-special-decl spec res vars))
1098 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1099 (process-ftype-decl (second spec) res (cddr spec) fvars))
1100 ((inline notinline maybe-inline)
1101 (process-inline-decl spec res fvars))
1103 (process-ignore-decl spec vars fvars)
1108 :policy (process-optimize-decl spec (lexenv-policy res))))
1110 (process-type-decl (cdr spec) res vars))
1111 (values ;; FIXME -- APD, 2002-01-26
1112 (if t ; *suppress-values-declaration*
1114 (let ((types (cdr spec)))
1115 (ir1ize-the-or-values (if (eql (length types) 1)
1120 "in VALUES declaration"))))
1122 (when (policy *lexenv* (> speed inhibit-warnings))
1124 "compiler limitation: ~
1125 ~% There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1128 (unless (info :declaration :recognized (first spec))
1129 (compiler-warn "unrecognized declaration ~S" raw-spec))
1132 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1133 ;;; and FUNCTIONAL structures which are being bound. In addition to
1134 ;;; filling in slots in the leaf structures, we return a new LEXENV
1135 ;;; which reflects pervasive special and function type declarations,
1136 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1137 ;;; continuation affected by VALUES declarations.
1139 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1141 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1142 (declare (list decls vars fvars) (type continuation cont))
1143 (dolist (decl decls)
1144 (dolist (spec (rest decl))
1145 (unless (consp spec)
1146 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1147 (setq env (process-1-decl spec env vars fvars cont))))
1150 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1151 ;;; declaration. If there is a global variable of that name, then
1152 ;;; check that it isn't a constant and return it. Otherwise, create an
1153 ;;; anonymous GLOBAL-VAR.
1154 (defun specvar-for-binding (name)
1155 (cond ((not (eq (info :variable :where-from name) :assumed))
1156 (let ((found (find-free-var name)))
1157 (when (heap-alien-info-p found)
1159 "~S is an alien variable and so can't be declared special."
1161 (unless (global-var-p found)
1163 "~S is a constant and so can't be declared special."
1167 (make-global-var :kind :special
1169 :where-from :declared))))