1 ;;;; the usual place for DEF-IR1-TRANSLATOR forms (and their
2 ;;;; close personal friends)
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 ;;;; control special forms
17 (def-ir1-translator progn ((&rest forms) start cont)
20 Evaluates each Form in order, returning the values of the last form. With no
22 (ir1-convert-progn-body start cont forms))
24 (def-ir1-translator if ((test then &optional else) start cont)
26 "If Predicate Then [Else]
27 If Predicate evaluates to non-null, evaluate Then and returns its values,
28 otherwise evaluate Else and return its values. Else defaults to NIL."
29 (let* ((pred (make-continuation))
30 (then-cont (make-continuation))
31 (then-block (continuation-starts-block then-cont))
32 (else-cont (make-continuation))
33 (else-block (continuation-starts-block else-cont))
34 (dummy-cont (make-continuation))
35 (node (make-if :test pred
36 :consequent then-block
37 :alternative else-block)))
38 (setf (continuation-dest pred) node)
39 (ir1-convert start pred test)
41 (use-continuation node dummy-cont)
43 (let ((start-block (continuation-block pred)))
44 (setf (block-last start-block) node)
45 (continuation-starts-block cont)
47 (link-blocks start-block then-block)
48 (link-blocks start-block else-block))
50 (ir1-convert then-cont cont then)
51 (ir1-convert else-cont cont else)))
53 ;;;; BLOCK and TAGBODY
55 ;;;; We make an ENTRY node to mark the start and a :ENTRY cleanup to
56 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an EXIT
59 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
60 ;;; body in the modified environment. We make CONT start a block now,
61 ;;; since if it was done later, the block would be in the wrong
63 (def-ir1-translator block ((name &rest forms) start cont)
66 Evaluate the Forms as a PROGN. Within the lexical scope of the body,
67 (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
68 result of Value-Form."
69 (unless (symbolp name)
70 (compiler-error "The block name ~S is not a symbol." name))
71 (continuation-starts-block cont)
72 (let* ((dummy (make-continuation))
74 (cleanup (make-cleanup :kind :block
76 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
77 (setf (entry-cleanup entry) cleanup)
78 (prev-link entry start)
79 (use-continuation entry dummy)
81 (let* ((env-entry (list entry cont))
82 (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
84 (push env-entry (continuation-lexenv-uses cont))
85 (ir1-convert-progn-body dummy cont forms))))
88 (def-ir1-translator return-from ((name &optional value)
91 "Return-From Block-Name Value-Form
92 Evaluate the Value-Form, returning its values from the lexically enclosing
93 BLOCK Block-Name. This is constrained to be used only within the dynamic
96 ;; We make CONT start a block just so that it will have a block
97 ;; assigned. People assume that when they pass a continuation into
98 ;; IR1-CONVERT as CONT, it will have a block when it is done.
99 ;; KLUDGE: Note that this block is basically fictitious. In the code
100 ;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
101 ;; it's the block which answers the question "which block is
102 ;; the (SETQ X 3) in?" when the right answer is that (SETQ X 3) is
103 ;; dead code and so doesn't really have a block at all. The existence
104 ;; of this block, and that way that it doesn't explicitly say
105 ;; "I'm actually nowhere at all" makes some logic (e.g.
106 ;; BLOCK-HOME-LAMBDA-OR-NULL) more obscure, and it might be better
107 ;; to get rid of it, perhaps using a special placeholder value
108 ;; to indicate the orphanedness of the code.
109 (continuation-starts-block cont)
110 (let* ((found (or (lexenv-find name blocks)
111 (compiler-error "return for unknown block: ~S" name)))
112 (value-cont (make-continuation))
113 (entry (first found))
114 (exit (make-exit :entry entry
116 (push exit (entry-exits entry))
117 (setf (continuation-dest value-cont) exit)
118 (ir1-convert start value-cont value)
119 (prev-link exit value-cont)
120 (use-continuation exit (second found))))
122 ;;; Return a list of the segments of a TAGBODY. Each segment looks
123 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
124 ;;; tagbody into segments of non-tag statements, and explicitly
125 ;;; represent the drop-through with a GO. The first segment has a
126 ;;; dummy NIL tag, since it represents code before the first tag. The
127 ;;; last segment (which may also be the first segment) ends in NIL
128 ;;; rather than a GO.
129 (defun parse-tagbody (body)
130 (declare (list body))
131 (collect ((segments))
132 (let ((current (cons nil body)))
134 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
136 (segments `(,@current nil))
138 (let ((tag (elt current tag-pos)))
139 (when (assoc tag (segments))
141 "The tag ~S appears more than once in the tagbody."
143 (unless (or (symbolp tag) (integerp tag))
144 (compiler-error "~S is not a legal tagbody statement." tag))
145 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
146 (setq current (nthcdr tag-pos current)))))
149 ;;; Set up the cleanup, emitting the entry node. Then make a block for
150 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
151 ;;; Finally, convert each segment with the precomputed Start and Cont
153 (def-ir1-translator tagbody ((&rest statements) start cont)
155 "Tagbody {Tag | Statement}*
156 Define tags for used with GO. The Statements are evaluated in order
157 (skipping Tags) and NIL is returned. If a statement contains a GO to a
158 defined Tag within the lexical scope of the form, then control is transferred
159 to the next statement following that tag. A Tag must an integer or a
160 symbol. A statement must be a list. Other objects are illegal within the
162 (continuation-starts-block cont)
163 (let* ((dummy (make-continuation))
165 (segments (parse-tagbody statements))
166 (cleanup (make-cleanup :kind :tagbody
168 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
169 (setf (entry-cleanup entry) cleanup)
170 (prev-link entry start)
171 (use-continuation entry dummy)
177 (dolist (segment (rest segments))
178 (let* ((tag-cont (make-continuation))
179 (tag (list (car segment) entry tag-cont)))
182 (continuation-starts-block tag-cont)
184 (push (cdr tag) (continuation-lexenv-uses tag-cont))))
187 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
188 (mapc (lambda (segment start cont)
189 (ir1-convert-progn-body start cont (rest segment)))
190 segments (starts) (conts))))))
192 ;;; Emit an EXIT node without any value.
193 (def-ir1-translator go ((tag) start cont)
196 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
197 is constrained to be used only within the dynamic extent of the TAGBODY."
198 (continuation-starts-block cont)
199 (let* ((found (or (lexenv-find tag tags :test #'eql)
200 (compiler-error "Go to nonexistent tag: ~S." tag)))
201 (entry (first found))
202 (exit (make-exit :entry entry)))
203 (push exit (entry-exits entry))
204 (prev-link exit start)
205 (use-continuation exit (second found))))
207 ;;;; translators for compiler-magic special forms
209 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
210 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
211 ;;; so that they're never seen at this level.)
213 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
214 ;;; of non-top-level EVAL-WHENs is very simple:
215 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
216 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
217 ;;; are ignored for non-top-level forms. For non-top-level forms, an
218 ;;; eval-when specifying the :EXECUTE situation is treated as an
219 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
220 ;;; form; otherwise, the forms in the body are ignored.
221 (def-ir1-translator eval-when ((situations &rest forms) start cont)
223 "EVAL-WHEN (Situation*) Form*
224 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
225 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
226 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
227 (declare (ignore ct lt))
228 (ir1-convert-progn-body start cont (and e forms)))
231 ;;; common logic for MACROLET and SYMBOL-MACROLET
233 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
234 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
235 ;;; call FUN (with no arguments).
236 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
237 definitionize-keyword
240 (declare (type function definitionize-fun fun))
241 (declare (type (member :variables :functions) definitionize-keyword))
242 (declare (type list definitions))
243 (unless (= (length definitions)
244 (length (remove-duplicates definitions :key #'first)))
245 (compiler-style-warning "duplicate definitions in ~S" definitions))
246 (let* ((processed-definitions (mapcar definitionize-fun definitions))
247 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
250 ;;; Tweak *LEXENV* to include the DEFINITIONS from a MACROLET, then
251 ;;; call FUN (with no arguments).
253 ;;; This is split off from the IR1 convert method so that it can be
254 ;;; shared by the special-case top level MACROLET processing code.
255 (defun funcall-in-macrolet-lexenv (definitions fun)
256 (%funcall-in-foomacrolet-lexenv
258 (unless (list-of-length-at-least-p definition 2)
260 "The list ~S is too short to be a legal local macro definition."
262 (destructuring-bind (name arglist &body body) definition
263 (unless (symbolp name)
264 (compiler-error "The local macro name ~S is not a symbol." name))
265 (let ((whole (gensym "WHOLE"))
266 (environment (gensym "ENVIRONMENT")))
267 (multiple-value-bind (body local-decls)
268 (parse-defmacro arglist whole body name 'macrolet
269 :environment environment)
272 `(lambda (,whole ,environment)
274 (block ,name ,body))))))))
279 (def-ir1-translator macrolet ((definitions &rest body) start cont)
281 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
282 Evaluate the Body-Forms in an environment with the specified local macros
283 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
284 destructuring lambda list, and the Forms evaluate to the expansion. The
285 Forms are evaluated in the null environment."
286 (funcall-in-macrolet-lexenv definitions
288 (ir1-translate-locally body start cont))))
290 (defun funcall-in-symbol-macrolet-lexenv (definitions fun)
291 (%funcall-in-foomacrolet-lexenv
293 (unless (proper-list-of-length-p definition 2)
294 (compiler-error "malformed symbol/expansion pair: ~S" definition))
295 (destructuring-bind (name expansion) definition
296 (unless (symbolp name)
298 "The local symbol macro name ~S is not a symbol."
300 `(,name . (MACRO . ,expansion))))
305 (def-ir1-translator symbol-macrolet ((macrobindings &body body) start cont)
307 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
308 Define the Names as symbol macros with the given Expansions. Within the
309 body, references to a Name will effectively be replaced with the Expansion."
310 (funcall-in-symbol-macrolet-lexenv
313 (ir1-translate-locally body start cont))))
315 ;;; not really a special form, but..
316 (def-ir1-translator declare ((&rest stuff) start cont)
317 (declare (ignore stuff))
318 ;; We ignore START and CONT too, but we can't use DECLARE IGNORE to
319 ;; tell the compiler about it here, because the DEF-IR1-TRANSLATOR
320 ;; macro would put the DECLARE in the wrong place, so..
322 (compiler-error "misplaced declaration"))
326 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
327 ;;;; into a funny function.
329 ;;; Carefully evaluate a list of forms, returning a list of the results.
330 (defun eval-info-args (args)
331 (declare (list args))
332 (handler-case (mapcar #'eval args)
334 (compiler-error "Lisp error during evaluation of info args:~%~A"
337 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
338 ;;; the template, the second is a list of the results of any
339 ;;; codegen-info args, and the remaining arguments are the runtime
342 ;;; We do various error checking now so that we don't bomb out with
343 ;;; a fatal error during IR2 conversion.
345 ;;; KLUDGE: It's confusing having multiple names floating around for
346 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
347 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
348 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
349 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
350 ;;; VOP or %VOP.. -- WHN 2001-06-11
351 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
352 (def-ir1-translator %primitive ((name &rest args) start cont)
353 (unless (symbolp name)
354 (compiler-error "internal error: Primitive name ~S is not a symbol." name))
355 (let* ((template (or (gethash name *backend-template-names*)
357 "internal error: Primitive name ~A is not defined."
359 (required (length (template-arg-types template)))
360 (info (template-info-arg-count template))
361 (min (+ required info))
362 (nargs (length args)))
363 (if (template-more-args-type template)
365 (compiler-error "internal error: Primitive ~A was called ~
366 with ~R argument~:P, ~
367 but wants at least ~R."
371 (unless (= nargs min)
372 (compiler-error "internal error: Primitive ~A was called ~
373 with ~R argument~:P, ~
374 but wants exactly ~R."
379 (when (eq (template-result-types template) :conditional)
381 "%PRIMITIVE was used with a conditional template."))
383 (when (template-more-results-type template)
385 "%PRIMITIVE was used with an unknown values template."))
389 `(%%primitive ',template
391 (subseq args required min))
392 ,@(subseq args 0 required)
393 ,@(subseq args min)))))
395 ;;;; QUOTE and FUNCTION
397 (def-ir1-translator quote ((thing) start cont)
400 Return Value without evaluating it."
401 (reference-constant start cont thing))
403 (def-ir1-translator function ((thing) start cont)
406 Return the lexically apparent definition of the function Name. Name may also
411 (reference-leaf start
413 (ir1-convert-lambda thing
414 :debug-name (debug-namify
417 (let ((var (find-lexically-apparent-function
418 thing "as the argument to FUNCTION")))
419 (reference-leaf start cont var)))
421 (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing))
422 :debug-name (debug-namify "#'~S"
424 (setf (getf (functional-plist res) :fin-function) t)
425 (reference-leaf start cont res)))
427 (compiler-error "~S is not a legal function name." thing)))
428 (let ((var (find-lexically-apparent-function
429 thing "as the argument to FUNCTION")))
430 (reference-leaf start cont var))))
434 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
435 ;;; (not symbols). %FUNCALL is used directly in some places where the
436 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
437 (deftransform funcall ((function &rest args) * * :when :both)
438 (let ((arg-names (make-gensym-list (length args))))
439 `(lambda (function ,@arg-names)
440 (%funcall ,(if (csubtypep (continuation-type function)
441 (specifier-type 'function))
443 '(%coerce-callable-to-fun function))
446 (def-ir1-translator %funcall ((function &rest args) start cont)
447 (let ((fun-cont (make-continuation)))
448 (ir1-convert start fun-cont function)
449 (assert-continuation-type fun-cont (specifier-type 'function))
450 (ir1-convert-combination-args fun-cont cont args)))
452 ;;; This source transform exists to reduce the amount of work for the
453 ;;; compiler. If the called function is a FUNCTION form, then convert
454 ;;; directly to %FUNCALL, instead of waiting around for type
456 (def-source-transform funcall (function &rest args)
457 (if (and (consp function) (eq (car function) 'function))
458 `(%funcall ,function ,@args)
461 (deftransform %coerce-callable-to-fun ((thing) (function) *
464 "optimize away possible call to FDEFINITION at runtime"
469 ;;;; (LET and LET* can't be implemented as macros due to the fact that
470 ;;;; any pervasive declarations also affect the evaluation of the
473 ;;; Given a list of binding specifiers in the style of Let, return:
474 ;;; 1. The list of var structures for the variables bound.
475 ;;; 2. The initial value form for each variable.
477 ;;; The variable names are checked for legality and globally special
478 ;;; variables are marked as such. Context is the name of the form, for
479 ;;; error reporting purposes.
480 (declaim (ftype (function (list symbol) (values list list list))
481 extract-let-variables))
482 (defun extract-let-variables (bindings context)
486 (flet ((get-var (name)
487 (varify-lambda-arg name
488 (if (eq context 'let*)
491 (dolist (spec bindings)
493 (let ((var (get-var spec)))
495 (names (cons spec var))
498 (unless (proper-list-of-length-p spec 1 2)
499 (compiler-error "The ~S binding spec ~S is malformed."
502 (let* ((name (first spec))
503 (var (get-var name)))
506 (vals (second spec)))))))
508 (values (vars) (vals) (names))))
510 (def-ir1-translator let ((bindings &body body)
513 "LET ({(Var [Value]) | Var}*) Declaration* Form*
514 During evaluation of the Forms, bind the Vars to the result of evaluating the
515 Value forms. The variables are bound in parallel after all of the Values are
517 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
518 (multiple-value-bind (vars values) (extract-let-variables bindings 'let)
519 (let* ((*lexenv* (process-decls decls vars nil cont))
520 (fun-cont (make-continuation))
521 (fun (ir1-convert-lambda-body
522 forms vars :debug-name (debug-namify "LET ~S" bindings))))
523 (reference-leaf start fun-cont fun)
524 (ir1-convert-combination-args fun-cont cont values)))))
526 (def-ir1-translator let* ((bindings &body body)
529 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
530 Similar to LET, but the variables are bound sequentially, allowing each Value
531 form to reference any of the previous Vars."
532 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
533 (multiple-value-bind (vars values) (extract-let-variables bindings 'let*)
534 (let ((*lexenv* (process-decls decls vars nil cont)))
535 (ir1-convert-aux-bindings start cont forms vars values)))))
537 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
538 ;;; and SYMBOL-MACROLET
540 ;;; Note that all these things need to preserve toplevel-formness,
541 ;;; but we don't need to worry about that within an IR1 translator,
542 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
543 ;;; forms before we hit the IR1 transform level.
544 (defun ir1-translate-locally (body start cont)
545 (declare (type list body) (type continuation start cont))
546 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
547 (let ((*lexenv* (process-decls decls nil nil cont)))
548 (ir1-convert-aux-bindings start cont forms nil nil))))
550 (def-ir1-translator locally ((&body body) start cont)
552 "LOCALLY Declaration* Form*
553 Sequentially evaluate the Forms in a lexical environment where the
554 the Declarations have effect. If LOCALLY is a top level form, then
555 the Forms are also processed as top level forms."
556 (ir1-translate-locally body start cont))
560 ;;; Given a list of local function specifications in the style of
561 ;;; FLET, return lists of the function names and of the lambdas which
562 ;;; are their definitions.
564 ;;; The function names are checked for legality. CONTEXT is the name
565 ;;; of the form, for error reporting.
566 (declaim (ftype (function (list symbol) (values list list))
567 extract-flet-variables))
568 (defun extract-flet-variables (definitions context)
571 (dolist (def definitions)
572 (when (or (atom def) (< (length def) 2))
573 (compiler-error "The ~S definition spec ~S is malformed." context def))
575 (let ((name (first def)))
576 (check-fun-name name)
578 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr def))
579 (defs `(lambda ,(second def)
581 (block ,(fun-name-block-name name)
583 (values (names) (defs))))
585 (def-ir1-translator flet ((definitions &body body)
588 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
589 Evaluate the Body-Forms with some local function definitions. The bindings
590 do not enclose the definitions; any use of Name in the Forms will refer to
591 the lexically apparent function definition in the enclosing environment."
592 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
593 (multiple-value-bind (names defs)
594 (extract-flet-variables definitions 'flet)
595 (let* ((fvars (mapcar (lambda (n d)
596 (ir1-convert-lambda d
598 :debug-name (debug-namify
601 (*lexenv* (make-lexenv
602 :default (process-decls decls nil fvars cont)
603 :functions (pairlis names fvars))))
604 (ir1-convert-progn-body start cont forms)))))
606 (def-ir1-translator labels ((definitions &body body) start cont)
608 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
609 Evaluate the Body-Forms with some local function definitions. The bindings
610 enclose the new definitions, so the defined functions can call themselves or
612 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
613 (multiple-value-bind (names defs)
614 (extract-flet-variables definitions 'labels)
615 (let* (;; dummy LABELS functions, to be used as placeholders
616 ;; during construction of real LABELS functions
617 (placeholder-funs (mapcar (lambda (name)
620 :%debug-name (debug-namify
621 "LABELS placeholder ~S"
624 ;; (like PAIRLIS but guaranteed to preserve ordering:)
625 (placeholder-fenv (mapcar #'cons names placeholder-funs))
626 ;; the real LABELS functions, compiled in a LEXENV which
627 ;; includes the dummy LABELS functions
629 (let ((*lexenv* (make-lexenv
630 :functions placeholder-fenv)))
631 (mapcar (lambda (name def)
632 (ir1-convert-lambda def
634 :debug-name (debug-namify
638 ;; Modify all the references to the dummy function leaves so
639 ;; that they point to the real function leaves.
640 (loop for real-fun in real-funs and
641 placeholder-cons in placeholder-fenv do
642 (substitute-leaf real-fun (cdr placeholder-cons))
643 (setf (cdr placeholder-cons) real-fun))
646 (let ((*lexenv* (make-lexenv
647 :default (process-decls decls nil real-funs cont)
648 ;; Use a proper FENV here (not the
649 ;; placeholder used earlier) so that if the
650 ;; lexical environment is used for inline
651 ;; expansion we'll get the right functions.
652 :functions (pairlis names real-funs))))
653 (ir1-convert-progn-body start cont forms))))))
655 ;;;; the THE special operator, and friends
657 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
658 ;;; continuation that the assertion applies to, TYPE is the type
659 ;;; specifier and LEXENV is the current lexical environment. NAME is
660 ;;; the name of the declaration we are doing, for use in error
663 ;;; This is somewhat involved, since a type assertion may only be made
664 ;;; on a continuation, not on a node. We can't just set the
665 ;;; continuation asserted type and let it go at that, since there may
666 ;;; be parallel THE's for the same continuation, i.e.
671 ;;; In this case, our representation can do no better than the union
672 ;;; of these assertions. And if there is a branch with no assertion,
673 ;;; we have nothing at all. We really need to recognize scoping, since
674 ;;; we need to be able to discern between parallel assertions (which
675 ;;; we union) and nested ones (which we intersect).
677 ;;; We represent the scoping by throwing our innermost (intersected)
678 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
679 ;;; intersect our assertions together. If CONT has no uses yet, we
680 ;;; have not yet bottomed out on the first COND branch; in this case
681 ;;; we optimistically assume that this type will be the one we end up
682 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
683 ;;; than the type that we have the first time we bottom out. Later
684 ;;; THE's (or the absence thereof) can only weaken this result.
686 ;;; We make this work by getting USE-CONTINUATION to do the unioning
687 ;;; across COND branches. We can't do it here, since we don't know how
688 ;;; many branches there are going to be.
689 (defun do-the-stuff (type cont lexenv name)
690 (declare (type continuation cont) (type lexenv lexenv))
691 (let* ((ctype (values-specifier-type type))
692 (old-type (or (lexenv-find cont type-restrictions)
694 (intersects (values-types-equal-or-intersect old-type ctype))
695 (int (values-type-intersection old-type ctype))
696 (new (if intersects int old-type)))
697 (when (null (find-uses cont))
698 (setf (continuation-asserted-type cont) new))
699 (when (and (not intersects)
700 (not (policy *lexenv*
701 (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
703 "The type ~S in ~S declaration conflicts with an enclosing assertion:~% ~S"
704 (type-specifier ctype)
706 (type-specifier old-type)))
707 (make-lexenv :type-restrictions `((,cont . ,new))
710 ;;; Assert that FORM evaluates to the specified type (which may be a
713 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
714 ;;; this didn't seem to expand into an assertion, at least for ALIEN
715 ;;; values. Check that SBCL doesn't have this problem.
716 (def-ir1-translator the ((type value) start cont)
717 (let ((*lexenv* (do-the-stuff type cont *lexenv* 'the)))
718 (ir1-convert start cont value)))
720 ;;; This is like the THE special form, except that it believes
721 ;;; whatever you tell it. It will never generate a type check, but
722 ;;; will cause a warning if the compiler can prove the assertion is
725 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
726 ;;; its uses's types, setting it won't work. Instead we must intersect
727 ;;; the type with the uses's DERIVED-TYPE.
728 (def-ir1-translator truly-the ((type value) start cont)
730 (declare (inline member))
731 (let ((type (values-specifier-type type))
732 (old (find-uses cont)))
733 (ir1-convert start cont value)
735 (unless (member use old :test #'eq)
736 (derive-node-type use type)))))
740 ;;; If there is a definition in LEXENV-VARIABLES, just set that,
741 ;;; otherwise look at the global information. If the name is for a
742 ;;; constant, then error out.
743 (def-ir1-translator setq ((&whole source &rest things) start cont)
744 (let ((len (length things)))
746 (compiler-error "odd number of args to SETQ: ~S" source))
748 (let* ((name (first things))
749 (leaf (or (lexenv-find name variables)
750 (find-free-variable name))))
753 (when (constant-p leaf)
754 (compiler-error "~S is a constant and thus can't be set." name))
755 (when (lambda-var-p leaf)
756 (let ((home-lambda (continuation-home-lambda-or-null start)))
758 (pushnew leaf (lambda-refers-to-vars home-lambda))))
759 (when (lambda-var-ignorep leaf)
760 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
761 ;; requires that this be a STYLE-WARNING, not a full warning.
762 (compiler-style-warning
763 "~S is being set even though it was declared to be ignored."
765 (set-variable start cont leaf (second things)))
767 (aver (eq (car leaf) 'MACRO))
768 (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
770 (ir1-convert start cont
771 `(%set-heap-alien ',leaf ,(second things))))))
773 (do ((thing things (cddr thing)))
775 (ir1-convert-progn-body start cont (sets)))
776 (sets `(setq ,(first thing) ,(second thing))))))))
778 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
779 ;;; This should only need to be called in SETQ.
780 (defun set-variable (start cont var value)
781 (declare (type continuation start cont) (type basic-var var))
782 (let ((dest (make-continuation)))
783 (setf (continuation-asserted-type dest) (leaf-type var))
784 (ir1-convert start dest value)
785 (let ((res (make-set :var var :value dest)))
786 (setf (continuation-dest dest) res)
787 (setf (leaf-ever-used var) t)
788 (push res (basic-var-sets var))
790 (use-continuation res cont))))
792 ;;;; CATCH, THROW and UNWIND-PROTECT
794 ;;; We turn THROW into a multiple-value-call of a magical function,
795 ;;; since as as far as IR1 is concerned, it has no interesting
796 ;;; properties other than receiving multiple-values.
797 (def-ir1-translator throw ((tag result) start cont)
800 Do a non-local exit, return the values of Form from the CATCH whose tag
801 evaluates to the same thing as Tag."
802 (ir1-convert start cont
803 `(multiple-value-call #'%throw ,tag ,result)))
805 ;;; This is a special special form used to instantiate a cleanup as
806 ;;; the current cleanup within the body. KIND is the kind of cleanup
807 ;;; to make, and MESS-UP is a form that does the mess-up action. We
808 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
809 ;;; and introduce the cleanup into the lexical environment. We
810 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
811 ;;; cleanup, since this inner cleanup is the interesting one.
812 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
813 (let ((dummy (make-continuation))
814 (dummy2 (make-continuation)))
815 (ir1-convert start dummy mess-up)
816 (let* ((mess-node (continuation-use dummy))
817 (cleanup (make-cleanup :kind kind
819 (old-cup (lexenv-cleanup *lexenv*))
820 (*lexenv* (make-lexenv :cleanup cleanup)))
821 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
822 (ir1-convert dummy dummy2 '(%cleanup-point))
823 (ir1-convert-progn-body dummy2 cont body))))
825 ;;; This is a special special form that makes an "escape function"
826 ;;; which returns unknown values from named block. We convert the
827 ;;; function, set its kind to :ESCAPE, and then reference it. The
828 ;;; :ESCAPE kind indicates that this function's purpose is to
829 ;;; represent a non-local control transfer, and that it might not
830 ;;; actually have to be compiled.
832 ;;; Note that environment analysis replaces references to escape
833 ;;; functions with references to the corresponding NLX-INFO structure.
834 (def-ir1-translator %escape-function ((tag) start cont)
835 (let ((fun (ir1-convert-lambda
837 (return-from ,tag (%unknown-values)))
838 :debug-name (debug-namify "escape function for ~S" tag))))
839 (setf (functional-kind fun) :escape)
840 (reference-leaf start cont fun)))
842 ;;; Yet another special special form. This one looks up a local
843 ;;; function and smashes it to a :CLEANUP function, as well as
845 (def-ir1-translator %cleanup-function ((name) start cont)
846 (let ((fun (lexenv-find name functions)))
847 (aver (lambda-p fun))
848 (setf (functional-kind fun) :cleanup)
849 (reference-leaf start cont fun)))
851 ;;; We represent the possibility of the control transfer by making an
852 ;;; "escape function" that does a lexical exit, and instantiate the
853 ;;; cleanup using %WITHIN-CLEANUP.
854 (def-ir1-translator catch ((tag &body body) start cont)
857 Evaluates Tag and instantiates it as a catcher while the body forms are
858 evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
859 scope of the body, then control will be transferred to the end of the body
860 and the thrown values will be returned."
863 (let ((exit-block (gensym "EXIT-BLOCK-")))
867 (%catch (%escape-function ,exit-block) ,tag)
870 ;;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
871 ;;; cleanup forms into a local function so that they can be referenced
872 ;;; both in the case where we are unwound and in any local exits. We
873 ;;; use %CLEANUP-FUNCTION on this to indicate that reference by
874 ;;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
876 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
878 "Unwind-Protect Protected Cleanup*
879 Evaluate the form Protected, returning its values. The cleanup forms are
880 evaluated whenever the dynamic scope of the Protected form is exited (either
881 due to normal completion or a non-local exit such as THROW)."
884 (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
885 (drop-thru-tag (gensym "DROP-THRU-TAG-"))
886 (exit-tag (gensym "EXIT-TAG-"))
887 (next (gensym "NEXT"))
888 (start (gensym "START"))
889 (count (gensym "COUNT")))
890 `(flet ((,cleanup-fun () ,@cleanup nil))
891 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
892 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
893 ;; and something can be done to make %ESCAPE-FUNCTION have
894 ;; dynamic extent too.
895 (block ,drop-thru-tag
896 (multiple-value-bind (,next ,start ,count)
900 (%unwind-protect (%escape-function ,exit-tag)
901 (%cleanup-function ,cleanup-fun))
902 (return-from ,drop-thru-tag ,protected)))
904 (%continue-unwind ,next ,start ,count)))))))
906 ;;;; multiple-value stuff
908 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
911 ;;; If there are no arguments, then we convert to a normal
912 ;;; combination, ensuring that a MV-COMBINATION always has at least
913 ;;; one argument. This can be regarded as an optimization, but it is
914 ;;; more important for simplifying compilation of MV-COMBINATIONS.
915 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
917 "MULTIPLE-VALUE-CALL Function Values-Form*
918 Call Function, passing all the values of each Values-Form as arguments,
919 values from the first Values-Form making up the first argument, etc."
920 (let* ((fun-cont (make-continuation))
922 (make-mv-combination fun-cont)
923 (make-combination fun-cont))))
924 (ir1-convert start fun-cont
925 (if (and (consp fun) (eq (car fun) 'function))
927 `(%coerce-callable-to-fun ,fun)))
928 (setf (continuation-dest fun-cont) node)
929 (assert-continuation-type fun-cont
930 (specifier-type '(or function symbol)))
931 (collect ((arg-conts))
932 (let ((this-start fun-cont))
934 (let ((this-cont (make-continuation node)))
935 (ir1-convert this-start this-cont arg)
936 (setq this-start this-cont)
937 (arg-conts this-cont)))
938 (prev-link node this-start)
939 (use-continuation node cont)
940 (setf (basic-combination-args node) (arg-conts))))))
942 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
943 ;;; the result code use result continuation (CONT), but transfer
944 ;;; control to the evaluation of the body. In other words, the result
945 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
948 ;;; In order to get the control flow right, we convert the result with
949 ;;; a dummy result continuation, then convert all the uses of the
950 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
951 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
952 ;;; that they are consistent. Note that this doesn't amount to
953 ;;; changing the exit target, since the control destination of an exit
954 ;;; is determined by the block successor; we are just indicating the
955 ;;; continuation that the result is delivered to.
957 ;;; We then convert the body, using another dummy continuation in its
958 ;;; own block as the result. After we are done converting the body, we
959 ;;; move all predecessors of the dummy end block to CONT's block.
961 ;;; Note that we both exploit and maintain the invariant that the CONT
962 ;;; to an IR1 convert method either has no block or starts the block
963 ;;; that control should transfer to after completion for the form.
964 ;;; Nested MV-PROG1's work because during conversion of the result
965 ;;; form, we use dummy continuation whose block is the true control
967 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
969 "MULTIPLE-VALUE-PROG1 Values-Form Form*
970 Evaluate Values-Form and then the Forms, but return all the values of
972 (continuation-starts-block cont)
973 (let* ((dummy-result (make-continuation))
974 (dummy-start (make-continuation))
975 (cont-block (continuation-block cont)))
976 (continuation-starts-block dummy-start)
977 (ir1-convert start dummy-start result)
979 (substitute-continuation-uses cont dummy-start)
981 (continuation-starts-block dummy-result)
982 (ir1-convert-progn-body dummy-start dummy-result forms)
983 (let ((end-block (continuation-block dummy-result)))
984 (dolist (pred (block-pred end-block))
985 (unlink-blocks pred end-block)
986 (link-blocks pred cont-block))
987 (aver (not (continuation-dest dummy-result)))
988 (delete-continuation dummy-result)
989 (remove-from-dfo end-block))))
991 ;;;; interface to defining macros
994 ;;;; classic CMU CL comment:
995 ;;;; DEFMACRO and DEFUN expand into calls to %DEFxxx functions
996 ;;;; so that we get a chance to see what is going on. We define
997 ;;;; IR1 translators for these functions which look at the
998 ;;;; definition and then generate a call to the %%DEFxxx function.
999 ;;;; Alas, this implementation doesn't do the right thing for
1000 ;;;; non-toplevel uses of these forms, so this should probably
1001 ;;;; be changed to use EVAL-WHEN instead.
1003 ;;; Return a new source path with any stuff intervening between the
1004 ;;; current path and the first form beginning with NAME stripped off.
1005 ;;; This is used to hide the guts of DEFmumble macros to prevent
1006 ;;; annoying error messages.
1007 (defun revert-source-path (name)
1008 (do ((path *current-path* (cdr path)))
1009 ((null path) *current-path*)
1010 (let ((first (first path)))
1011 (when (or (eq first name)
1012 (eq first 'original-source-start))
1015 ;;; Warn about incompatible or illegal definitions and add the macro
1016 ;;; to the compiler environment.
1018 ;;; Someday we could check for macro arguments being incompatibly
1019 ;;; redefined. Doing this right will involve finding the old macro
1020 ;;; lambda-list and comparing it with the new one.
1021 (def-ir1-translator %defmacro ((qname qdef lambda-list doc) start cont
1023 (let (;; QNAME is typically a quoted name. I think the idea is to
1024 ;; let %DEFMACRO work as an ordinary function when
1025 ;; interpreting. Whatever the reason the quote is there, we
1026 ;; don't want it any more. -- WHN 19990603
1028 ;; QDEF should be a sharp-quoted definition. We don't want to
1029 ;; make a function of it just yet, so we just drop the
1032 (aver (eq 'function (first qdef)))
1033 (aver (proper-list-of-length-p qdef 2))
1036 (/show "doing IR1 translator for %DEFMACRO" name)
1038 (unless (symbolp name)
1039 (compiler-error "The macro name ~S is not a symbol." name))
1041 (ecase (info :function :kind name)
1044 (remhash name *free-functions*)
1045 (undefine-fun-name name)
1047 "~S is being redefined as a macro when it was ~
1048 previously ~(~A~) to be a function."
1050 (info :function :where-from name)))
1053 (compiler-error "The special form ~S can't be redefined as a macro."
1056 (setf (info :function :kind name) :macro
1057 (info :function :where-from name) :defined
1058 (info :function :macro-function name) (coerce def 'function))
1060 (let* ((*current-path* (revert-source-path 'defmacro))
1061 (fun (ir1-convert-lambda def
1062 :debug-name (debug-namify "DEFMACRO ~S"
1064 (setf (functional-arg-documentation fun) (eval lambda-list))
1066 (ir1-convert start cont `(%%defmacro ',name ,fun ,doc)))
1068 (when sb!xc:*compile-print*
1069 ;; FIXME: It would be nice to convert this, and the other places
1070 ;; which create compiler diagnostic output prefixed by
1071 ;; semicolons, to use some common utility which automatically
1072 ;; prefixes all its output with semicolons. (The addition of
1073 ;; semicolon prefixes was introduced ca. sbcl-0.6.8.10 as the
1074 ;; "MNA compiler message patch", and implemented by modifying a
1075 ;; bunch of output statements on a case-by-case basis, which
1076 ;; seems unnecessarily error-prone and unclear, scattering
1077 ;; implicit information about output style throughout the
1078 ;; system.) Starting by rewriting COMPILER-MUMBLE to add
1079 ;; semicolon prefixes would be a good start, and perhaps also:
1080 ;; * Add semicolon prefixes for "FOO assembled" messages emitted
1081 ;; when e.g. src/assembly/x86/assem-rtns.lisp is processed.
1082 ;; * At least some debugger output messages deserve semicolon
1084 ;; ** restarts table
1085 ;; ** "Within the debugger, you can type HELP for help."
1086 (compiler-mumble "~&; converted ~S~%" name))))
1088 (def-ir1-translator %define-compiler-macro ((name def lambda-list doc)
1091 (let ((name (eval name))
1092 (def (second def))) ; We don't want to make a function just yet...
1094 (when (eq (info :function :kind name) :special-form)
1095 (compiler-error "attempt to define a compiler-macro for special form ~S"
1098 (setf (info :function :compiler-macro-function name)
1099 (coerce def 'function))
1101 (let* ((*current-path* (revert-source-path 'define-compiler-macro))
1102 (fun (ir1-convert-lambda def
1103 :debug-name (debug-namify
1104 "DEFINE-COMPILER-MACRO ~S"
1106 (setf (functional-arg-documentation fun) (eval lambda-list))
1108 (ir1-convert start cont `(%%define-compiler-macro ',name ,fun ,doc)))
1110 (when sb!xc:*compile-print*
1111 (compiler-mumble "~&; converted ~S~%" name))))