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 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 ;;;; namespace management utilities
54 ;;; Return a GLOBAL-VAR structure usable for referencing the global
56 (defun find-free-really-function (name)
57 (unless (info :function :kind name)
58 (setf (info :function :kind name) :function)
59 (setf (info :function :where-from name) :assumed))
61 (let ((where (info :function :where-from name)))
62 (when (and (eq where :assumed)
63 ;; In the ordinary target Lisp, it's silly to report
64 ;; undefinedness when the function is defined in the
65 ;; running Lisp. But at cross-compile time, the current
66 ;; definedness of a function is irrelevant to the
67 ;; definedness at runtime, which is what matters.
68 #-sb-xc-host (not (fboundp name)))
69 (note-undefined-reference name :function))
70 (make-global-var :kind :global-function
72 :type (if (or *derive-function-types*
74 (info :function :type name)
75 (specifier-type 'function))
78 ;;; Return a SLOT-ACCESSOR structure usable for referencing the slot
79 ;;; accessor NAME. CLASS is the structure class.
80 (defun find-structure-slot-accessor (class name)
81 (declare (type sb!xc:class class))
82 (let* ((info (layout-info
83 (or (info :type :compiler-layout (sb!xc:class-name class))
84 (class-layout class))))
85 (accessor-name (if (listp name) (cadr name) name))
86 (slot (find accessor-name (dd-slots info)
87 :key #'sb!kernel:dsd-accessor-name))
89 (slot-type (dsd-type slot)))
91 (error "can't find slot ~S" type))
96 `(function (,slot-type ,type) ,slot-type)
97 `(function (,type) ,slot-type)))
101 ;;; If NAME is already entered in *FREE-FUNCTIONS*, then return the
102 ;;; value. Otherwise, make a new GLOBAL-VAR using information from the
103 ;;; global environment and enter it in *FREE-FUNCTIONS*. If NAME names
104 ;;; a macro or special form, then we error out using the supplied
105 ;;; context which indicates what we were trying to do that demanded a
107 (defun find-free-function (name context)
108 (declare (string context))
109 (declare (values global-var))
110 (or (gethash name *free-functions*)
111 (ecase (info :function :kind name)
112 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
114 (compiler-error "The macro name ~S was found ~A." name context))
116 (compiler-error "The special form name ~S was found ~A."
120 (check-function-name name)
121 (note-if-setf-function-and-macro name)
122 (let ((expansion (info :function :inline-expansion name))
123 (inlinep (info :function :inlinep name)))
124 (setf (gethash name *free-functions*)
125 (if (or expansion inlinep)
126 (make-defined-function
128 :inline-expansion expansion
130 :where-from (info :function :where-from name)
131 :type (info :function :type name))
132 (let ((info (info :function :accessor-for name)))
135 (find-free-really-function name))
136 (sb!xc:structure-class
137 (find-structure-slot-accessor info name))
139 (if (typep (layout-info (info :type :compiler-layout
142 'defstruct-description)
143 (find-structure-slot-accessor info name)
144 (find-free-really-function name))))))))))))
146 ;;; Return the LEAF structure for the lexically apparent function
147 ;;; definition of NAME.
148 (declaim (ftype (function (t string) leaf) find-lexically-apparent-function))
149 (defun find-lexically-apparent-function (name context)
150 (let ((var (lexenv-find name functions :test #'equal)))
153 (aver (and (consp var) (eq (car var) 'macro)))
154 (compiler-error "found macro name ~S ~A" name context))
157 (find-free-function name context)))))
159 ;;; Return the LEAF node for a global variable reference to NAME. If
160 ;;; NAME is already entered in *FREE-VARIABLES*, then we just return
161 ;;; the corresponding value. Otherwise, we make a new leaf using
162 ;;; information from the global environment and enter it in
163 ;;; *FREE-VARIABLES*. If the variable is unknown, then we emit a
165 (defun find-free-variable (name)
166 (declare (values (or leaf heap-alien-info)))
167 (unless (symbolp name)
168 (compiler-error "Variable name is not a symbol: ~S." name))
169 (or (gethash name *free-variables*)
170 (let ((kind (info :variable :kind name))
171 (type (info :variable :type name))
172 (where-from (info :variable :where-from name)))
173 (when (and (eq where-from :assumed) (eq kind :global))
174 (note-undefined-reference name :variable))
176 (setf (gethash name *free-variables*)
178 (info :variable :alien-info name)
179 (multiple-value-bind (val valp)
180 (info :variable :constant-value name)
181 (if (and (eq kind :constant) valp)
182 (make-constant :value val
185 :where-from where-from)
186 (make-global-var :kind kind
189 :where-from where-from))))))))
191 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
192 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
193 ;;; CONSTANT might be circular. We also check that the constant (and
194 ;;; any subparts) are dumpable at all.
195 (eval-when (:compile-toplevel :load-toplevel :execute)
196 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
197 ;; below. -- AL 20010227
198 (defconstant list-to-hash-table-threshold 32))
199 (defun maybe-emit-make-load-forms (constant)
200 (let ((things-processed nil)
202 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
203 (declare (type (or list hash-table) things-processed)
204 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
206 (labels ((grovel (value)
207 ;; Unless VALUE is an object which which obviously
208 ;; can't contain other objects
210 '(or #-sb-xc-host unboxed-array
215 (etypecase things-processed
217 (when (member value things-processed :test #'eq)
218 (return-from grovel nil))
219 (push value things-processed)
221 (when (> count list-to-hash-table-threshold)
222 (let ((things things-processed))
223 (setf things-processed
224 (make-hash-table :test 'eq))
225 (dolist (thing things)
226 (setf (gethash thing things-processed) t)))))
228 (when (gethash value things-processed)
229 (return-from grovel nil))
230 (setf (gethash value things-processed) t)))
234 (grovel (cdr value)))
236 (dotimes (i (length value))
237 (grovel (svref value i))))
239 (dotimes (i (length value))
240 (grovel (aref value i))))
242 ;; Even though the (ARRAY T) branch does the exact
243 ;; same thing as this branch we do this separately
244 ;; so that the compiler can use faster versions of
245 ;; array-total-size and row-major-aref.
246 (dotimes (i (array-total-size value))
247 (grovel (row-major-aref value i))))
249 (dotimes (i (array-total-size value))
250 (grovel (row-major-aref value i))))
251 (;; In the target SBCL, we can dump any instance,
252 ;; but in the cross-compilation host,
253 ;; %INSTANCE-FOO functions don't work on general
254 ;; instances, only on STRUCTURE!OBJECTs.
255 #+sb-xc-host structure!object
256 #-sb-xc-host instance
257 (when (emit-make-load-form value)
258 (dotimes (i (%instance-length value))
259 (grovel (%instance-ref value i)))))
262 "Objects of type ~S can't be dumped into fasl files."
263 (type-of value)))))))
267 ;;;; some flow-graph hacking utilities
269 ;;; This function sets up the back link between the node and the
270 ;;; continuation which continues at it.
271 #!-sb-fluid (declaim (inline prev-link))
272 (defun prev-link (node cont)
273 (declare (type node node) (type continuation cont))
274 (aver (not (continuation-next cont)))
275 (setf (continuation-next cont) node)
276 (setf (node-prev node) cont))
278 ;;; This function is used to set the continuation for a node, and thus
279 ;;; determine what receives the value and what is evaluated next. If
280 ;;; the continuation has no block, then we make it be in the block
281 ;;; that the node is in. If the continuation heads its block, we end
282 ;;; our block and link it to that block. If the continuation is not
283 ;;; currently used, then we set the derived-type for the continuation
284 ;;; to that of the node, so that a little type propagation gets done.
286 ;;; We also deal with a bit of THE's semantics here: we weaken the
287 ;;; assertion on CONT to be no stronger than the assertion on CONT in
288 ;;; our scope. See the IR1-CONVERT method for THE.
289 #!-sb-fluid (declaim (inline use-continuation))
290 (defun use-continuation (node cont)
291 (declare (type node node) (type continuation cont))
292 (let ((node-block (continuation-block (node-prev node))))
293 (case (continuation-kind cont)
295 (setf (continuation-block cont) node-block)
296 (setf (continuation-kind cont) :inside-block)
297 (setf (continuation-use cont) node)
298 (setf (node-cont node) cont))
300 (%use-continuation node cont)))))
301 (defun %use-continuation (node cont)
302 (declare (type node node) (type continuation cont) (inline member))
303 (let ((block (continuation-block cont))
304 (node-block (continuation-block (node-prev node))))
305 (aver (eq (continuation-kind cont) :block-start))
306 (when (block-last node-block)
307 (error "~S has already ended." node-block))
308 (setf (block-last node-block) node)
309 (when (block-succ node-block)
310 (error "~S already has successors." node-block))
311 (setf (block-succ node-block) (list block))
312 (when (memq node-block (block-pred block))
313 (error "~S is already a predecessor of ~S." node-block block))
314 (push node-block (block-pred block))
315 (add-continuation-use node cont)
316 (unless (eq (continuation-asserted-type cont) *wild-type*)
317 (let ((new (values-type-union (continuation-asserted-type cont)
318 (or (lexenv-find cont type-restrictions)
320 (when (type/= new (continuation-asserted-type cont))
321 (setf (continuation-asserted-type cont) new)
322 (reoptimize-continuation cont))))))
324 ;;;; exported functions
326 ;;; This function takes a form and the top-level form number for that
327 ;;; form, and returns a lambda representing the translation of that
328 ;;; form in the current global environment. The returned lambda is a
329 ;;; top-level lambda that can be called to cause evaluation of the
330 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
331 ;;; then the value of the form is returned from the function,
332 ;;; otherwise NIL is returned.
334 ;;; This function may have arbitrary effects on the global environment
335 ;;; due to processing of PROCLAIMs and EVAL-WHENs. All syntax error
336 ;;; checking is done, with erroneous forms being replaced by a proxy
337 ;;; which signals an error if it is evaluated. Warnings about possibly
338 ;;; inconsistent or illegal changes to the global environment will
341 ;;; We make the initial component and convert the form in a PROGN (and
342 ;;; an optional NIL tacked on the end.) We then return the lambda. We
343 ;;; bind all of our state variables here, rather than relying on the
344 ;;; global value (if any) so that IR1 conversion will be reentrant.
345 ;;; This is necessary for EVAL-WHEN processing, etc.
347 ;;; The hashtables used to hold global namespace info must be
348 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
349 ;;; that local macro definitions can be introduced by enclosing code.
350 (defun ir1-top-level (form path for-value)
351 (declare (list path))
352 (let* ((*current-path* path)
353 (component (make-empty-component))
354 (*current-component* component))
355 (setf (component-name component) "initial component")
356 (setf (component-kind component) :initial)
357 (let* ((forms (if for-value `(,form) `(,form nil)))
358 (res (ir1-convert-lambda-body forms ())))
359 (setf (leaf-name res) "top-level form")
360 (setf (functional-entry-function res) res)
361 (setf (functional-arg-documentation res) ())
362 (setf (functional-kind res) :top-level)
365 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
366 ;;; form number to associate with a source path. This should be bound
367 ;;; to an initial value of 0 before the processing of each truly
369 (declaim (type index *current-form-number*))
370 (defvar *current-form-number*)
372 ;;; This function is called on freshly read forms to record the
373 ;;; initial location of each form (and subform.) Form is the form to
374 ;;; find the paths in, and TLF-NUM is the top-level form number of the
375 ;;; truly top-level form.
377 ;;; This gets a bit interesting when the source code is circular. This
378 ;;; can (reasonably?) happen in the case of circular list constants.
379 (defun find-source-paths (form tlf-num)
380 (declare (type index tlf-num))
381 (let ((*current-form-number* 0))
382 (sub-find-source-paths form (list tlf-num)))
384 (defun sub-find-source-paths (form path)
385 (unless (gethash form *source-paths*)
386 (setf (gethash form *source-paths*)
387 (list* 'original-source-start *current-form-number* path))
388 (incf *current-form-number*)
392 (declare (fixnum pos))
395 (when (atom subform) (return))
396 (let ((fm (car subform)))
398 (sub-find-source-paths fm (cons pos path)))
400 (setq subform (cdr subform))
401 (when (eq subform trail) (return)))))
405 (setq trail (cdr trail)))))))
407 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
409 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
410 ;; out of the body and converts a proxy form instead.
411 (ir1-error-bailout ((start
415 (proxy ``(error "execution of a form compiled with errors:~% ~S"
418 (let ((skip (gensym "SKIP")))
420 (catch 'ir1-error-abort
421 (let ((*compiler-error-bailout*
423 (throw 'ir1-error-abort nil))))
425 (return-from ,skip nil)))
426 (ir1-convert ,start ,cont ,proxy)))))
428 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
429 ;; continuation START. CONT is the continuation which receives the
430 ;; value of the FORM to be translated. The translators call this
431 ;; function recursively to translate their subnodes.
433 ;; As a special hack to make life easier in the compiler, a LEAF
434 ;; IR1-converts into a reference to that LEAF structure. This allows
435 ;; the creation using backquote of forms that contain leaf
436 ;; references, without having to introduce dummy names into the
438 (declaim (ftype (function (continuation continuation t) (values)) ir1-convert))
439 (defun ir1-convert (start cont form)
440 (ir1-error-bailout (start cont form)
441 (let ((*current-path* (or (gethash form *source-paths*)
442 (cons form *current-path*))))
444 (cond ((and (symbolp form) (not (keywordp form)))
445 (ir1-convert-variable start cont form))
447 (reference-leaf start cont form))
449 (reference-constant start cont form)))
450 (let ((fun (car form)))
453 (let ((lexical-def (lexenv-find fun functions)))
454 (typecase lexical-def
455 (null (ir1-convert-global-functoid start cont form))
457 (ir1-convert-local-combination start
462 (ir1-convert-srctran start cont lexical-def form))
464 (aver (and (consp lexical-def)
465 (eq (car lexical-def) 'macro)))
466 (ir1-convert start cont
467 (careful-expand-macro (cdr lexical-def)
469 ((or (atom fun) (not (eq (car fun) 'lambda)))
470 (compiler-error "illegal function call"))
472 (ir1-convert-combination start
475 (ir1-convert-lambda fun))))))))
478 ;; Generate a reference to a manifest constant, creating a new leaf
479 ;; if necessary. If we are producing a fasl file, make sure that
480 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
482 (defun reference-constant (start cont value)
483 (declare (type continuation start cont)
484 (inline find-constant))
487 '(error "attempt to reference undumpable constant"))
488 (when (producing-fasl-file)
489 (maybe-emit-make-load-forms value))
490 (let* ((leaf (find-constant value))
491 (res (make-ref (leaf-type leaf) leaf)))
492 (push res (leaf-refs leaf))
493 (prev-link res start)
494 (use-continuation res cont)))
497 ;;; Add Fun to the COMPONENT-REANALYZE-FUNCTIONS. Fun is returned.
498 (defun maybe-reanalyze-function (fun)
499 (declare (type functional fun))
500 (when (typep fun '(or optional-dispatch clambda))
501 (pushnew fun (component-reanalyze-functions *current-component*)))
504 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
505 ;;; needed. If LEAF represents a defined function which has already
506 ;;; been converted, and is not :NOTINLINE, then reference the
507 ;;; functional instead.
508 (defun reference-leaf (start cont leaf)
509 (declare (type continuation start cont) (type leaf leaf))
510 (let* ((leaf (or (and (defined-function-p leaf)
511 (not (eq (defined-function-inlinep leaf)
513 (let ((fun (defined-function-functional leaf)))
514 (when (and fun (not (functional-kind fun)))
515 (maybe-reanalyze-function fun))))
517 (res (make-ref (or (lexenv-find leaf type-restrictions)
520 (push res (leaf-refs leaf))
521 (setf (leaf-ever-used leaf) t)
522 (prev-link res start)
523 (use-continuation res cont)))
525 ;;; Convert a reference to a symbolic constant or variable. If the
526 ;;; symbol is entered in the LEXENV-VARIABLES we use that definition,
527 ;;; otherwise we find the current global definition. This is also
528 ;;; where we pick off symbol macro and Alien variable references.
529 (defun ir1-convert-variable (start cont name)
530 (declare (type continuation start cont) (symbol name))
531 (let ((var (or (lexenv-find name variables) (find-free-variable name))))
534 (when (and (lambda-var-p var) (lambda-var-ignorep var))
535 ;; (ANSI's specification for the IGNORE declaration requires
536 ;; that this be a STYLE-WARNING, not a full WARNING.)
537 (compiler-style-warning "reading an ignored variable: ~S" name))
538 (reference-leaf start cont var))
540 (aver (eq (car var) 'MACRO))
541 (ir1-convert start cont (cdr var)))
543 (ir1-convert start cont `(%heap-alien ',var)))))
546 ;;; Convert anything that looks like a special form, global function
548 (defun ir1-convert-global-functoid (start cont form)
549 (declare (type continuation start cont) (list form))
550 (let* ((fun (first form))
551 (translator (info :function :ir1-convert fun))
552 (cmacro (info :function :compiler-macro-function fun)))
553 (cond (translator (funcall translator start cont form))
555 (not (eq (info :function :inlinep fun)
557 (let ((res (careful-expand-macro cmacro form)))
559 (ir1-convert-global-functoid-no-cmacro start cont form fun)
560 (ir1-convert start cont res))))
562 (ir1-convert-global-functoid-no-cmacro start cont form fun)))))
564 ;;; Handle the case of where the call was not a compiler macro, or was a
565 ;;; compiler macro and passed.
566 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
567 (declare (type continuation start cont) (list form))
568 ;; FIXME: Couldn't all the INFO calls here be converted into
569 ;; standard CL functions, like MACRO-FUNCTION or something?
570 ;; And what happens with lexically-defined (MACROLET) macros
572 (ecase (info :function :kind fun)
576 (careful-expand-macro (info :function :macro-function fun)
579 (ir1-convert-srctran start cont (find-free-function fun "Eh?") form))))
581 (defun muffle-warning-or-die ()
583 (error "internal error -- no MUFFLE-WARNING restart"))
585 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
586 ;;; errors which occur during the macroexpansion.
587 (defun careful-expand-macro (fun form)
588 (handler-bind (;; When cross-compiling, we can get style warnings
589 ;; about e.g. undefined functions. An unhandled
590 ;; CL:STYLE-WARNING (as opposed to a
591 ;; SB!C::COMPILER-NOTE) would cause FAILURE-P to be
592 ;; set on the return from #'SB!XC:COMPILE-FILE, which
593 ;; would falsely indicate an error sufficiently
594 ;; serious that we should stop the build process. To
595 ;; avoid this, we translate CL:STYLE-WARNING
596 ;; conditions from the host Common Lisp into
597 ;; cross-compiler SB!C::COMPILER-NOTE calls. (It
598 ;; might be cleaner to just make Python use
599 ;; CL:STYLE-WARNING internally, so that the
600 ;; significance of any host Common Lisp
601 ;; CL:STYLE-WARNINGs is understood automatically. But
602 ;; for now I'm not motivated to do this. -- WHN
604 (style-warning (lambda (c)
605 (compiler-note "(during macroexpansion)~%~A"
607 (muffle-warning-or-die)))
608 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
609 ;; Debian Linux, anyway) raises a CL:WARNING
610 ;; condition (not a CL:STYLE-WARNING) for undefined
611 ;; symbols when converting interpreted functions,
612 ;; causing COMPILE-FILE to think the file has a real
613 ;; problem, causing COMPILE-FILE to return FAILURE-P
614 ;; set (not just WARNINGS-P set). Since undefined
615 ;; symbol warnings are often harmless forward
616 ;; references, and since it'd be inordinately painful
617 ;; to try to eliminate all such forward references,
618 ;; these warnings are basically unavoidable. Thus, we
619 ;; need to coerce the system to work through them,
620 ;; and this code does so, by crudely suppressing all
621 ;; warnings in cross-compilation macroexpansion. --
626 "(during macroexpansion)~%~
628 (KLUDGE: That was a non-STYLE WARNING.~%~
629 Ordinarily that would cause compilation to~%~
630 fail. However, since we're running under~%~
631 CMU CL, and since CMU CL emits non-STYLE~%~
632 warnings for safe, hard-to-fix things (e.g.~%~
633 references to not-yet-defined functions)~%~
634 we're going to have to ignore it and proceed~%~
635 anyway. Hopefully we're not ignoring anything~%~
638 (muffle-warning-or-die)))
640 (compiler-error "(during macroexpansion)~%~A" c))))
641 (funcall sb!xc:*macroexpand-hook*
646 ;;;; conversion utilities
648 ;;; Convert a bunch of forms, discarding all the values except the
649 ;;; last. If there aren't any forms, then translate a NIL.
650 (declaim (ftype (function (continuation continuation list) (values))
651 ir1-convert-progn-body))
652 (defun ir1-convert-progn-body (start cont body)
654 (reference-constant start cont nil)
655 (let ((this-start start)
658 (let ((form (car forms)))
659 (when (endp (cdr forms))
660 (ir1-convert this-start cont form)
662 (let ((this-cont (make-continuation)))
663 (ir1-convert this-start this-cont form)
664 (setq this-start this-cont forms (cdr forms)))))))
667 ;;;; converting combinations
669 ;;; Convert a function call where the function (Fun) is a Leaf. We
670 ;;; return the Combination node so that we can poke at it if we want to.
671 (declaim (ftype (function (continuation continuation list leaf) combination)
672 ir1-convert-combination))
673 (defun ir1-convert-combination (start cont form fun)
674 (let ((fun-cont (make-continuation)))
675 (reference-leaf start fun-cont fun)
676 (ir1-convert-combination-args fun-cont cont (cdr form))))
678 ;;; Convert the arguments to a call and make the Combination node. Fun-Cont
679 ;;; is the continuation which yields the function to call. Form is the source
680 ;;; for the call. Args is the list of arguments for the call, which defaults
681 ;;; to the cdr of source. We return the Combination node.
682 (defun ir1-convert-combination-args (fun-cont cont args)
683 (declare (type continuation fun-cont cont) (list args))
684 (let ((node (make-combination fun-cont)))
685 (setf (continuation-dest fun-cont) node)
686 (assert-continuation-type fun-cont
687 (specifier-type '(or function symbol)))
688 (collect ((arg-conts))
689 (let ((this-start fun-cont))
691 (let ((this-cont (make-continuation node)))
692 (ir1-convert this-start this-cont arg)
693 (setq this-start this-cont)
694 (arg-conts this-cont)))
695 (prev-link node this-start)
696 (use-continuation node cont)
697 (setf (combination-args node) (arg-conts))))
700 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
701 ;;; source transforms and try out any inline expansion. If there is no
702 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
703 ;;; known function which will quite possibly be open-coded.) Next, we
704 ;;; go to ok-combination conversion.
705 (defun ir1-convert-srctran (start cont var form)
706 (declare (type continuation start cont) (type global-var var))
707 (let ((inlinep (when (defined-function-p var)
708 (defined-function-inlinep var))))
709 (if (eq inlinep :notinline)
710 (ir1-convert-combination start cont form var)
711 (let ((transform (info :function :source-transform (leaf-name var))))
713 (multiple-value-bind (result pass) (funcall transform form)
715 (ir1-convert-maybe-predicate start cont form var)
716 (ir1-convert start cont result)))
717 (ir1-convert-maybe-predicate start cont form var))))))
719 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
720 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
721 ;;; predicate always appears in a conditional context.
723 ;;; If the function isn't a predicate, then we call
724 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
725 (defun ir1-convert-maybe-predicate (start cont form var)
726 (declare (type continuation start cont) (list form) (type global-var var))
727 (let ((info (info :function :info (leaf-name var))))
729 (ir1-attributep (function-info-attributes info) predicate)
730 (not (if-p (continuation-dest cont))))
731 (ir1-convert start cont `(if ,form t nil))
732 (ir1-convert-combination-checking-type start cont form var))))
734 ;;; Actually really convert a global function call that we are allowed
737 ;;; If we know the function type of the function, then we check the
738 ;;; call for syntactic legality with respect to the declared function
739 ;;; type. If it is impossible to determine whether the call is correct
740 ;;; due to non-constant keywords, then we give up, marking the call as
741 ;;; :FULL to inhibit further error messages. We return true when the
744 ;;; If the call is legal, we also propagate type assertions from the
745 ;;; function type to the arg and result continuations. We do this now
746 ;;; so that IR1 optimize doesn't have to redundantly do the check
747 ;;; later so that it can do the type propagation.
748 (defun ir1-convert-combination-checking-type (start cont form var)
749 (declare (type continuation start cont) (list form) (type leaf var))
750 (let* ((node (ir1-convert-combination start cont form var))
751 (fun-cont (basic-combination-fun node))
752 (type (leaf-type var)))
753 (when (validate-call-type node type t)
754 (setf (continuation-%derived-type fun-cont) type)
755 (setf (continuation-reoptimize fun-cont) nil)
756 (setf (continuation-%type-check fun-cont) nil)))
760 ;;; Convert a call to a local function. If the function has already
761 ;;; been let converted, then throw FUN to LOCAL-CALL-LOSSAGE. This
762 ;;; should only happen when we are converting inline expansions for
763 ;;; local functions during optimization.
764 (defun ir1-convert-local-combination (start cont form fun)
765 (if (functional-kind fun)
766 (throw 'local-call-lossage fun)
767 (ir1-convert-combination start cont form
768 (maybe-reanalyze-function fun))))
772 ;;; Given a list of Lambda-Var structures and a variable name, return
773 ;;; the structure for that name, or NIL if it isn't found. We return
774 ;;; the *last* variable with that name, since LET* bindings may be
775 ;;; duplicated, and declarations always apply to the last.
776 (declaim (ftype (function (list symbol) (or lambda-var list))
778 (defun find-in-bindings (vars name)
782 (when (eq (leaf-name var) name)
784 (let ((info (lambda-var-arg-info var)))
786 (let ((supplied-p (arg-info-supplied-p info)))
787 (when (and supplied-p
788 (eq (leaf-name supplied-p) name))
789 (setq found supplied-p))))))
790 ((and (consp var) (eq (car var) name))
791 (setf found (cdr var)))))
794 ;;; Called by Process-Decls to deal with a variable type declaration.
795 ;;; If a lambda-var being bound, we intersect the type with the vars
796 ;;; type, otherwise we add a type-restriction on the var. If a symbol
797 ;;; macro, we just wrap a THE around the expansion.
798 (defun process-type-decl (decl res vars)
799 (declare (list decl vars) (type lexenv res))
800 (let ((type (specifier-type (first decl))))
801 (collect ((restr nil cons)
803 (dolist (var-name (rest decl))
804 (let* ((bound-var (find-in-bindings vars var-name))
806 (lexenv-find var-name variables)
807 (find-free-variable var-name))))
810 (let* ((old-type (or (lexenv-find var type-restrictions)
812 (int (if (or (function-type-p type)
813 (function-type-p old-type))
815 (type-approx-intersection2 old-type type))))
816 (cond ((eq int *empty-type*)
817 (unless (policy *lexenv* (= inhibit-warnings 3))
819 "The type declarations ~S and ~S for ~S conflict."
820 (type-specifier old-type) (type-specifier type)
822 (bound-var (setf (leaf-type bound-var) int))
824 (restr (cons var int))))))
826 ;; FIXME: non-ANSI weirdness
827 (aver (eq (car var) 'MACRO))
828 (new-vars `(,var-name . (MACRO . (the ,(first decl)
832 "~S is an alien variable, so its type can't be declared."
835 (if (or (restr) (new-vars))
836 (make-lexenv :default res
837 :type-restrictions (restr)
838 :variables (new-vars))
841 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
842 ;;; declarations for function variables. In addition to allowing
843 ;;; declarations for functions being bound, we must also deal with
844 ;;; declarations that constrain the type of lexically apparent
846 (defun process-ftype-decl (spec res names fvars)
847 (declare (list spec names fvars) (type lexenv res))
848 (let ((type (specifier-type spec)))
849 (collect ((res nil cons))
851 (let ((found (find name fvars :key #'leaf-name :test #'equal)))
854 (setf (leaf-type found) type)
855 (assert-definition-type found type
856 :warning-function #'compiler-note
857 :where "FTYPE declaration"))
859 (res (cons (find-lexically-apparent-function
860 name "in a function type declaration")
863 (make-lexenv :default res :type-restrictions (res))
866 ;;; Process a special declaration, returning a new LEXENV. A non-bound
867 ;;; special declaration is instantiated by throwing a special variable
868 ;;; into the variables.
869 (defun process-special-decl (spec res vars)
870 (declare (list spec vars) (type lexenv res))
871 (collect ((new-venv nil cons))
872 (dolist (name (cdr spec))
873 (let ((var (find-in-bindings vars name)))
876 (aver (eq (car var) 'MACRO))
878 "~S is a symbol-macro and thus can't be declared special."
881 (when (lambda-var-ignorep var)
882 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
883 ;; requires that this be a STYLE-WARNING, not a full WARNING.
884 (compiler-style-warning
885 "The ignored variable ~S is being declared special."
887 (setf (lambda-var-specvar var)
888 (specvar-for-binding name)))
890 (unless (assoc name (new-venv) :test #'eq)
891 (new-venv (cons name (specvar-for-binding name))))))))
893 (make-lexenv :default res :variables (new-venv))
896 ;;; Return a DEFINED-FUNCTION which copies a global-var but for its inlinep.
897 (defun make-new-inlinep (var inlinep)
898 (declare (type global-var var) (type inlinep inlinep))
899 (let ((res (make-defined-function
900 :name (leaf-name var)
901 :where-from (leaf-where-from var)
902 :type (leaf-type var)
904 (when (defined-function-p var)
905 (setf (defined-function-inline-expansion res)
906 (defined-function-inline-expansion var))
907 (setf (defined-function-functional res)
908 (defined-function-functional var)))
911 ;;; Parse an inline/notinline declaration. If it's a local function we're
912 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
913 (defun process-inline-decl (spec res fvars)
914 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
916 (dolist (name (rest spec))
917 (let ((fvar (find name fvars :key #'leaf-name :test #'equal)))
919 (setf (functional-inlinep fvar) sense)
921 (find-lexically-apparent-function
922 name "in an inline or notinline declaration")))
925 (when (policy *lexenv* (>= speed inhibit-warnings))
926 (compiler-note "ignoring ~A declaration not at ~
927 definition of local function:~% ~S"
930 (push (cons name (make-new-inlinep found sense))
934 (make-lexenv :default res :functions new-fenv)
937 ;;; Like FIND-IN-BINDINGS, but looks for #'foo in the fvars.
938 (defun find-in-bindings-or-fbindings (name vars fvars)
939 (declare (list vars fvars))
941 (destructuring-bind (wot fn-name) name
942 (unless (eq wot 'function)
943 (compiler-error "The function or variable name ~S is unrecognizable."
945 (find fn-name fvars :key #'leaf-name :test #'equal))
946 (find-in-bindings vars name)))
948 ;;; Process an ignore/ignorable declaration, checking for various losing
950 (defun process-ignore-decl (spec vars fvars)
951 (declare (list spec vars fvars))
952 (dolist (name (rest spec))
953 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
956 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
957 ;; requires that this be a STYLE-WARNING, not a full WARNING.
958 (compiler-style-warning "declaring unknown variable ~S to be ignored"
960 ;; FIXME: This special case looks like non-ANSI weirdness.
961 ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
962 ;; Just ignore the IGNORE decl.
965 (setf (leaf-ever-used var) t))
966 ((lambda-var-specvar var)
967 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
968 ;; requires that this be a STYLE-WARNING, not a full WARNING.
969 (compiler-style-warning "declaring special variable ~S to be ignored"
971 ((eq (first spec) 'ignorable)
972 (setf (leaf-ever-used var) t))
974 (setf (lambda-var-ignorep var) t)))))
977 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
978 ;;; go away, I think.
979 (defvar *suppress-values-declaration* nil
981 "If true, processing of the VALUES declaration is inhibited.")
983 ;;; Process a single declaration spec, augmenting the specified LEXENV
984 ;;; RES and returning it as a result. VARS and FVARS are as described in
986 (defun process-1-decl (raw-spec res vars fvars cont)
987 (declare (type list raw-spec vars fvars))
988 (declare (type lexenv res))
989 (declare (type continuation cont))
990 (let ((spec (canonized-decl-spec raw-spec)))
992 (special (process-special-decl spec res vars))
995 (compiler-error "No type specified in FTYPE declaration: ~S" spec))
996 (process-ftype-decl (second spec) res (cddr spec) fvars))
997 ((inline notinline maybe-inline)
998 (process-inline-decl spec res fvars))
1000 (process-ignore-decl spec vars fvars)
1005 :policy (process-optimize-decl spec (lexenv-policy res))))
1007 (process-type-decl (cdr spec) res vars))
1009 (if *suppress-values-declaration*
1011 (let ((types (cdr spec)))
1012 (do-the-stuff (if (eql (length types) 1)
1015 cont res 'values))))
1017 (when (policy *lexenv* (> speed inhibit-warnings))
1019 "compiler limitation:~
1020 ~% There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1023 (unless (info :declaration :recognized (first spec))
1024 (compiler-warning "unrecognized declaration ~S" raw-spec))
1027 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1028 ;;; and FUNCTIONAL structures which are being bound. In addition to
1029 ;;; filling in slots in the leaf structures, we return a new LEXENV
1030 ;;; which reflects pervasive special and function type declarations,
1031 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1032 ;;; continuation affected by VALUES declarations.
1034 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1036 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1037 (declare (list decls vars fvars) (type continuation cont))
1038 (dolist (decl decls)
1039 (dolist (spec (rest decl))
1040 (unless (consp spec)
1041 (compiler-error "malformed declaration specifier ~S in ~S"
1044 (setq env (process-1-decl spec env vars fvars cont))))
1047 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1048 ;;; declaration. If there is a global variable of that name, then
1049 ;;; check that it isn't a constant and return it. Otherwise, create an
1050 ;;; anonymous GLOBAL-VAR.
1051 (defun specvar-for-binding (name)
1052 (cond ((not (eq (info :variable :where-from name) :assumed))
1053 (let ((found (find-free-variable name)))
1054 (when (heap-alien-info-p found)
1056 "~S is an alien variable and so can't be declared special."
1058 (when (or (not (global-var-p found))
1059 (eq (global-var-kind found) :constant))
1061 "~S is a constant and so can't be declared special."
1065 (make-global-var :kind :special
1067 :where-from :declared))))
1071 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
1072 ;;;; function representation" before you seriously mess with this
1075 ;;; Verify that a thing is a legal name for a variable and return a
1076 ;;; Var structure for it, filling in info if it is globally special.
1077 ;;; If it is losing, we punt with a Compiler-Error. Names-So-Far is an
1078 ;;; alist of names which have previously been bound. If the name is in
1079 ;;; this list, then we error out.
1080 (declaim (ftype (function (t list) lambda-var) varify-lambda-arg))
1081 (defun varify-lambda-arg (name names-so-far)
1082 (declare (inline member))
1083 (unless (symbolp name)
1084 (compiler-error "The lambda-variable ~S is not a symbol." name))
1085 (when (member name names-so-far :test #'eq)
1086 (compiler-error "The variable ~S occurs more than once in the lambda-list."
1088 (let ((kind (info :variable :kind name)))
1089 (when (or (keywordp name) (eq kind :constant))
1090 (compiler-error "The name of the lambda-variable ~S is a constant."
1092 (cond ((eq kind :special)
1093 (let ((specvar (find-free-variable name)))
1094 (make-lambda-var :name name
1095 :type (leaf-type specvar)
1096 :where-from (leaf-where-from specvar)
1099 (note-lexical-binding name)
1100 (make-lambda-var :name name)))))
1102 ;;; Make the default keyword for a &KEY arg, checking that the keyword
1103 ;;; isn't already used by one of the VARS. We also check that the
1104 ;;; keyword isn't the magical :ALLOW-OTHER-KEYS.
1105 (declaim (ftype (function (symbol list t) keyword) make-keyword-for-arg))
1106 (defun make-keyword-for-arg (symbol vars keywordify)
1107 (let ((key (if (and keywordify (not (keywordp symbol)))
1108 (keywordicate symbol)
1110 (when (eq key :allow-other-keys)
1111 (compiler-error "No &KEY arg can be called :ALLOW-OTHER-KEYS."))
1113 (let ((info (lambda-var-arg-info var)))
1115 (eq (arg-info-kind info) :keyword)
1116 (eq (arg-info-key info) key))
1118 "The keyword ~S appears more than once in the lambda-list."
1122 ;;; Parse a lambda-list into a list of VAR structures, stripping off
1123 ;;; any aux bindings. Each arg name is checked for legality, and
1124 ;;; duplicate names are checked for. If an arg is globally special,
1125 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
1126 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
1127 ;;; which contains the extra information. If we hit something losing,
1128 ;;; we bug out with COMPILER-ERROR. These values are returned:
1129 ;;; 1. a list of the var structures for each top-level argument;
1130 ;;; 2. a flag indicating whether &KEY was specified;
1131 ;;; 3. a flag indicating whether other &KEY args are allowed;
1132 ;;; 4. a list of the &AUX variables; and
1133 ;;; 5. a list of the &AUX values.
1134 (declaim (ftype (function (list) (values list boolean boolean list list))
1136 (defun find-lambda-vars (list)
1137 (multiple-value-bind (required optional restp rest keyp keys allowp aux
1138 morep more-context more-count)
1139 (parse-lambda-list list)
1144 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
1145 ;; for optionals and keywords args.
1146 (parse-default (spec info)
1147 (when (consp (cdr spec))
1148 (setf (arg-info-default info) (second spec))
1149 (when (consp (cddr spec))
1150 (let* ((supplied-p (third spec))
1151 (supplied-var (varify-lambda-arg supplied-p
1153 (setf (arg-info-supplied-p info) supplied-var)
1154 (names-so-far supplied-p)
1155 (when (> (length (the list spec)) 3)
1157 "The list ~S is too long to be an arg specifier."
1160 (dolist (name required)
1161 (let ((var (varify-lambda-arg name (names-so-far))))
1163 (names-so-far name)))
1165 (dolist (spec optional)
1167 (let ((var (varify-lambda-arg spec (names-so-far))))
1168 (setf (lambda-var-arg-info var) (make-arg-info :kind :optional))
1170 (names-so-far spec))
1171 (let* ((name (first spec))
1172 (var (varify-lambda-arg name (names-so-far)))
1173 (info (make-arg-info :kind :optional)))
1174 (setf (lambda-var-arg-info var) info)
1177 (parse-default spec info))))
1180 (let ((var (varify-lambda-arg rest (names-so-far))))
1181 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
1183 (names-so-far rest)))
1186 (let ((var (varify-lambda-arg more-context (names-so-far))))
1187 (setf (lambda-var-arg-info var)
1188 (make-arg-info :kind :more-context))
1190 (names-so-far more-context))
1191 (let ((var (varify-lambda-arg more-count (names-so-far))))
1192 (setf (lambda-var-arg-info var)
1193 (make-arg-info :kind :more-count))
1195 (names-so-far more-count)))
1200 (let ((var (varify-lambda-arg spec (names-so-far))))
1201 (setf (lambda-var-arg-info var)
1202 (make-arg-info :kind :keyword
1203 :key (make-keyword-for-arg spec
1207 (names-so-far spec)))
1208 ((atom (first spec))
1209 (let* ((name (first spec))
1210 (var (varify-lambda-arg name (names-so-far)))
1211 (info (make-arg-info
1213 :key (make-keyword-for-arg name (vars) t))))
1214 (setf (lambda-var-arg-info var) info)
1217 (parse-default spec info)))
1219 (let ((head (first spec)))
1220 (unless (proper-list-of-length-p head 2)
1221 (error "malformed &KEY argument specifier: ~S" spec))
1222 (let* ((name (second head))
1223 (var (varify-lambda-arg name (names-so-far)))
1224 (info (make-arg-info
1226 :key (make-keyword-for-arg (first head)
1229 (setf (lambda-var-arg-info var) info)
1232 (parse-default spec info))))))
1236 (let ((var (varify-lambda-arg spec nil)))
1239 (names-so-far spec)))
1241 (unless (proper-list-of-length-p spec 1 2)
1242 (compiler-error "malformed &AUX binding specifier: ~S"
1244 (let* ((name (first spec))
1245 (var (varify-lambda-arg name nil)))
1247 (aux-vals (second spec))
1248 (names-so-far name)))))
1250 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
1252 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
1253 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
1254 ;;; converting the body. If there are no bindings, just convert the
1255 ;;; body, otherwise do one binding and recurse on the rest.
1256 (defun ir1-convert-aux-bindings (start cont body aux-vars aux-vals)
1257 (declare (type continuation start cont) (list body aux-vars aux-vals))
1259 (ir1-convert-progn-body start cont body)
1260 (let ((fun-cont (make-continuation))
1261 (fun (ir1-convert-lambda-body body
1262 (list (first aux-vars))
1263 :aux-vars (rest aux-vars)
1264 :aux-vals (rest aux-vals))))
1265 (reference-leaf start fun-cont fun)
1266 (ir1-convert-combination-args fun-cont cont
1267 (list (first aux-vals)))))
1270 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
1271 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
1272 ;;; around the body. If there are no special bindings, we just convert
1273 ;;; the body, otherwise we do one special binding and recurse on the
1276 ;;; We make a cleanup and introduce it into the lexical environment.
1277 ;;; If there are multiple special bindings, the cleanup for the blocks
1278 ;;; will end up being the innermost one. We force CONT to start a
1279 ;;; block outside of this cleanup, causing cleanup code to be emitted
1280 ;;; when the scope is exited.
1281 (defun ir1-convert-special-bindings (start cont body aux-vars aux-vals svars)
1282 (declare (type continuation start cont)
1283 (list body aux-vars aux-vals svars))
1286 (ir1-convert-aux-bindings start cont body aux-vars aux-vals))
1288 (continuation-starts-block cont)
1289 (let ((cleanup (make-cleanup :kind :special-bind))
1291 (next-cont (make-continuation))
1292 (nnext-cont (make-continuation)))
1293 (ir1-convert start next-cont
1294 `(%special-bind ',(lambda-var-specvar var) ,var))
1295 (setf (cleanup-mess-up cleanup) (continuation-use next-cont))
1296 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
1297 (ir1-convert next-cont nnext-cont '(%cleanup-point))
1298 (ir1-convert-special-bindings nnext-cont cont body aux-vars aux-vals
1302 ;;; Create a lambda node out of some code, returning the result. The
1303 ;;; bindings are specified by the list of VAR structures VARS. We deal
1304 ;;; with adding the names to the LEXENV-VARIABLES for the conversion.
1305 ;;; The result is added to the NEW-FUNCTIONS in the
1306 ;;; *CURRENT-COMPONENT* and linked to the component head and tail.
1308 ;;; We detect special bindings here, replacing the original VAR in the
1309 ;;; lambda list with a temporary variable. We then pass a list of the
1310 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
1311 ;;; the special binding code.
1313 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
1314 ;;; dealing with &nonsense.
1316 ;;; AUX-VARS is a list of VAR structures for variables that are to be
1317 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
1318 ;;; to get the initial value for the corresponding AUX-VAR.
1319 (defun ir1-convert-lambda-body (body vars &key aux-vars aux-vals result)
1320 (declare (list body vars aux-vars aux-vals)
1321 (type (or continuation null) result))
1322 (let* ((bind (make-bind))
1323 (lambda (make-lambda :vars vars :bind bind))
1324 (result (or result (make-continuation))))
1325 (setf (lambda-home lambda) lambda)
1327 (new-venv nil cons))
1330 ;; As far as I can see, LAMBDA-VAR-HOME should never have
1331 ;; been set before. Let's make sure. -- WHN 2001-09-29
1332 (aver (null (lambda-var-home var)))
1333 (setf (lambda-var-home var) lambda)
1334 (let ((specvar (lambda-var-specvar var)))
1337 (new-venv (cons (leaf-name specvar) specvar)))
1339 (note-lexical-binding (leaf-name var))
1340 (new-venv (cons (leaf-name var) var))))))
1342 (let ((*lexenv* (make-lexenv :variables (new-venv)
1345 (setf (bind-lambda bind) lambda)
1346 (setf (node-lexenv bind) *lexenv*)
1348 (let ((cont1 (make-continuation))
1349 (cont2 (make-continuation)))
1350 (continuation-starts-block cont1)
1351 (prev-link bind cont1)
1352 (use-continuation bind cont2)
1353 (ir1-convert-special-bindings cont2 result body aux-vars aux-vals
1356 (let ((block (continuation-block result)))
1358 (let ((return (make-return :result result :lambda lambda))
1359 (tail-set (make-tail-set :functions (list lambda)))
1360 (dummy (make-continuation)))
1361 (setf (lambda-tail-set lambda) tail-set)
1362 (setf (lambda-return lambda) return)
1363 (setf (continuation-dest result) return)
1364 (setf (block-last block) return)
1365 (prev-link return result)
1366 (use-continuation return dummy))
1367 (link-blocks block (component-tail *current-component*))))))
1369 (link-blocks (component-head *current-component*) (node-block bind))
1370 (push lambda (component-new-functions *current-component*))
1373 ;;; Create the actual entry-point function for an optional entry
1374 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
1375 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
1376 ;;; to the VARS by name. The VALS are passed in in reverse order.
1378 ;;; If any of the copies of the vars are referenced more than once,
1379 ;;; then we mark the corresponding var as EVER-USED to inhibit
1380 ;;; "defined but not read" warnings for arguments that are only used
1381 ;;; by default forms.
1382 (defun convert-optional-entry (fun vars vals defaults)
1383 (declare (type clambda fun) (list vars vals defaults))
1384 (let* ((fvars (reverse vars))
1385 (arg-vars (mapcar (lambda (var)
1386 (unless (lambda-var-specvar var)
1387 (note-lexical-binding (leaf-name var)))
1389 :name (leaf-name var)
1390 :type (leaf-type var)
1391 :where-from (leaf-where-from var)
1392 :specvar (lambda-var-specvar var)))
1395 (ir1-convert-lambda-body `((%funcall ,fun
1399 (mapc (lambda (var arg-var)
1400 (when (cdr (leaf-refs arg-var))
1401 (setf (leaf-ever-used var) t)))
1405 ;;; This function deals with supplied-p vars in optional arguments. If
1406 ;;; the there is no supplied-p arg, then we just call
1407 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
1408 ;;; optional entry that calls the result. If there is a supplied-p
1409 ;;; var, then we add it into the default vars and throw a T into the
1410 ;;; entry values. The resulting entry point function is returned.
1411 (defun generate-optional-default-entry (res default-vars default-vals
1412 entry-vars entry-vals
1413 vars supplied-p-p body
1414 aux-vars aux-vals cont)
1415 (declare (type optional-dispatch res)
1416 (list default-vars default-vals entry-vars entry-vals vars body
1418 (type (or continuation null) cont))
1419 (let* ((arg (first vars))
1420 (arg-name (leaf-name arg))
1421 (info (lambda-var-arg-info arg))
1422 (supplied-p (arg-info-supplied-p info))
1424 (ir1-convert-hairy-args
1426 (list* supplied-p arg default-vars)
1427 (list* (leaf-name supplied-p) arg-name default-vals)
1428 (cons arg entry-vars)
1429 (list* t arg-name entry-vals)
1430 (rest vars) t body aux-vars aux-vals cont)
1431 (ir1-convert-hairy-args
1433 (cons arg default-vars)
1434 (cons arg-name default-vals)
1435 (cons arg entry-vars)
1436 (cons arg-name entry-vals)
1437 (rest vars) supplied-p-p body aux-vars aux-vals cont))))
1439 (convert-optional-entry ep default-vars default-vals
1441 (list (arg-info-default info) nil)
1442 (list (arg-info-default info))))))
1444 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
1445 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
1446 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
1448 ;;; The most interesting thing that we do is parse keywords. We create
1449 ;;; a bunch of temporary variables to hold the result of the parse,
1450 ;;; and then loop over the supplied arguments, setting the appropriate
1451 ;;; temps for the supplied keyword. Note that it is significant that
1452 ;;; we iterate over the keywords in reverse order --- this implements
1453 ;;; the CL requirement that (when a keyword appears more than once)
1454 ;;; the first value is used.
1456 ;;; If there is no supplied-p var, then we initialize the temp to the
1457 ;;; default and just pass the temp into the main entry. Since
1458 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
1459 ;;; know that the default is constant, and thus safe to evaluate out
1462 ;;; If there is a supplied-p var, then we create temps for both the
1463 ;;; value and the supplied-p, and pass them into the main entry,
1464 ;;; letting it worry about defaulting.
1466 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
1467 ;;; until we have scanned all the keywords.
1468 (defun convert-more-entry (res entry-vars entry-vals rest morep keys)
1469 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
1470 (collect ((arg-vars)
1471 (arg-vals (reverse entry-vals))
1475 (dolist (var (reverse entry-vars))
1476 (arg-vars (make-lambda-var :name (leaf-name var)
1477 :type (leaf-type var)
1478 :where-from (leaf-where-from var))))
1480 (let* ((n-context (gensym "N-CONTEXT-"))
1481 (context-temp (make-lambda-var :name n-context))
1482 (n-count (gensym "N-COUNT-"))
1483 (count-temp (make-lambda-var :name n-count
1484 :type (specifier-type 'index))))
1486 (arg-vars context-temp count-temp)
1489 (arg-vals `(%listify-rest-args ,n-context ,n-count)))
1491 (arg-vals n-context)
1494 (when (optional-dispatch-keyp res)
1495 (let ((n-index (gensym "N-INDEX-"))
1496 (n-key (gensym "N-KEY-"))
1497 (n-value-temp (gensym "N-VALUE-TEMP-"))
1498 (n-allowp (gensym "N-ALLOWP-"))
1499 (n-losep (gensym "N-LOSEP-"))
1500 (allowp (or (optional-dispatch-allowp res)
1501 (policy *lexenv* (zerop safety)))))
1503 (temps `(,n-index (1- ,n-count)) n-key n-value-temp)
1504 (body `(declare (fixnum ,n-index) (ignorable ,n-key ,n-value-temp)))
1508 (let* ((info (lambda-var-arg-info key))
1509 (default (arg-info-default info))
1510 (keyword (arg-info-key info))
1511 (supplied-p (arg-info-supplied-p info))
1512 (n-value (gensym "N-VALUE-")))
1513 (temps `(,n-value ,default))
1515 (let ((n-supplied (gensym "N-SUPPLIED-")))
1517 (arg-vals n-value n-supplied)
1518 (tests `((eq ,n-key ',keyword)
1519 (setq ,n-supplied t)
1520 (setq ,n-value ,n-value-temp)))))
1523 (tests `((eq ,n-key ',keyword)
1524 (setq ,n-value ,n-value-temp)))))))
1527 (temps n-allowp n-losep)
1528 (tests `((eq ,n-key :allow-other-keys)
1529 (setq ,n-allowp ,n-value-temp)))
1531 (setq ,n-losep ,n-key))))
1534 `(when (oddp ,n-count)
1535 (%odd-key-arguments-error)))
1539 (declare (optimize (safety 0)))
1541 (when (minusp ,n-index) (return))
1542 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
1544 (setq ,n-key (%more-arg ,n-context ,n-index))
1549 (body `(when (and ,n-losep (not ,n-allowp))
1550 (%unknown-key-argument-error ,n-losep)))))))
1552 (let ((ep (ir1-convert-lambda-body
1555 (%funcall ,(optional-dispatch-main-entry res)
1556 . ,(arg-vals)))) ; FIXME: What is the '.'? ,@?
1558 (setf (optional-dispatch-more-entry res) ep))))
1562 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
1563 ;;; or &KEY arg. The arguments are similar to that function, but we
1564 ;;; split off any &REST arg and pass it in separately. REST is the
1565 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
1566 ;;; the &KEY argument vars.
1568 ;;; When there are &KEY arguments, we introduce temporary gensym
1569 ;;; variables to hold the values while keyword defaulting is in
1570 ;;; progress to get the required sequential binding semantics.
1572 ;;; This gets interesting mainly when there are &KEY arguments with
1573 ;;; supplied-p vars or non-constant defaults. In either case, pass in
1574 ;;; a supplied-p var. If the default is non-constant, we introduce an
1575 ;;; IF in the main entry that tests the supplied-p var and decides
1576 ;;; whether to evaluate the default or not. In this case, the real
1577 ;;; incoming value is NIL, so we must union NULL with the declared
1578 ;;; type when computing the type for the main entry's argument.
1579 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
1580 rest more-context more-count keys supplied-p-p
1581 body aux-vars aux-vals cont)
1582 (declare (type optional-dispatch res)
1583 (list default-vars default-vals entry-vars entry-vals keys body
1585 (type (or continuation null) cont))
1586 (collect ((main-vars (reverse default-vars))
1587 (main-vals default-vals cons)
1594 (main-vars more-context)
1596 (main-vars more-count)
1600 (let* ((info (lambda-var-arg-info key))
1601 (default (arg-info-default info))
1602 (hairy-default (not (sb!xc:constantp default)))
1603 (supplied-p (arg-info-supplied-p info))
1604 (n-val (make-symbol (format nil
1605 "~A-DEFAULTING-TEMP"
1607 (key-type (leaf-type key))
1608 (val-temp (make-lambda-var
1610 :type (if hairy-default
1611 (type-union key-type (specifier-type 'null))
1613 (main-vars val-temp)
1615 (cond ((or hairy-default supplied-p)
1616 (let* ((n-supplied (gensym "N-SUPPLIED-"))
1617 (supplied-temp (make-lambda-var :name n-supplied)))
1619 (setf (arg-info-supplied-p info) supplied-temp))
1621 (setf (arg-info-default info) nil))
1622 (main-vars supplied-temp)
1623 (cond (hairy-default
1625 (bind-vals `(if ,n-supplied ,n-val ,default)))
1627 (main-vals default nil)
1630 (bind-vars supplied-p)
1631 (bind-vals n-supplied))))
1633 (main-vals (arg-info-default info))
1634 (bind-vals n-val)))))
1636 (let* ((main-entry (ir1-convert-lambda-body
1638 :aux-vars (append (bind-vars) aux-vars)
1639 :aux-vals (append (bind-vals) aux-vals)
1641 (last-entry (convert-optional-entry main-entry default-vars
1643 (setf (optional-dispatch-main-entry res) main-entry)
1644 (convert-more-entry res entry-vars entry-vals rest more-context keys)
1646 (push (if supplied-p-p
1647 (convert-optional-entry last-entry entry-vars entry-vals ())
1649 (optional-dispatch-entry-points res))
1652 ;;; This function generates the entry point functions for the
1653 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
1654 ;;; of arguments, analyzing the arglist on the way down and generating
1655 ;;; entry points on the way up.
1657 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
1658 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
1659 ;;; names of the DEFAULT-VARS.
1661 ;;; ENTRY-VARS is a reversed list of processed argument vars,
1662 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
1663 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
1664 ;;; It has the var name for each required or optional arg, and has T
1665 ;;; for each supplied-p arg.
1667 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
1668 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
1669 ;;; argument has already been processed; only in this case are the
1670 ;;; DEFAULT-XXX and ENTRY-XXX different.
1672 ;;; The result at each point is a lambda which should be called by the
1673 ;;; above level to default the remaining arguments and evaluate the
1674 ;;; body. We cause the body to be evaluated by converting it and
1675 ;;; returning it as the result when the recursion bottoms out.
1677 ;;; Each level in the recursion also adds its entry point function to
1678 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
1679 ;;; function and the entry point function will be the same, but when
1680 ;;; SUPPLIED-P args are present they may be different.
1682 ;;; When we run into a &REST or &KEY arg, we punt out to
1683 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
1684 (defun ir1-convert-hairy-args (res default-vars default-vals
1685 entry-vars entry-vals
1686 vars supplied-p-p body aux-vars
1688 (declare (type optional-dispatch res)
1689 (list default-vars default-vals entry-vars entry-vals vars body
1691 (type (or continuation null) cont))
1693 (if (optional-dispatch-keyp res)
1694 ;; Handle &KEY with no keys...
1695 (ir1-convert-more res default-vars default-vals
1696 entry-vars entry-vals
1697 nil nil nil vars supplied-p-p body aux-vars
1699 (let ((fun (ir1-convert-lambda-body body (reverse default-vars)
1703 (setf (optional-dispatch-main-entry res) fun)
1704 (push (if supplied-p-p
1705 (convert-optional-entry fun entry-vars entry-vals ())
1707 (optional-dispatch-entry-points res))
1709 ((not (lambda-var-arg-info (first vars)))
1710 (let* ((arg (first vars))
1711 (nvars (cons arg default-vars))
1712 (nvals (cons (leaf-name arg) default-vals)))
1713 (ir1-convert-hairy-args res nvars nvals nvars nvals
1714 (rest vars) nil body aux-vars aux-vals
1717 (let* ((arg (first vars))
1718 (info (lambda-var-arg-info arg))
1719 (kind (arg-info-kind info)))
1722 (let ((ep (generate-optional-default-entry
1723 res default-vars default-vals
1724 entry-vars entry-vals vars supplied-p-p body
1725 aux-vars aux-vals cont)))
1726 (push (if supplied-p-p
1727 (convert-optional-entry ep entry-vars entry-vals ())
1729 (optional-dispatch-entry-points res))
1732 (ir1-convert-more res default-vars default-vals
1733 entry-vars entry-vals
1734 arg nil nil (rest vars) supplied-p-p body
1735 aux-vars aux-vals cont))
1737 (ir1-convert-more res default-vars default-vals
1738 entry-vars entry-vals
1739 nil arg (second vars) (cddr vars) supplied-p-p
1740 body aux-vars aux-vals cont))
1742 (ir1-convert-more res default-vars default-vals
1743 entry-vars entry-vals
1744 nil nil nil vars supplied-p-p body aux-vars
1745 aux-vals cont)))))))
1747 ;;; This function deals with the case where we have to make an
1748 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
1749 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
1750 ;;; figure out the MIN-ARGS and MAX-ARGS.
1751 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals cont)
1752 (declare (list body vars aux-vars aux-vals) (type continuation cont))
1753 (let ((res (make-optional-dispatch :arglist vars
1756 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
1757 (push res (component-new-functions *current-component*))
1758 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
1760 (setf (optional-dispatch-min-args res) min)
1761 (setf (optional-dispatch-max-args res)
1762 (+ (1- (length (optional-dispatch-entry-points res))) min))
1766 (setf (functional-kind ep) :optional)
1767 (setf (leaf-ever-used ep) t)
1768 (setf (lambda-optional-dispatch ep) res))))
1769 (dolist (ep (optional-dispatch-entry-points res)) (frob ep))
1770 (frob (optional-dispatch-more-entry res))
1771 (frob (optional-dispatch-main-entry res)))
1775 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
1776 (defun ir1-convert-lambda (form &optional name)
1777 (unless (consp form)
1778 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
1781 (unless (eq (car form) 'lambda)
1782 (compiler-error "~S was expected but ~S was found:~% ~S"
1786 (unless (and (consp (cdr form)) (listp (cadr form)))
1788 "The lambda expression has a missing or non-list lambda-list:~% ~S"
1791 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
1792 (find-lambda-vars (cadr form))
1793 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr form))
1794 (let* ((cont (make-continuation))
1795 (*lexenv* (process-decls decls
1796 (append aux-vars vars)
1798 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
1799 (ir1-convert-hairy-lambda forms vars keyp
1801 aux-vars aux-vals cont)
1802 (ir1-convert-lambda-body forms vars
1806 (setf (functional-inline-expansion res) form)
1807 (setf (functional-arg-documentation res) (cadr form))
1808 (setf (leaf-name res) name)
1811 ;;; FIXME: This file is rather long, and contains two distinct sections,
1812 ;;; transform machinery above this point and transforms themselves below this
1813 ;;; point. Why not split it in two? (ir1translate.lisp and
1814 ;;; ir1translators.lisp?) Then consider byte-compiling the translators, too.
1816 ;;;; control special forms
1818 (def-ir1-translator progn ((&rest forms) start cont)
1821 Evaluates each Form in order, returning the values of the last form. With no
1822 forms, returns NIL."
1823 (ir1-convert-progn-body start cont forms))
1825 (def-ir1-translator if ((test then &optional else) start cont)
1827 "If Predicate Then [Else]
1828 If Predicate evaluates to non-null, evaluate Then and returns its values,
1829 otherwise evaluate Else and return its values. Else defaults to NIL."
1830 (let* ((pred (make-continuation))
1831 (then-cont (make-continuation))
1832 (then-block (continuation-starts-block then-cont))
1833 (else-cont (make-continuation))
1834 (else-block (continuation-starts-block else-cont))
1835 (dummy-cont (make-continuation))
1836 (node (make-if :test pred
1837 :consequent then-block
1838 :alternative else-block)))
1839 (setf (continuation-dest pred) node)
1840 (ir1-convert start pred test)
1841 (prev-link node pred)
1842 (use-continuation node dummy-cont)
1844 (let ((start-block (continuation-block pred)))
1845 (setf (block-last start-block) node)
1846 (continuation-starts-block cont)
1848 (link-blocks start-block then-block)
1849 (link-blocks start-block else-block)
1851 (ir1-convert then-cont cont then)
1852 (ir1-convert else-cont cont else))))
1854 ;;;; BLOCK and TAGBODY
1856 ;;;; We make an Entry node to mark the start and a :Entry cleanup to
1857 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an Exit
1860 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
1861 ;;; body in the modified environment. We make CONT start a block now,
1862 ;;; since if it was done later, the block would be in the wrong
1864 (def-ir1-translator block ((name &rest forms) start cont)
1867 Evaluate the Forms as a PROGN. Within the lexical scope of the body,
1868 (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
1869 result of Value-Form."
1870 (unless (symbolp name)
1871 (compiler-error "The block name ~S is not a symbol." name))
1872 (continuation-starts-block cont)
1873 (let* ((dummy (make-continuation))
1874 (entry (make-entry))
1875 (cleanup (make-cleanup :kind :block
1877 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
1878 (setf (entry-cleanup entry) cleanup)
1879 (prev-link entry start)
1880 (use-continuation entry dummy)
1882 (let* ((env-entry (list entry cont))
1883 (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
1885 (push env-entry (continuation-lexenv-uses cont))
1886 (ir1-convert-progn-body dummy cont forms))))
1889 ;;; We make CONT start a block just so that it will have a block
1890 ;;; assigned. People assume that when they pass a continuation into
1891 ;;; IR1-CONVERT as CONT, it will have a block when it is done.
1892 (def-ir1-translator return-from ((name &optional value)
1895 "Return-From Block-Name Value-Form
1896 Evaluate the Value-Form, returning its values from the lexically enclosing
1897 BLOCK Block-Name. This is constrained to be used only within the dynamic
1898 extent of the BLOCK."
1899 (continuation-starts-block cont)
1900 (let* ((found (or (lexenv-find name blocks)
1901 (compiler-error "return for unknown block: ~S" name)))
1902 (value-cont (make-continuation))
1903 (entry (first found))
1904 (exit (make-exit :entry entry
1905 :value value-cont)))
1906 (push exit (entry-exits entry))
1907 (setf (continuation-dest value-cont) exit)
1908 (ir1-convert start value-cont value)
1909 (prev-link exit value-cont)
1910 (use-continuation exit (second found))))
1912 ;;; Return a list of the segments of a TAGBODY. Each segment looks
1913 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
1914 ;;; tagbody into segments of non-tag statements, and explicitly
1915 ;;; represent the drop-through with a GO. The first segment has a
1916 ;;; dummy NIL tag, since it represents code before the first tag. The
1917 ;;; last segment (which may also be the first segment) ends in NIL
1918 ;;; rather than a GO.
1919 (defun parse-tagbody (body)
1920 (declare (list body))
1921 (collect ((segments))
1922 (let ((current (cons nil body)))
1924 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
1926 (segments `(,@current nil))
1928 (let ((tag (elt current tag-pos)))
1929 (when (assoc tag (segments))
1931 "The tag ~S appears more than once in the tagbody."
1933 (unless (or (symbolp tag) (integerp tag))
1934 (compiler-error "~S is not a legal tagbody statement." tag))
1935 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
1936 (setq current (nthcdr tag-pos current)))))
1939 ;;; Set up the cleanup, emitting the entry node. Then make a block for
1940 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
1941 ;;; Finally, convert each segment with the precomputed Start and Cont
1943 (def-ir1-translator tagbody ((&rest statements) start cont)
1945 "Tagbody {Tag | Statement}*
1946 Define tags for used with GO. The Statements are evaluated in order
1947 (skipping Tags) and NIL is returned. If a statement contains a GO to a
1948 defined Tag within the lexical scope of the form, then control is transferred
1949 to the next statement following that tag. A Tag must an integer or a
1950 symbol. A statement must be a list. Other objects are illegal within the
1952 (continuation-starts-block cont)
1953 (let* ((dummy (make-continuation))
1954 (entry (make-entry))
1955 (segments (parse-tagbody statements))
1956 (cleanup (make-cleanup :kind :tagbody
1958 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
1959 (setf (entry-cleanup entry) cleanup)
1960 (prev-link entry start)
1961 (use-continuation entry dummy)
1967 (dolist (segment (rest segments))
1968 (let* ((tag-cont (make-continuation))
1969 (tag (list (car segment) entry tag-cont)))
1972 (continuation-starts-block tag-cont)
1974 (push (cdr tag) (continuation-lexenv-uses tag-cont))))
1977 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
1978 (mapc (lambda (segment start cont)
1979 (ir1-convert-progn-body start cont (rest segment)))
1980 segments (starts) (conts))))))
1982 ;;; Emit an EXIT node without any value.
1983 (def-ir1-translator go ((tag) start cont)
1986 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
1987 is constrained to be used only within the dynamic extent of the TAGBODY."
1988 (continuation-starts-block cont)
1989 (let* ((found (or (lexenv-find tag tags :test #'eql)
1990 (compiler-error "Go to nonexistent tag: ~S." tag)))
1991 (entry (first found))
1992 (exit (make-exit :entry entry)))
1993 (push exit (entry-exits entry))
1994 (prev-link exit start)
1995 (use-continuation exit (second found))))
1997 ;;;; translators for compiler-magic special forms
1999 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in
2000 ;;; top-level forms are picked off and handled by PROCESS-TOP-LEVEL-FORM,
2001 ;;; so that they're never seen at this level.)
2003 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
2004 ;;; of non-top-level EVAL-WHENs is very simple:
2005 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
2006 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
2007 ;;; are ignored for non-top-level forms. For non-top-level forms, an
2008 ;;; eval-when specifying the :EXECUTE situation is treated as an
2009 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
2010 ;;; form; otherwise, the forms in the body are ignored.
2011 (def-ir1-translator eval-when ((situations &rest forms) start cont)
2013 "EVAL-WHEN (Situation*) Form*
2014 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
2015 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
2016 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
2017 (declare (ignore ct lt))
2018 (ir1-convert-progn-body start cont (and e forms)))
2021 ;;; common logic for MACROLET and SYMBOL-MACROLET
2023 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
2024 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
2025 ;;; call FUN (with no arguments).
2026 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
2027 definitionize-keyword
2030 (declare (type function definitionize-fun fun))
2031 (declare (type (member :variables :functions) definitionize-keyword))
2032 (declare (type list definitions))
2033 (unless (= (length definitions)
2034 (length (remove-duplicates definitions :key #'first)))
2035 (compiler-style-warning "duplicate definitions in ~S" definitions))
2036 (let* ((processed-definitions (mapcar definitionize-fun definitions))
2037 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
2040 ;;; Tweak *LEXENV* to include the DEFINITIONS from a MACROLET, then
2041 ;;; call FUN (with no arguments).
2043 ;;; This is split off from the IR1 convert method so that it can be
2044 ;;; shared by the special-case top-level MACROLET processing code.
2045 (defun funcall-in-macrolet-lexenv (definitions fun)
2046 (%funcall-in-foomacrolet-lexenv
2047 (lambda (definition)
2048 (unless (list-of-length-at-least-p definition 2)
2050 "The list ~S is too short to be a legal local macro definition."
2052 (destructuring-bind (name arglist &body body) definition
2053 (unless (symbolp name)
2054 (compiler-error "The local macro name ~S is not a symbol." name))
2055 (let ((whole (gensym "WHOLE"))
2056 (environment (gensym "ENVIRONMENT")))
2057 (multiple-value-bind (body local-decls)
2058 (parse-defmacro arglist whole body name 'macrolet
2059 :environment environment)
2062 `(lambda (,whole ,environment)
2064 (block ,name ,body))))))))
2069 (def-ir1-translator macrolet ((definitions &rest body) start cont)
2071 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
2072 Evaluate the Body-Forms in an environment with the specified local macros
2073 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
2074 destructuring lambda list, and the Forms evaluate to the expansion. The
2075 Forms are evaluated in the null environment."
2076 (funcall-in-macrolet-lexenv definitions
2078 (ir1-translate-locally body start cont))))
2080 (defun funcall-in-symbol-macrolet-lexenv (definitions fun)
2081 (%funcall-in-foomacrolet-lexenv
2082 (lambda (definition)
2083 (unless (proper-list-of-length-p definition 2)
2084 (compiler-error "malformed symbol/expansion pair: ~S" definition))
2085 (destructuring-bind (name expansion) definition
2086 (unless (symbolp name)
2088 "The local symbol macro name ~S is not a symbol."
2090 `(,name . (MACRO . ,expansion))))
2095 (def-ir1-translator symbol-macrolet ((macrobindings &body body) start cont)
2097 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
2098 Define the Names as symbol macros with the given Expansions. Within the
2099 body, references to a Name will effectively be replaced with the Expansion."
2100 (funcall-in-symbol-macrolet-lexenv
2103 (ir1-translate-locally body start cont))))
2105 ;;; not really a special form, but..
2106 (def-ir1-translator declare ((&rest stuff) start cont)
2107 (declare (ignore stuff))
2108 ;; We ignore START and CONT too, but we can't use DECLARE IGNORE to
2109 ;; tell the compiler about it here, because the DEF-IR1-TRANSLATOR
2110 ;; macro would put the DECLARE in the wrong place, so..
2112 (compiler-error "misplaced declaration"))
2116 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
2117 ;;;; into a funny function.
2119 ;;; Carefully evaluate a list of forms, returning a list of the results.
2120 (defun eval-info-args (args)
2121 (declare (list args))
2122 (handler-case (mapcar #'eval args)
2124 (compiler-error "Lisp error during evaluation of info args:~%~A"
2127 ;;; If there is a primitive translator, then we expand the call.
2128 ;;; Otherwise, we convert to the %%PRIMITIVE funny function. The first
2129 ;;; argument is the template, the second is a list of the results of
2130 ;;; any codegen-info args, and the remaining arguments are the runtime
2133 ;;; We do a bunch of error checking now so that we don't bomb out with
2134 ;;; a fatal error during IR2 conversion.
2136 ;;; KLUDGE: It's confusing having multiple names floating around for
2137 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
2138 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
2139 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
2140 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
2141 ;;; VOP or %VOP.. -- WHN 2001-06-11
2142 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
2143 (def-ir1-translator %primitive ((name &rest args) start cont)
2144 (unless (symbolp name)
2145 (compiler-error "The primitive name ~S is not a symbol." name))
2147 (let* ((template (or (gethash name *backend-template-names*)
2149 "The primitive name ~A is not defined."
2151 (required (length (template-arg-types template)))
2152 (info (template-info-arg-count template))
2153 (min (+ required info))
2154 (nargs (length args)))
2155 (if (template-more-args-type template)
2157 (compiler-error "Primitive ~A was called with ~R argument~:P, ~
2158 but wants at least ~R."
2162 (unless (= nargs min)
2163 (compiler-error "Primitive ~A was called with ~R argument~:P, ~
2164 but wants exactly ~R."
2169 (when (eq (template-result-types template) :conditional)
2171 "%PRIMITIVE was used with a conditional template."))
2173 (when (template-more-results-type template)
2175 "%PRIMITIVE was used with an unknown values template."))
2179 `(%%primitive ',template
2181 (subseq args required min))
2182 ,@(subseq args 0 required)
2183 ,@(subseq args min)))))
2185 ;;;; QUOTE and FUNCTION
2187 (def-ir1-translator quote ((thing) start cont)
2190 Return Value without evaluating it."
2191 (reference-constant start cont thing))
2193 (def-ir1-translator function ((thing) start cont)
2196 Return the lexically apparent definition of the function Name. Name may also
2201 (reference-leaf start cont (ir1-convert-lambda thing)))
2203 (let ((var (find-lexically-apparent-function
2204 thing "as the argument to FUNCTION")))
2205 (reference-leaf start cont var)))
2207 (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing)))))
2208 (setf (getf (functional-plist res) :fin-function) t)
2209 (reference-leaf start cont res)))
2211 (compiler-error "~S is not a legal function name." thing)))
2212 (let ((var (find-lexically-apparent-function
2213 thing "as the argument to FUNCTION")))
2214 (reference-leaf start cont var))))
2218 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
2219 ;;; (not symbols). %FUNCALL is used directly in some places where the
2220 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
2221 (deftransform funcall ((function &rest args) * * :when :both)
2222 (let ((arg-names (make-gensym-list (length args))))
2223 `(lambda (function ,@arg-names)
2224 (%funcall ,(if (csubtypep (continuation-type function)
2225 (specifier-type 'function))
2227 '(%coerce-callable-to-function function))
2230 (def-ir1-translator %funcall ((function &rest args) start cont)
2231 (let ((fun-cont (make-continuation)))
2232 (ir1-convert start fun-cont function)
2233 (assert-continuation-type fun-cont (specifier-type 'function))
2234 (ir1-convert-combination-args fun-cont cont args)))
2236 ;;; This source transform exists to reduce the amount of work for the
2237 ;;; compiler. If the called function is a FUNCTION form, then convert
2238 ;;; directly to %FUNCALL, instead of waiting around for type
2240 (def-source-transform funcall (function &rest args)
2241 (if (and (consp function) (eq (car function) 'function))
2242 `(%funcall ,function ,@args)
2245 (deftransform %coerce-callable-to-function ((thing) (function) *
2248 "optimize away possible call to FDEFINITION at runtime"
2253 ;;;; (LET and LET* can't be implemented as macros due to the fact that
2254 ;;;; any pervasive declarations also affect the evaluation of the
2257 ;;; Given a list of binding specifiers in the style of Let, return:
2258 ;;; 1. The list of var structures for the variables bound.
2259 ;;; 2. The initial value form for each variable.
2261 ;;; The variable names are checked for legality and globally special
2262 ;;; variables are marked as such. Context is the name of the form, for
2263 ;;; error reporting purposes.
2264 (declaim (ftype (function (list symbol) (values list list list))
2265 extract-let-variables))
2266 (defun extract-let-variables (bindings context)
2270 (flet ((get-var (name)
2271 (varify-lambda-arg name
2272 (if (eq context 'let*)
2275 (dolist (spec bindings)
2277 (let ((var (get-var spec)))
2279 (names (cons spec var))
2282 (unless (proper-list-of-length-p spec 1 2)
2283 (compiler-error "The ~S binding spec ~S is malformed."
2286 (let* ((name (first spec))
2287 (var (get-var name)))
2290 (vals (second spec)))))))
2292 (values (vars) (vals) (names))))
2294 (def-ir1-translator let ((bindings &body body)
2297 "LET ({(Var [Value]) | Var}*) Declaration* Form*
2298 During evaluation of the Forms, bind the Vars to the result of evaluating the
2299 Value forms. The variables are bound in parallel after all of the Values are
2301 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2302 (multiple-value-bind (vars values) (extract-let-variables bindings 'let)
2303 (let* ((*lexenv* (process-decls decls vars nil cont))
2304 (fun-cont (make-continuation))
2305 (fun (ir1-convert-lambda-body forms vars)))
2306 (reference-leaf start fun-cont fun)
2307 (ir1-convert-combination-args fun-cont cont values)))))
2309 (def-ir1-translator let* ((bindings &body body)
2312 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
2313 Similar to LET, but the variables are bound sequentially, allowing each Value
2314 form to reference any of the previous Vars."
2315 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2316 (multiple-value-bind (vars values) (extract-let-variables bindings 'let*)
2317 (let ((*lexenv* (process-decls decls vars nil cont)))
2318 (ir1-convert-aux-bindings start cont forms vars values)))))
2320 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
2321 ;;; and SYMBOL-MACROLET
2323 ;;; Note that all these things need to preserve top-level-formness,
2324 ;;; but we don't need to worry about that within an IR1 translator,
2325 ;;; since top-level-formness is picked off by PROCESS-TOP-LEVEL-FOO
2326 ;;; forms before we hit the IR1 transform level.
2327 (defun ir1-translate-locally (body start cont)
2328 (declare (type list body) (type continuation start cont))
2329 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2330 (let ((*lexenv* (process-decls decls nil nil cont)))
2331 (ir1-convert-aux-bindings start cont forms nil nil))))
2333 (def-ir1-translator locally ((&body body) start cont)
2335 "LOCALLY Declaration* Form*
2336 Sequentially evaluate the Forms in a lexical environment where the
2337 the Declarations have effect. If LOCALLY is a top-level form, then
2338 the Forms are also processed as top-level forms."
2339 (ir1-translate-locally body start cont))
2341 ;;;; FLET and LABELS
2343 ;;; Given a list of local function specifications in the style of
2344 ;;; FLET, return lists of the function names and of the lambdas which
2345 ;;; are their definitions.
2347 ;;; The function names are checked for legality. CONTEXT is the name
2348 ;;; of the form, for error reporting.
2349 (declaim (ftype (function (list symbol) (values list list))
2350 extract-flet-variables))
2351 (defun extract-flet-variables (definitions context)
2354 (dolist (def definitions)
2355 (when (or (atom def) (< (length def) 2))
2356 (compiler-error "The ~S definition spec ~S is malformed." context def))
2358 (let ((name (check-function-name (first def))))
2360 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr def))
2361 (defs `(lambda ,(second def)
2363 (block ,(function-name-block-name name)
2365 (values (names) (defs))))
2367 (def-ir1-translator flet ((definitions &body body)
2370 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
2371 Evaluate the Body-Forms with some local function definitions. The bindings
2372 do not enclose the definitions; any use of Name in the Forms will refer to
2373 the lexically apparent function definition in the enclosing environment."
2374 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2375 (multiple-value-bind (names defs)
2376 (extract-flet-variables definitions 'flet)
2377 (let* ((fvars (mapcar (lambda (n d)
2378 (ir1-convert-lambda d n))
2380 (*lexenv* (make-lexenv
2381 :default (process-decls decls nil fvars cont)
2382 :functions (pairlis names fvars))))
2383 (ir1-convert-progn-body start cont forms)))))
2385 ;;; For LABELS, we have to create dummy function vars and add them to
2386 ;;; the function namespace while converting the functions. We then
2387 ;;; modify all the references to these leaves so that they point to
2388 ;;; the real functional leaves. We also backpatch the FENV so that if
2389 ;;; the lexical environment is used for inline expansion we will get
2390 ;;; the right functions.
2391 (def-ir1-translator labels ((definitions &body body) start cont)
2393 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
2394 Evaluate the Body-Forms with some local function definitions. The bindings
2395 enclose the new definitions, so the defined functions can call themselves or
2397 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2398 (multiple-value-bind (names defs)
2399 (extract-flet-variables definitions 'labels)
2400 (let* ((new-fenv (loop for name in names
2401 collect (cons name (make-functional :name name))))
2403 (let ((*lexenv* (make-lexenv :functions new-fenv)))
2404 (mapcar (lambda (n d)
2405 (ir1-convert-lambda d n))
2408 (loop for real in real-funs and env in new-fenv do
2409 (let ((dum (cdr env)))
2410 (substitute-leaf real dum)
2411 (setf (cdr env) real)))
2413 (let ((*lexenv* (make-lexenv
2414 :default (process-decls decls nil real-funs cont)
2415 :functions (pairlis names real-funs))))
2416 (ir1-convert-progn-body start cont forms))))))
2420 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
2421 ;;; continuation that the assertion applies to, TYPE is the type
2422 ;;; specifier and Lexenv is the current lexical environment. NAME is
2423 ;;; the name of the declaration we are doing, for use in error
2426 ;;; This is somewhat involved, since a type assertion may only be made
2427 ;;; on a continuation, not on a node. We can't just set the
2428 ;;; continuation asserted type and let it go at that, since there may
2429 ;;; be parallel THE's for the same continuation, i.e.:
2434 ;;; In this case, our representation can do no better than the union
2435 ;;; of these assertions. And if there is a branch with no assertion,
2436 ;;; we have nothing at all. We really need to recognize scoping, since
2437 ;;; we need to be able to discern between parallel assertions (which
2438 ;;; we union) and nested ones (which we intersect).
2440 ;;; We represent the scoping by throwing our innermost (intersected)
2441 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
2442 ;;; intersect our assertions together. If CONT has no uses yet, we
2443 ;;; have not yet bottomed out on the first COND branch; in this case
2444 ;;; we optimistically assume that this type will be the one we end up
2445 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
2446 ;;; than the type that we have the first time we bottom out. Later
2447 ;;; THE's (or the absence thereof) can only weaken this result.
2449 ;;; We make this work by getting USE-CONTINUATION to do the unioning
2450 ;;; across COND branches. We can't do it here, since we don't know how
2451 ;;; many branches there are going to be.
2452 (defun do-the-stuff (type cont lexenv name)
2453 (declare (type continuation cont) (type lexenv lexenv))
2454 (let* ((ctype (values-specifier-type type))
2455 (old-type (or (lexenv-find cont type-restrictions)
2457 (intersects (values-types-equal-or-intersect old-type ctype))
2458 (int (values-type-intersection old-type ctype))
2459 (new (if intersects int old-type)))
2460 (when (null (find-uses cont))
2461 (setf (continuation-asserted-type cont) new))
2462 (when (and (not intersects)
2463 (not (policy *lexenv*
2464 (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
2466 "The type ~S in ~S declaration conflicts with an enclosing assertion:~% ~S"
2467 (type-specifier ctype)
2469 (type-specifier old-type)))
2470 (make-lexenv :type-restrictions `((,cont . ,new))
2473 ;;; Assert that FORM evaluates to the specified type (which may be a
2476 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
2477 ;;; this didn't seem to expand into an assertion, at least for ALIEN
2478 ;;; values. Check that SBCL doesn't have this problem.
2479 (def-ir1-translator the ((type value) start cont)
2480 (let ((*lexenv* (do-the-stuff type cont *lexenv* 'the)))
2481 (ir1-convert start cont value)))
2483 ;;; This is like the THE special form, except that it believes
2484 ;;; whatever you tell it. It will never generate a type check, but
2485 ;;; will cause a warning if the compiler can prove the assertion is
2488 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
2489 ;;; its uses's types, setting it won't work. Instead we must intersect
2490 ;;; the type with the uses's DERIVED-TYPE.
2491 (def-ir1-translator truly-the ((type value) start cont)
2493 (declare (inline member))
2494 (let ((type (values-specifier-type type))
2495 (old (find-uses cont)))
2496 (ir1-convert start cont value)
2498 (unless (member use old :test #'eq)
2499 (derive-node-type use type)))))
2503 ;;; If there is a definition in LEXENV-VARIABLES, just set that,
2504 ;;; otherwise look at the global information. If the name is for a
2505 ;;; constant, then error out.
2506 (def-ir1-translator setq ((&whole source &rest things) start cont)
2507 (let ((len (length things)))
2509 (compiler-error "odd number of args to SETQ: ~S" source))
2511 (let* ((name (first things))
2512 (leaf (or (lexenv-find name variables)
2513 (find-free-variable name))))
2516 (when (or (constant-p leaf)
2517 (and (global-var-p leaf)
2518 (eq (global-var-kind leaf) :constant)))
2519 (compiler-error "~S is a constant and thus can't be set." name))
2520 (when (and (lambda-var-p leaf)
2521 (lambda-var-ignorep leaf))
2522 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
2523 ;; requires that this be a STYLE-WARNING, not a full warning.
2524 (compiler-style-warning
2525 "~S is being set even though it was declared to be ignored."
2527 (set-variable start cont leaf (second things)))
2529 (aver (eq (car leaf) 'MACRO))
2530 (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
2532 (ir1-convert start cont
2533 `(%set-heap-alien ',leaf ,(second things))))))
2535 (do ((thing things (cddr thing)))
2537 (ir1-convert-progn-body start cont (sets)))
2538 (sets `(setq ,(first thing) ,(second thing))))))))
2540 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
2541 ;;; This should only need to be called in SETQ.
2542 (defun set-variable (start cont var value)
2543 (declare (type continuation start cont) (type basic-var var))
2544 (let ((dest (make-continuation)))
2545 (setf (continuation-asserted-type dest) (leaf-type var))
2546 (ir1-convert start dest value)
2547 (let ((res (make-set :var var :value dest)))
2548 (setf (continuation-dest dest) res)
2549 (setf (leaf-ever-used var) t)
2550 (push res (basic-var-sets var))
2551 (prev-link res dest)
2552 (use-continuation res cont))))
2554 ;;;; CATCH, THROW and UNWIND-PROTECT
2556 ;;; We turn THROW into a multiple-value-call of a magical function,
2557 ;;; since as as far as IR1 is concerned, it has no interesting
2558 ;;; properties other than receiving multiple-values.
2559 (def-ir1-translator throw ((tag result) start cont)
2562 Do a non-local exit, return the values of Form from the CATCH whose tag
2563 evaluates to the same thing as Tag."
2564 (ir1-convert start cont
2565 `(multiple-value-call #'%throw ,tag ,result)))
2567 ;;; This is a special special form used to instantiate a cleanup as
2568 ;;; the current cleanup within the body. KIND is a the kind of cleanup
2569 ;;; to make, and MESS-UP is a form that does the mess-up action. We
2570 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
2571 ;;; and introduce the cleanup into the lexical environment. We
2572 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
2573 ;;; cleanup, since this inner cleanup is the interesting one.
2574 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
2575 (let ((dummy (make-continuation))
2576 (dummy2 (make-continuation)))
2577 (ir1-convert start dummy mess-up)
2578 (let* ((mess-node (continuation-use dummy))
2579 (cleanup (make-cleanup :kind kind
2580 :mess-up mess-node))
2581 (old-cup (lexenv-cleanup *lexenv*))
2582 (*lexenv* (make-lexenv :cleanup cleanup)))
2583 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
2584 (ir1-convert dummy dummy2 '(%cleanup-point))
2585 (ir1-convert-progn-body dummy2 cont body))))
2587 ;;; This is a special special form that makes an "escape function"
2588 ;;; which returns unknown values from named block. We convert the
2589 ;;; function, set its kind to :ESCAPE, and then reference it. The
2590 ;;; :Escape kind indicates that this function's purpose is to
2591 ;;; represent a non-local control transfer, and that it might not
2592 ;;; actually have to be compiled.
2594 ;;; Note that environment analysis replaces references to escape
2595 ;;; functions with references to the corresponding NLX-INFO structure.
2596 (def-ir1-translator %escape-function ((tag) start cont)
2597 (let ((fun (ir1-convert-lambda
2599 (return-from ,tag (%unknown-values))))))
2600 (setf (functional-kind fun) :escape)
2601 (reference-leaf start cont fun)))
2603 ;;; Yet another special special form. This one looks up a local
2604 ;;; function and smashes it to a :CLEANUP function, as well as
2606 (def-ir1-translator %cleanup-function ((name) start cont)
2607 (let ((fun (lexenv-find name functions)))
2608 (aver (lambda-p fun))
2609 (setf (functional-kind fun) :cleanup)
2610 (reference-leaf start cont fun)))
2612 ;;; We represent the possibility of the control transfer by making an
2613 ;;; "escape function" that does a lexical exit, and instantiate the
2614 ;;; cleanup using %WITHIN-CLEANUP.
2615 (def-ir1-translator catch ((tag &body body) start cont)
2618 Evaluates Tag and instantiates it as a catcher while the body forms are
2619 evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
2620 scope of the body, then control will be transferred to the end of the body
2621 and the thrown values will be returned."
2624 (let ((exit-block (gensym "EXIT-BLOCK-")))
2628 (%catch (%escape-function ,exit-block) ,tag)
2631 ;;; UNWIND-PROTECT is similar to CATCH, but more hairy. We make the
2632 ;;; cleanup forms into a local function so that they can be referenced
2633 ;;; both in the case where we are unwound and in any local exits. We
2634 ;;; use %CLEANUP-FUNCTION on this to indicate that reference by
2635 ;;; %UNWIND-PROTECT ISN'T "real", and thus doesn't cause creation of
2637 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
2639 "Unwind-Protect Protected Cleanup*
2640 Evaluate the form Protected, returning its values. The cleanup forms are
2641 evaluated whenever the dynamic scope of the Protected form is exited (either
2642 due to normal completion or a non-local exit such as THROW)."
2645 (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
2646 (drop-thru-tag (gensym "DROP-THRU-TAG-"))
2647 (exit-tag (gensym "EXIT-TAG-"))
2648 (next (gensym "NEXT"))
2649 (start (gensym "START"))
2650 (count (gensym "COUNT")))
2651 `(flet ((,cleanup-fun () ,@cleanup nil))
2652 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
2653 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
2654 ;; and something can be done to make %ESCAPE-FUNCTION have
2655 ;; dynamic extent too.
2656 (block ,drop-thru-tag
2657 (multiple-value-bind (,next ,start ,count)
2661 (%unwind-protect (%escape-function ,exit-tag)
2662 (%cleanup-function ,cleanup-fun))
2663 (return-from ,drop-thru-tag ,protected)))
2665 (%continue-unwind ,next ,start ,count)))))))
2667 ;;;; multiple-value stuff
2669 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
2672 ;;; If there are no arguments, then we convert to a normal
2673 ;;; combination, ensuring that a MV-COMBINATION always has at least
2674 ;;; one argument. This can be regarded as an optimization, but it is
2675 ;;; more important for simplifying compilation of MV-COMBINATIONS.
2676 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
2678 "MULTIPLE-VALUE-CALL Function Values-Form*
2679 Call Function, passing all the values of each Values-Form as arguments,
2680 values from the first Values-Form making up the first argument, etc."
2681 (let* ((fun-cont (make-continuation))
2683 (make-mv-combination fun-cont)
2684 (make-combination fun-cont))))
2685 (ir1-convert start fun-cont
2686 (if (and (consp fun) (eq (car fun) 'function))
2688 `(%coerce-callable-to-function ,fun)))
2689 (setf (continuation-dest fun-cont) node)
2690 (assert-continuation-type fun-cont
2691 (specifier-type '(or function symbol)))
2692 (collect ((arg-conts))
2693 (let ((this-start fun-cont))
2695 (let ((this-cont (make-continuation node)))
2696 (ir1-convert this-start this-cont arg)
2697 (setq this-start this-cont)
2698 (arg-conts this-cont)))
2699 (prev-link node this-start)
2700 (use-continuation node cont)
2701 (setf (basic-combination-args node) (arg-conts))))))
2703 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
2704 ;;; the result code use result continuation (CONT), but transfer
2705 ;;; control to the evaluation of the body. In other words, the result
2706 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
2709 ;;; In order to get the control flow right, we convert the result with
2710 ;;; a dummy result continuation, then convert all the uses of the
2711 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
2712 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
2713 ;;; that they are consistent. Note that this doesn't amount to
2714 ;;; changing the exit target, since the control destination of an exit
2715 ;;; is determined by the block successor; we are just indicating the
2716 ;;; continuation that the result is delivered to.
2718 ;;; We then convert the body, using another dummy continuation in its
2719 ;;; own block as the result. After we are done converting the body, we
2720 ;;; move all predecessors of the dummy end block to CONT's block.
2722 ;;; Note that we both exploit and maintain the invariant that the CONT
2723 ;;; to an IR1 convert method either has no block or starts the block
2724 ;;; that control should transfer to after completion for the form.
2725 ;;; Nested MV-PROG1's work because during conversion of the result
2726 ;;; form, we use dummy continuation whose block is the true control
2728 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
2730 "MULTIPLE-VALUE-PROG1 Values-Form Form*
2731 Evaluate Values-Form and then the Forms, but return all the values of
2733 (continuation-starts-block cont)
2734 (let* ((dummy-result (make-continuation))
2735 (dummy-start (make-continuation))
2736 (cont-block (continuation-block cont)))
2737 (continuation-starts-block dummy-start)
2738 (ir1-convert start dummy-start result)
2740 (substitute-continuation-uses cont dummy-start)
2742 (continuation-starts-block dummy-result)
2743 (ir1-convert-progn-body dummy-start dummy-result forms)
2744 (let ((end-block (continuation-block dummy-result)))
2745 (dolist (pred (block-pred end-block))
2746 (unlink-blocks pred end-block)
2747 (link-blocks pred cont-block))
2748 (aver (not (continuation-dest dummy-result)))
2749 (delete-continuation dummy-result)
2750 (remove-from-dfo end-block))))
2752 ;;;; interface to defining macros
2755 ;;;; classic CMU CL comment:
2756 ;;;; DEFMACRO and DEFUN expand into calls to %DEFxxx functions
2757 ;;;; so that we get a chance to see what is going on. We define
2758 ;;;; IR1 translators for these functions which look at the
2759 ;;;; definition and then generate a call to the %%DEFxxx function.
2760 ;;;; Alas, this implementation doesn't do the right thing for
2761 ;;;; non-toplevel uses of these forms, so this should probably
2762 ;;;; be changed to use EVAL-WHEN instead.
2764 ;;; Return a new source path with any stuff intervening between the
2765 ;;; current path and the first form beginning with NAME stripped off.
2766 ;;; This is used to hide the guts of DEFmumble macros to prevent
2767 ;;; annoying error messages.
2768 (defun revert-source-path (name)
2769 (do ((path *current-path* (cdr path)))
2770 ((null path) *current-path*)
2771 (let ((first (first path)))
2772 (when (or (eq first name)
2773 (eq first 'original-source-start))
2776 ;;; Warn about incompatible or illegal definitions and add the macro
2777 ;;; to the compiler environment.
2779 ;;; Someday we could check for macro arguments being incompatibly
2780 ;;; redefined. Doing this right will involve finding the old macro
2781 ;;; lambda-list and comparing it with the new one.
2782 (def-ir1-translator %defmacro ((qname qdef lambda-list doc) start cont
2784 (let (;; QNAME is typically a quoted name. I think the idea is to
2785 ;; let %DEFMACRO work as an ordinary function when
2786 ;; interpreting. Whatever the reason the quote is there, we
2787 ;; don't want it any more. -- WHN 19990603
2789 ;; QDEF should be a sharp-quoted definition. We don't want to
2790 ;; make a function of it just yet, so we just drop the
2793 (aver (eq 'function (first qdef)))
2794 (aver (proper-list-of-length-p qdef 2))
2797 (/show "doing IR1 translator for %DEFMACRO" name)
2799 (unless (symbolp name)
2800 (compiler-error "The macro name ~S is not a symbol." name))
2802 (ecase (info :function :kind name)
2805 (remhash name *free-functions*)
2806 (undefine-function-name name)
2808 "~S is being redefined as a macro when it was ~
2809 previously ~(~A~) to be a function."
2811 (info :function :where-from name)))
2814 (compiler-error "The special form ~S can't be redefined as a macro."
2817 (setf (info :function :kind name) :macro
2818 (info :function :where-from name) :defined
2819 (info :function :macro-function name) (coerce def 'function))
2821 (let* ((*current-path* (revert-source-path 'defmacro))
2822 (fun (ir1-convert-lambda def name)))
2823 (setf (leaf-name fun)
2824 (concatenate 'string "DEFMACRO " (symbol-name name)))
2825 (setf (functional-arg-documentation fun) (eval lambda-list))
2827 (ir1-convert start cont `(%%defmacro ',name ,fun ,doc)))
2829 (when sb!xc:*compile-print*
2830 ;; FIXME: It would be nice to convert this, and the other places
2831 ;; which create compiler diagnostic output prefixed by
2832 ;; semicolons, to use some common utility which automatically
2833 ;; prefixes all its output with semicolons. (The addition of
2834 ;; semicolon prefixes was introduced ca. sbcl-0.6.8.10 as the
2835 ;; "MNA compiler message patch", and implemented by modifying a
2836 ;; bunch of output statements on a case-by-case basis, which
2837 ;; seems unnecessarily error-prone and unclear, scattering
2838 ;; implicit information about output style throughout the
2839 ;; system.) Starting by rewriting COMPILER-MUMBLE to add
2840 ;; semicolon prefixes would be a good start, and perhaps also:
2841 ;; * Add semicolon prefixes for "FOO assembled" messages emitted
2842 ;; when e.g. src/assembly/x86/assem-rtns.lisp is processed.
2843 ;; * At least some debugger output messages deserve semicolon
2845 ;; ** restarts table
2846 ;; ** "Within the debugger, you can type HELP for help."
2847 (compiler-mumble "~&; converted ~S~%" name))))
2849 (def-ir1-translator %define-compiler-macro ((name def lambda-list doc)
2852 (let ((name (eval name))
2853 (def (second def))) ; We don't want to make a function just yet...
2855 (when (eq (info :function :kind name) :special-form)
2856 (compiler-error "attempt to define a compiler-macro for special form ~S"
2859 (setf (info :function :compiler-macro-function name)
2860 (coerce def 'function))
2862 (let* ((*current-path* (revert-source-path 'define-compiler-macro))
2863 (fun (ir1-convert-lambda def name)))
2864 (setf (leaf-name fun)
2865 (let ((*print-case* :upcase))
2866 (format nil "DEFINE-COMPILER-MACRO ~S" name)))
2867 (setf (functional-arg-documentation fun) (eval lambda-list))
2869 (ir1-convert start cont `(%%define-compiler-macro ',name ,fun ,doc)))
2871 (when sb!xc:*compile-print*
2872 (compiler-mumble "~&; converted ~S~%" name))))
2874 ;;;; defining global functions
2876 ;;; Convert FUN as a lambda in the null environment, but use the
2877 ;;; current compilation policy. Note that FUN may be a
2878 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
2879 ;;; reflect the state at the definition site.
2880 (defun ir1-convert-inline-lambda (fun &optional name)
2881 (destructuring-bind (decls macros symbol-macros &rest body)
2882 (if (eq (car fun) 'lambda-with-lexenv)
2884 `(() () () . ,(cdr fun)))
2885 (let ((*lexenv* (make-lexenv
2886 :default (process-decls decls nil nil
2889 :variables (copy-list symbol-macros)
2893 (macro . ,(coerce (cdr x) 'function))))
2895 :policy (lexenv-policy *lexenv*))))
2896 (ir1-convert-lambda `(lambda ,@body) name))))
2898 ;;; Get a DEFINED-FUNCTION object for a function we are about to
2899 ;;; define. If the function has been forward referenced, then
2900 ;;; substitute for the previous references.
2901 (defun get-defined-function (name)
2902 (let* ((name (proclaim-as-function-name name))
2903 (found (find-free-function name "Eh?")))
2904 (note-name-defined name :function)
2905 (cond ((not (defined-function-p found))
2906 (aver (not (info :function :inlinep name)))
2907 (let* ((where-from (leaf-where-from found))
2908 (res (make-defined-function
2910 :where-from (if (eq where-from :declared)
2912 :type (leaf-type found))))
2913 (substitute-leaf res found)
2914 (setf (gethash name *free-functions*) res)))
2915 ;; If *FREE-FUNCTIONS* has a previously converted definition for this
2916 ;; name, then blow it away and try again.
2917 ((defined-function-functional found)
2918 (remhash name *free-functions*)
2919 (get-defined-function name))
2922 ;;; Check a new global function definition for consistency with
2923 ;;; previous declaration or definition, and assert argument/result
2924 ;;; types if appropriate. This assertion is suppressed by the
2925 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
2926 ;;; check their argument types as a consequence of type dispatching.
2927 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
2928 (defun assert-new-definition (var fun)
2929 (let ((type (leaf-type var))
2930 (for-real (eq (leaf-where-from var) :declared))
2931 (info (info :function :info (leaf-name var))))
2932 (assert-definition-type
2934 ;; KLUDGE: Common Lisp is such a dynamic language that in general
2935 ;; all we can do here in general is issue a STYLE-WARNING. It
2936 ;; would be nice to issue a full WARNING in the special case of
2937 ;; of type mismatches within a compilation unit (as in section
2938 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
2939 ;; keep track of whether the mismatched data came from the same
2940 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
2941 :error-function #'compiler-style-warning
2942 :warning-function (cond (info #'compiler-style-warning)
2943 (for-real #'compiler-note)
2948 (ir1-attributep (function-info-attributes info)
2951 "previous declaration"
2952 "previous definition"))))
2954 ;;; Convert a lambda doing all the basic stuff we would do if we were
2955 ;;; converting a DEFUN. This is used both by the %DEFUN translator and
2956 ;;; for global inline expansion.
2958 ;;; Unless a :INLINE function, we temporarily clobber the inline
2959 ;;; expansion. This prevents recursive inline expansion of
2960 ;;; opportunistic pseudo-inlines.
2961 (defun ir1-convert-lambda-for-defun (lambda var expansion converter)
2962 (declare (cons lambda) (function converter) (type defined-function var))
2963 (let ((var-expansion (defined-function-inline-expansion var)))
2964 (unless (eq (defined-function-inlinep var) :inline)
2965 (setf (defined-function-inline-expansion var) nil))
2966 (let* ((name (leaf-name var))
2967 (fun (funcall converter lambda name))
2968 (function-info (info :function :info name)))
2969 (setf (functional-inlinep fun) (defined-function-inlinep var))
2970 (assert-new-definition var fun)
2971 (setf (defined-function-inline-expansion var) var-expansion)
2972 ;; If definitely not an interpreter stub, then substitute for any
2974 (unless (or (eq (defined-function-inlinep var) :notinline)
2975 (not *block-compile*)
2977 (or (function-info-transforms function-info)
2978 (function-info-templates function-info)
2979 (function-info-ir2-convert function-info))))
2980 (substitute-leaf fun var)
2981 ;; If in a simple environment, then we can allow backward
2982 ;; references to this function from following top-level forms.
2983 (when expansion (setf (defined-function-functional var) fun)))
2986 ;;; the even-at-compile-time part of DEFUN
2988 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
2989 ;;; no inline expansion.
2990 (defun %compiler-defun (name lambda-with-lexenv)
2992 (let ((defined-function nil)) ; will be set below if we're in the compiler
2994 ;; when in the compiler
2995 (when (boundp '*lexenv*)
2996 (when sb!xc:*compile-print*
2997 (compiler-mumble "~&; recognizing DEFUN ~S~%" name))
2998 (remhash name *free-functions*)
2999 (setf defined-function (get-defined-function name)))
3001 (become-defined-function-name name)
3003 (cond (lambda-with-lexenv
3004 (setf (info :function :inline-expansion name) lambda-with-lexenv)
3005 (when defined-function
3006 (setf (defined-function-inline-expansion defined-function)
3007 lambda-with-lexenv)))
3009 (clear-info :function :inline-expansion name)))
3011 ;; old CMU CL comment:
3012 ;; If there is a type from a previous definition, blast it,
3013 ;; since it is obsolete.
3014 (when (and defined-function
3015 (eq (leaf-where-from defined-function) :defined))
3016 (setf (leaf-type defined-function)
3017 ;; FIXME: If this is a block compilation thing, shouldn't
3018 ;; we be setting the type to the full derived type for the
3019 ;; definition, instead of this most general function type?
3020 (specifier-type 'function))))
3024 ;;;; hacking function names
3026 ;;; This is like LAMBDA, except the result is tweaked so that
3027 ;;; %FUNCTION-NAME or BYTE-FUNCTION-NAME can extract a name. (Also
3028 ;;; possibly the name could also be used at compile time to emit
3029 ;;; more-informative name-based compiler diagnostic messages as well.)
3030 (defmacro-mundanely named-lambda (name args &body body)
3032 ;; FIXME: For now, in this stub version, we just discard the name. A
3033 ;; non-stub version might use either macro-level LOAD-TIME-VALUE
3034 ;; hackery or customized IR1-transform level magic to actually put
3035 ;; the name in place.
3036 (aver (legal-function-name-p name))
3037 `(lambda ,args ,@body))