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 (let* ((type (lexenv-find leaf type-restrictions))
561 (leaf (or (and (defined-fun-p leaf)
562 (not (eq (defined-fun-inlinep leaf)
564 (let ((functional (defined-fun-functional leaf)))
565 (when (and functional
566 (not (functional-kind functional)))
567 (maybe-reanalyze-functional functional))))
568 (when (and (lambda-p leaf)
569 (memq (functional-kind leaf)
571 (maybe-reanalyze-functional leaf))
573 (ref (make-ref leaf)))
574 (push ref (leaf-refs leaf))
575 (setf (leaf-ever-used leaf) t)
576 (link-node-to-previous-continuation ref start)
577 (cond (type (let* ((ref-cont (make-continuation))
578 (cast (make-cast ref-cont
579 (make-single-value-type type)
580 (lexenv-policy *lexenv*))))
581 (setf (continuation-dest ref-cont) cast)
582 (use-continuation ref ref-cont)
583 (link-node-to-previous-continuation cast ref-cont)
584 (use-continuation cast cont)))
585 (t (use-continuation ref cont)))))
587 ;;; Convert a reference to a symbolic constant or variable. If the
588 ;;; symbol is entered in the LEXENV-VARS we use that definition,
589 ;;; otherwise we find the current global definition. This is also
590 ;;; where we pick off symbol macro and alien variable references.
591 (defun ir1-convert-var (start cont name)
592 (declare (type continuation start cont) (symbol name))
593 (let ((var (or (lexenv-find name vars) (find-free-var name))))
596 (when (lambda-var-p var)
597 (let ((home (continuation-home-lambda-or-null start)))
599 (pushnew var (lambda-calls-or-closes home))))
600 (when (lambda-var-ignorep var)
601 ;; (ANSI's specification for the IGNORE declaration requires
602 ;; that this be a STYLE-WARNING, not a full WARNING.)
603 (compiler-style-warn "reading an ignored variable: ~S" name)))
604 (reference-leaf start cont var))
606 (aver (eq (car var) 'MACRO))
607 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
608 (ir1-convert start cont (cdr var)))
610 (ir1-convert start cont `(%heap-alien ',var)))))
613 ;;; Convert anything that looks like a special form, global function
614 ;;; or compiler-macro call.
615 (defun ir1-convert-global-functoid (start cont form)
616 (declare (type continuation start cont) (list form))
617 (let* ((fun-name (first form))
618 (translator (info :function :ir1-convert fun-name))
619 (cmacro-fun (sb!xc:compiler-macro-function fun-name *lexenv*)))
622 (compiler-warn "ignoring compiler macro for special form"))
623 (funcall translator start cont form))
625 ;; gotcha: If you look up the DEFINE-COMPILER-MACRO
626 ;; macro in the ANSI spec, you might think that
627 ;; suppressing compiler-macro expansion when NOTINLINE
628 ;; is some pre-ANSI hack. However, if you look up the
629 ;; NOTINLINE declaration, you'll find that ANSI
630 ;; requires this behavior after all.
631 (not (eq (info :function :inlinep fun-name) :notinline)))
632 (let ((res (careful-expand-macro cmacro-fun form)))
634 (ir1-convert-global-functoid-no-cmacro
635 start cont form fun-name)
636 (ir1-convert start cont res))))
638 (ir1-convert-global-functoid-no-cmacro start cont form fun-name)))))
640 ;;; Handle the case of where the call was not a compiler macro, or was
641 ;;; a compiler macro and passed.
642 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
643 (declare (type continuation start cont) (list form))
644 ;; FIXME: Couldn't all the INFO calls here be converted into
645 ;; standard CL functions, like MACRO-FUNCTION or something?
646 ;; And what happens with lexically-defined (MACROLET) macros
648 (ecase (info :function :kind fun)
652 (careful-expand-macro (info :function :macro-function fun)
655 (ir1-convert-srctran start
657 (find-free-fun fun "shouldn't happen! (no-cmacro)")
660 (defun muffle-warning-or-die ()
662 (bug "no MUFFLE-WARNING restart"))
664 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
665 ;;; errors which occur during the macroexpansion.
666 (defun careful-expand-macro (fun form)
667 (let (;; a hint I (WHN) wish I'd known earlier
668 (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
669 (flet (;; Return a string to use as a prefix in error reporting,
670 ;; telling something about which form caused the problem.
672 (let ((*print-pretty* nil)
673 ;; We rely on the printer to abbreviate FORM.
678 #-sb-xc-host "(in macroexpansion of ~S)"
679 ;; longer message to avoid ambiguity "Was it the xc host
680 ;; or the cross-compiler which encountered the problem?"
681 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
683 (handler-bind ((style-warning (lambda (c)
685 "~@<~A~:@_~A~@:_~A~:>"
686 (wherestring) hint c)
687 (muffle-warning-or-die)))
688 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
689 ;; Debian Linux, anyway) raises a CL:WARNING
690 ;; condition (not a CL:STYLE-WARNING) for undefined
691 ;; symbols when converting interpreted functions,
692 ;; causing COMPILE-FILE to think the file has a real
693 ;; problem, causing COMPILE-FILE to return FAILURE-P
694 ;; set (not just WARNINGS-P set). Since undefined
695 ;; symbol warnings are often harmless forward
696 ;; references, and since it'd be inordinately painful
697 ;; to try to eliminate all such forward references,
698 ;; these warnings are basically unavoidable. Thus, we
699 ;; need to coerce the system to work through them,
700 ;; and this code does so, by crudely suppressing all
701 ;; warnings in cross-compilation macroexpansion. --
703 #+(and cmu sb-xc-host)
708 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
709 Ordinarily that would cause compilation to ~
710 fail. However, since we're running under ~
711 CMU CL, and since CMU CL emits non-STYLE ~
712 warnings for safe, hard-to-fix things (e.g. ~
713 references to not-yet-defined functions) ~
714 we're going to have to ignore it and ~
715 proceed anyway. Hopefully we're not ~
716 ignoring anything horrible here..)~:@>~:>"
719 (muffle-warning-or-die)))
720 #-(and cmu sb-xc-host)
722 (compiler-warn "~@<~A~:@_~A~@:_~A~:>"
723 (wherestring) hint c)
724 (muffle-warning-or-die)))
726 (compiler-error "~@<~A~:@_~A~@:_~A~:>"
727 (wherestring) hint c))))
728 (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
730 ;;;; conversion utilities
732 ;;; Convert a bunch of forms, discarding all the values except the
733 ;;; last. If there aren't any forms, then translate a NIL.
734 (declaim (ftype (sfunction (continuation continuation list) (values))
735 ir1-convert-progn-body))
736 (defun ir1-convert-progn-body (start cont body)
738 (reference-constant start cont nil)
739 (let ((this-start start)
742 (let ((form (car forms)))
743 (when (endp (cdr forms))
744 (ir1-convert this-start cont form)
746 (let ((this-cont (make-continuation)))
747 (ir1-convert this-start this-cont form)
748 (setq this-start this-cont
749 forms (cdr forms)))))))
752 ;;;; converting combinations
754 ;;; Convert a function call where the function FUN is a LEAF. FORM is
755 ;;; the source for the call. We return the COMBINATION node so that
756 ;;; the caller can poke at it if it wants to.
757 (declaim (ftype (sfunction (continuation continuation list leaf) combination)
758 ir1-convert-combination))
759 (defun ir1-convert-combination (start cont form fun)
760 (let ((fun-cont (make-continuation)))
761 (ir1-convert start fun-cont `(the (or function symbol) ,fun))
762 (ir1-convert-combination-args fun-cont cont (cdr form))))
764 ;;; Convert the arguments to a call and make the COMBINATION
765 ;;; node. FUN-CONT is the continuation which yields the function to
766 ;;; call. ARGS is the list of arguments for the call, which defaults
767 ;;; to the cdr of source. We return the COMBINATION node.
768 (defun ir1-convert-combination-args (fun-cont cont args)
769 (declare (type continuation fun-cont cont) (list args))
770 (let ((node (make-combination fun-cont)))
771 (setf (continuation-dest fun-cont) node)
772 (collect ((arg-conts))
773 (let ((this-start fun-cont))
775 (let ((this-cont (make-continuation node)))
776 (ir1-convert this-start this-cont arg)
777 (setq this-start this-cont)
778 (arg-conts this-cont)))
779 (link-node-to-previous-continuation node this-start)
780 (use-continuation node cont)
781 (setf (combination-args node) (arg-conts))))
784 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
785 ;;; source transforms and try out any inline expansion. If there is no
786 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
787 ;;; known function which will quite possibly be open-coded.) Next, we
788 ;;; go to ok-combination conversion.
789 (defun ir1-convert-srctran (start cont var form)
790 (declare (type continuation start cont) (type global-var var))
791 (let ((inlinep (when (defined-fun-p var)
792 (defined-fun-inlinep var))))
793 (if (eq inlinep :notinline)
794 (ir1-convert-combination start cont form var)
795 (let ((transform (info :function
797 (leaf-source-name var))))
799 (multiple-value-bind (result pass) (funcall transform form)
801 (ir1-convert-maybe-predicate start cont form var)
802 (ir1-convert start cont result)))
803 (ir1-convert-maybe-predicate start cont form var))))))
805 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
806 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
807 ;;; predicate always appears in a conditional context.
809 ;;; If the function isn't a predicate, then we call
810 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
811 (defun ir1-convert-maybe-predicate (start cont form var)
812 (declare (type continuation start cont) (list form) (type global-var var))
813 (let ((info (info :function :info (leaf-source-name var))))
815 (ir1-attributep (fun-info-attributes info) predicate)
816 (not (if-p (continuation-dest cont))))
817 (ir1-convert start cont `(if ,form t nil))
818 (ir1-convert-combination-checking-type start cont form var))))
820 ;;; Actually really convert a global function call that we are allowed
823 ;;; If we know the function type of the function, then we check the
824 ;;; call for syntactic legality with respect to the declared function
825 ;;; type. If it is impossible to determine whether the call is correct
826 ;;; due to non-constant keywords, then we give up, marking the call as
827 ;;; :FULL to inhibit further error messages. We return true when the
830 ;;; If the call is legal, we also propagate type assertions from the
831 ;;; function type to the arg and result continuations. We do this now
832 ;;; so that IR1 optimize doesn't have to redundantly do the check
833 ;;; later so that it can do the type propagation.
834 (defun ir1-convert-combination-checking-type (start cont form var)
835 (declare (type continuation start cont) (list form) (type leaf var))
836 (let* ((node (ir1-convert-combination start cont form var))
837 (fun-cont (basic-combination-fun node))
838 (type (leaf-type var)))
839 (when (validate-call-type node type t)
840 (setf (continuation-%derived-type fun-cont)
841 (make-single-value-type type))
842 (setf (continuation-reoptimize fun-cont) nil)))
845 ;;; Convert a call to a local function, or if the function has already
846 ;;; been LET converted, then throw FUNCTIONAL to
847 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
848 ;;; are converting inline expansions for local functions during
850 (defun ir1-convert-local-combination (start cont form functional)
852 ;; The test here is for "when LET converted", as a translation of
853 ;; the old CMU CL comments into code. Unfortunately, the old CMU CL
854 ;; comments aren't specific enough to tell whether the correct
855 ;; translation is FUNCTIONAL-SOMEWHAT-LETLIKE-P or
856 ;; FUNCTIONAL-LETLIKE-P or what. The old CMU CL code assumed that
857 ;; any non-null FUNCTIONAL-KIND meant that the function "had been
858 ;; LET converted", which might even be right, but seems fragile, so
859 ;; we try to be pickier.
861 ;; looks LET-converted
862 (functional-somewhat-letlike-p functional)
863 ;; It's possible for a LET-converted function to end up
864 ;; deleted later. In that case, for the purposes of this
865 ;; analysis, it is LET-converted: LET-converted functionals
866 ;; are too badly trashed to expand them inline, and deleted
867 ;; LET-converted functionals are even worse.
868 (eql (functional-kind functional) :deleted))
869 (throw 'locall-already-let-converted functional))
870 ;; Any other non-NIL KIND value is a case we haven't found a
871 ;; justification for, and at least some such values (e.g. :EXTERNAL
872 ;; and :TOPLEVEL) seem obviously wrong.
873 (aver (null (functional-kind functional)))
875 (ir1-convert-combination start
878 (maybe-reanalyze-functional functional)))
882 ;;; Given a list of LAMBDA-VARs and a variable name, return the
883 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
884 ;;; *last* variable with that name, since LET* bindings may be
885 ;;; duplicated, and declarations always apply to the last.
886 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
888 (defun find-in-bindings (vars name)
892 (when (eq (leaf-source-name var) name)
894 (let ((info (lambda-var-arg-info var)))
896 (let ((supplied-p (arg-info-supplied-p info)))
897 (when (and supplied-p
898 (eq (leaf-source-name supplied-p) name))
899 (setq found supplied-p))))))
900 ((and (consp var) (eq (car var) name))
901 (setf found (cdr var)))))
904 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
905 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
906 ;;; type, otherwise we add a type restriction on the var. If a symbol
907 ;;; macro, we just wrap a THE around the expansion.
908 (defun process-type-decl (decl res vars)
909 (declare (list decl vars) (type lexenv res))
910 (let ((type (compiler-specifier-type (first decl))))
911 (collect ((restr nil cons)
913 (dolist (var-name (rest decl))
914 (let* ((bound-var (find-in-bindings vars var-name))
916 (lexenv-find var-name vars)
917 (find-free-var var-name))))
920 (flet ((process-var (var bound-var)
921 (let* ((old-type (or (lexenv-find var type-restrictions)
923 (int (if (or (fun-type-p type)
924 (fun-type-p old-type))
926 (type-approx-intersection2 old-type type))))
927 (cond ((eq int *empty-type*)
928 (unless (policy *lexenv* (= inhibit-warnings 3))
930 "The type declarations ~S and ~S for ~S conflict."
931 (type-specifier old-type) (type-specifier type)
933 (bound-var (setf (leaf-type bound-var) int))
935 (restr (cons var int)))))))
936 (process-var var bound-var)
937 (awhen (and (lambda-var-p var)
938 (lambda-var-specvar var))
939 (process-var it nil))))
941 ;; FIXME: non-ANSI weirdness
942 (aver (eq (car var) 'MACRO))
943 (new-vars `(,var-name . (MACRO . (the ,(first decl)
947 "~S is an alien variable, so its type can't be declared."
950 (if (or (restr) (new-vars))
951 (make-lexenv :default res
952 :type-restrictions (restr)
956 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
957 ;;; declarations for function variables. In addition to allowing
958 ;;; declarations for functions being bound, we must also deal with
959 ;;; declarations that constrain the type of lexically apparent
961 (defun process-ftype-decl (spec res names fvars)
962 (declare (type list names fvars)
964 (let ((type (compiler-specifier-type spec)))
965 (collect ((res nil cons))
967 (let ((found (find name fvars
968 :key #'leaf-source-name
972 (setf (leaf-type found) type)
973 (assert-definition-type found type
974 :unwinnage-fun #'compiler-notify
975 :where "FTYPE declaration"))
977 (res (cons (find-lexically-apparent-fun
978 name "in a function type declaration")
981 (make-lexenv :default res :type-restrictions (res))
984 ;;; Process a special declaration, returning a new LEXENV. A non-bound
985 ;;; special declaration is instantiated by throwing a special variable
986 ;;; into the variables.
987 (defun process-special-decl (spec res vars)
988 (declare (list spec vars) (type lexenv res))
989 (collect ((new-venv nil cons))
990 (dolist (name (cdr spec))
991 (let ((var (find-in-bindings vars name)))
994 (aver (eq (car var) 'MACRO))
996 "~S is a symbol-macro and thus can't be declared special."
999 (when (lambda-var-ignorep var)
1000 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1001 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1002 (compiler-style-warn
1003 "The ignored variable ~S is being declared special."
1005 (setf (lambda-var-specvar var)
1006 (specvar-for-binding name)))
1008 (unless (assoc name (new-venv) :test #'eq)
1009 (new-venv (cons name (specvar-for-binding name))))))))
1011 (make-lexenv :default res :vars (new-venv))
1014 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1015 ;;; (and TYPE if notinline).
1016 (defun make-new-inlinep (var inlinep)
1017 (declare (type global-var var) (type inlinep inlinep))
1018 (let ((res (make-defined-fun
1019 :%source-name (leaf-source-name var)
1020 :where-from (leaf-where-from var)
1021 :type (if (and (eq inlinep :notinline)
1022 (not (eq (leaf-where-from var) :declared)))
1023 (specifier-type 'function)
1026 (when (defined-fun-p var)
1027 (setf (defined-fun-inline-expansion res)
1028 (defined-fun-inline-expansion var))
1029 (setf (defined-fun-functional res)
1030 (defined-fun-functional var)))
1033 ;;; Parse an inline/notinline declaration. If it's a local function we're
1034 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1035 (defun process-inline-decl (spec res fvars)
1036 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1038 (dolist (name (rest spec))
1039 (let ((fvar (find name fvars
1040 :key #'leaf-source-name
1043 (setf (functional-inlinep fvar) sense)
1045 (find-lexically-apparent-fun
1046 name "in an inline or notinline declaration")))
1049 (when (policy *lexenv* (>= speed inhibit-warnings))
1050 (compiler-notify "ignoring ~A declaration not at ~
1051 definition of local function:~% ~S"
1054 (push (cons name (make-new-inlinep found sense))
1058 (make-lexenv :default res :funs new-fenv)
1061 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1062 (defun find-in-bindings-or-fbindings (name vars fvars)
1063 (declare (list vars fvars))
1065 (destructuring-bind (wot fn-name) name
1066 (unless (eq wot 'function)
1067 (compiler-error "The function or variable name ~S is unrecognizable."
1069 (find fn-name fvars :key #'leaf-source-name :test #'equal))
1070 (find-in-bindings vars name)))
1072 ;;; Process an ignore/ignorable declaration, checking for various losing
1074 (defun process-ignore-decl (spec vars fvars)
1075 (declare (list spec vars fvars))
1076 (dolist (name (rest spec))
1077 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1080 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1081 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1082 (compiler-style-warn "declaring unknown variable ~S to be ignored"
1084 ;; FIXME: This special case looks like non-ANSI weirdness.
1085 ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
1086 ;; Just ignore the IGNORE decl.
1089 (setf (leaf-ever-used var) t))
1090 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1091 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1092 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1093 (compiler-style-warn "declaring special variable ~S to be ignored"
1095 ((eq (first spec) 'ignorable)
1096 (setf (leaf-ever-used var) t))
1098 (setf (lambda-var-ignorep var) t)))))
1101 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1102 ;;; go away, I think.
1103 (defvar *suppress-values-declaration* nil
1105 "If true, processing of the VALUES declaration is inhibited.")
1107 ;;; Process a single declaration spec, augmenting the specified LEXENV
1108 ;;; RES and returning it as a result. VARS and FVARS are as described in
1110 (defun process-1-decl (raw-spec res vars fvars cont)
1111 (declare (type list raw-spec vars fvars))
1112 (declare (type lexenv res))
1113 (declare (type continuation cont))
1114 (let ((spec (canonized-decl-spec raw-spec)))
1116 (special (process-special-decl spec res vars))
1119 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1120 (process-ftype-decl (second spec) res (cddr spec) fvars))
1121 ((inline notinline maybe-inline)
1122 (process-inline-decl spec res fvars))
1124 (process-ignore-decl spec vars fvars)
1129 :policy (process-optimize-decl spec (lexenv-policy res))))
1131 (process-type-decl (cdr spec) res vars))
1132 (values ;; FIXME -- APD, 2002-01-26
1133 (if t ; *suppress-values-declaration*
1135 (let ((types (cdr spec)))
1136 (ir1ize-the-or-values (if (eql (length types) 1)
1141 "in VALUES declaration"))))
1143 (when (policy *lexenv* (> speed inhibit-warnings))
1145 "compiler limitation: ~
1146 ~% There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1149 (unless (info :declaration :recognized (first spec))
1150 (compiler-warn "unrecognized declaration ~S" raw-spec))
1153 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1154 ;;; and FUNCTIONAL structures which are being bound. In addition to
1155 ;;; filling in slots in the leaf structures, we return a new LEXENV
1156 ;;; which reflects pervasive special and function type declarations,
1157 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1158 ;;; continuation affected by VALUES declarations.
1160 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1162 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1163 (declare (list decls vars fvars) (type continuation cont))
1164 (dolist (decl decls)
1165 (dolist (spec (rest decl))
1166 (unless (consp spec)
1167 (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1168 (setq env (process-1-decl spec env vars fvars cont))))
1171 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1172 ;;; declaration. If there is a global variable of that name, then
1173 ;;; check that it isn't a constant and return it. Otherwise, create an
1174 ;;; anonymous GLOBAL-VAR.
1175 (defun specvar-for-binding (name)
1176 (cond ((not (eq (info :variable :where-from name) :assumed))
1177 (let ((found (find-free-var name)))
1178 (when (heap-alien-info-p found)
1180 "~S is an alien variable and so can't be declared special."
1182 (unless (global-var-p found)
1184 "~S is a constant and so can't be declared special."
1188 (make-global-var :kind :special
1190 :where-from :declared))))