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 ;;;; special forms for control
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)
40 (link-node-to-previous-continuation node pred)
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 (link-node-to-previous-continuation 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))))
87 (def-ir1-translator return-from ((name &optional value) start cont)
89 "Return-From Block-Name Value-Form
90 Evaluate the Value-Form, returning its values from the lexically enclosing
91 BLOCK Block-Name. This is constrained to be used only within the dynamic
94 ;; We make CONT start a block just so that it will have a block
95 ;; assigned. People assume that when they pass a continuation into
96 ;; IR1-CONVERT as CONT, it will have a block when it is done.
97 ;; KLUDGE: Note that this block is basically fictitious. In the code
98 ;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
99 ;; it's the block which answers the question "which block is
100 ;; the (SETQ X 3) in?" when the right answer is that (SETQ X 3) is
101 ;; dead code and so doesn't really have a block at all. The existence
102 ;; of this block, and that way that it doesn't explicitly say
103 ;; "I'm actually nowhere at all" makes some logic (e.g.
104 ;; BLOCK-HOME-LAMBDA-OR-NULL) more obscure, and it might be better
105 ;; to get rid of it, perhaps using a special placeholder value
106 ;; to indicate the orphanedness of the code.
107 (continuation-starts-block cont)
108 (let* ((found (or (lexenv-find name blocks)
109 (compiler-error "return for unknown block: ~S" name)))
110 (value-cont (make-continuation))
111 (entry (first found))
112 (exit (make-exit :entry entry
114 (push exit (entry-exits entry))
115 (setf (continuation-dest value-cont) exit)
116 (ir1-convert start value-cont value)
117 (link-node-to-previous-continuation exit value-cont)
118 (let ((home-lambda (continuation-home-lambda-or-null start)))
120 (push entry (lambda-calls-or-closes home-lambda))))
121 (use-continuation exit (second found))))
123 ;;; Return a list of the segments of a TAGBODY. Each segment looks
124 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
125 ;;; tagbody into segments of non-tag statements, and explicitly
126 ;;; represent the drop-through with a GO. The first segment has a
127 ;;; dummy NIL tag, since it represents code before the first tag. The
128 ;;; last segment (which may also be the first segment) ends in NIL
129 ;;; rather than a GO.
130 (defun parse-tagbody (body)
131 (declare (list body))
132 (collect ((segments))
133 (let ((current (cons nil body)))
135 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
137 (segments `(,@current nil))
139 (let ((tag (elt current tag-pos)))
140 (when (assoc tag (segments))
142 "The tag ~S appears more than once in the tagbody."
144 (unless (or (symbolp tag) (integerp tag))
145 (compiler-error "~S is not a legal tagbody statement." tag))
146 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
147 (setq current (nthcdr tag-pos current)))))
150 ;;; Set up the cleanup, emitting the entry node. Then make a block for
151 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
152 ;;; Finally, convert each segment with the precomputed Start and Cont
154 (def-ir1-translator tagbody ((&rest statements) start cont)
156 "Tagbody {Tag | Statement}*
157 Define tags for used with GO. The Statements are evaluated in order
158 (skipping Tags) and NIL is returned. If a statement contains a GO to a
159 defined Tag within the lexical scope of the form, then control is transferred
160 to the next statement following that tag. A Tag must an integer or a
161 symbol. A statement must be a list. Other objects are illegal within the
163 (continuation-starts-block cont)
164 (let* ((dummy (make-continuation))
166 (segments (parse-tagbody statements))
167 (cleanup (make-cleanup :kind :tagbody
169 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
170 (setf (entry-cleanup entry) cleanup)
171 (link-node-to-previous-continuation entry start)
172 (use-continuation entry dummy)
178 (dolist (segment (rest segments))
179 (let* ((tag-cont (make-continuation))
180 (tag (list (car segment) entry tag-cont)))
183 (continuation-starts-block tag-cont)
185 (push (cdr tag) (continuation-lexenv-uses tag-cont))))
188 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
189 (mapc (lambda (segment start cont)
190 (ir1-convert-progn-body start cont (rest segment)))
191 segments (starts) (conts))))))
193 ;;; Emit an EXIT node without any value.
194 (def-ir1-translator go ((tag) start cont)
197 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
198 is constrained to be used only within the dynamic extent of the TAGBODY."
199 (continuation-starts-block cont)
200 (let* ((found (or (lexenv-find tag tags :test #'eql)
201 (compiler-error "attempt to GO to nonexistent tag: ~S"
203 (entry (first found))
204 (exit (make-exit :entry entry)))
205 (push exit (entry-exits entry))
206 (link-node-to-previous-continuation exit start)
207 (let ((home-lambda (continuation-home-lambda-or-null start)))
209 (push entry (lambda-calls-or-closes home-lambda))))
210 (use-continuation exit (second found))))
212 ;;;; translators for compiler-magic special forms
214 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
215 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
216 ;;; so that they're never seen at this level.)
218 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
219 ;;; of non-top-level EVAL-WHENs is very simple:
220 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
221 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
222 ;;; are ignored for non-top-level forms. For non-top-level forms, an
223 ;;; eval-when specifying the :EXECUTE situation is treated as an
224 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
225 ;;; form; otherwise, the forms in the body are ignored.
226 (def-ir1-translator eval-when ((situations &rest forms) start cont)
228 "EVAL-WHEN (Situation*) Form*
229 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
230 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
231 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
232 (declare (ignore ct lt))
233 (ir1-convert-progn-body start cont (and e forms)))
236 ;;; common logic for MACROLET and SYMBOL-MACROLET
238 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
239 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
240 ;;; call FUN (with no arguments).
241 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
242 definitionize-keyword
245 (declare (type function definitionize-fun fun))
246 (declare (type (member :vars :funs) definitionize-keyword))
247 (declare (type list definitions))
248 (unless (= (length definitions)
249 (length (remove-duplicates definitions :key #'first)))
250 (compiler-style-warn "duplicate definitions in ~S" definitions))
251 (let* ((processed-definitions (mapcar definitionize-fun definitions))
252 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
255 ;;; Tweak *LEXENV* to include the DEFINITIONS from a MACROLET, then
256 ;;; call FUN (with no arguments).
258 ;;; This is split off from the IR1 convert method so that it can be
259 ;;; shared by the special-case top level MACROLET processing code.
260 (defun funcall-in-macrolet-lexenv (definitions fun)
261 (%funcall-in-foomacrolet-lexenv
263 (unless (list-of-length-at-least-p definition 2)
265 "The list ~S is too short to be a legal local macro definition."
267 (destructuring-bind (name arglist &body body) definition
268 (unless (symbolp name)
269 (compiler-error "The local macro name ~S is not a symbol." name))
270 (unless (listp arglist)
271 (compiler-error "The local macro argument list ~S is not a list." arglist))
272 (let ((whole (gensym "WHOLE"))
273 (environment (gensym "ENVIRONMENT")))
274 (multiple-value-bind (body local-decls)
275 (parse-defmacro arglist whole body name 'macrolet
276 :environment environment)
280 `(lambda (,whole ,environment)
283 (make-restricted-lexenv *lexenv*)))))))
288 (def-ir1-translator macrolet ((definitions &rest body) start cont)
290 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
291 Evaluate the Body-Forms in an environment with the specified local macros
292 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
293 destructuring lambda list, and the Forms evaluate to the expansion. The
294 Forms are evaluated in the null environment."
295 (funcall-in-macrolet-lexenv definitions
297 (ir1-translate-locally body start cont))))
299 (defun funcall-in-symbol-macrolet-lexenv (definitions fun)
300 (%funcall-in-foomacrolet-lexenv
302 (unless (proper-list-of-length-p definition 2)
303 (compiler-error "malformed symbol/expansion pair: ~S" definition))
304 (destructuring-bind (name expansion) definition
305 (unless (symbolp name)
307 "The local symbol macro name ~S is not a symbol."
309 (let ((kind (info :variable :kind name)))
310 (when (member kind '(:special :constant))
311 (compiler-error "Attempt to bind a ~(~A~) variable with SYMBOL-MACROLET: ~S" kind name)))
312 `(,name . (MACRO . ,expansion))))
317 (def-ir1-translator symbol-macrolet ((macrobindings &body body) start cont)
319 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
320 Define the Names as symbol macros with the given Expansions. Within the
321 body, references to a Name will effectively be replaced with the Expansion."
322 (funcall-in-symbol-macrolet-lexenv
325 (ir1-translate-locally body start cont))))
327 ;;; not really a special form, but..
328 (def-ir1-translator declare ((&rest stuff) start cont)
329 (declare (ignore stuff))
330 ;; We ignore START and CONT too, but we can't use DECLARE IGNORE to
331 ;; tell the compiler about it here, because the DEF-IR1-TRANSLATOR
332 ;; macro would put the DECLARE in the wrong place, so..
334 (compiler-error "misplaced declaration"))
338 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
339 ;;;; into a funny function.
341 ;;; Carefully evaluate a list of forms, returning a list of the results.
342 (defun eval-info-args (args)
343 (declare (list args))
344 (handler-case (mapcar #'eval args)
346 (compiler-error "Lisp error during evaluation of info args:~%~A"
349 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
350 ;;; the template, the second is a list of the results of any
351 ;;; codegen-info args, and the remaining arguments are the runtime
354 ;;; We do various error checking now so that we don't bomb out with
355 ;;; a fatal error during IR2 conversion.
357 ;;; KLUDGE: It's confusing having multiple names floating around for
358 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
359 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
360 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
361 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
362 ;;; VOP or %VOP.. -- WHN 2001-06-11
363 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
364 (def-ir1-translator %primitive ((name &rest args) start cont)
365 (declare (type symbol name))
366 (let* ((template (or (gethash name *backend-template-names*)
367 (bug "undefined primitive ~A" name)))
368 (required (length (template-arg-types template)))
369 (info (template-info-arg-count template))
370 (min (+ required info))
371 (nargs (length args)))
372 (if (template-more-args-type template)
374 (bug "Primitive ~A was called with ~R argument~:P, ~
375 but wants at least ~R."
379 (unless (= nargs min)
380 (bug "Primitive ~A was called with ~R argument~:P, ~
381 but wants exactly ~R."
386 (when (eq (template-result-types template) :conditional)
387 (bug "%PRIMITIVE was used with a conditional template."))
389 (when (template-more-results-type template)
390 (bug "%PRIMITIVE was used with an unknown values template."))
394 `(%%primitive ',template
396 (subseq args required min))
397 ,@(subseq args 0 required)
398 ,@(subseq args min)))))
402 (def-ir1-translator quote ((thing) start cont)
405 Return Value without evaluating it."
406 (reference-constant start cont thing))
408 ;;;; FUNCTION and NAMED-LAMBDA
410 (def-ir1-translator function ((thing) start cont)
413 Return the lexically apparent definition of the function Name. Name may also
414 be a lambda expression."
418 (reference-leaf start
420 (ir1-convert-lambda thing
421 :debug-name (debug-namify
424 (let ((var (find-lexically-apparent-fun
425 thing "as the argument to FUNCTION")))
426 (reference-leaf start cont var)))
428 (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing))
429 :debug-name (debug-namify "#'~S"
431 (setf (getf (functional-plist res) :fin-function) t)
432 (reference-leaf start cont res)))
434 (compiler-error "~S is not a legal function name." thing)))
435 (let ((var (find-lexically-apparent-fun
436 thing "as the argument to FUNCTION")))
437 (reference-leaf start cont var))))
439 ;;; `(NAMED-LAMBDA ,NAME ,@REST) is like `(FUNCTION (LAMBDA ,@REST)),
440 ;;; except that the value of NAME is passed to the compiler for use in
441 ;;; creation of debug information for the resulting function.
443 ;;; NAME can be a legal function name or some arbitrary other thing.
445 ;;; If NAME is a legal function name, then the caller should be
446 ;;; planning to set (FDEFINITION NAME) to the created function.
447 ;;; (Otherwise the debug names will be inconsistent and thus
448 ;;; unnecessarily confusing.)
450 ;;; Arbitrary other things are appropriate for naming things which are
451 ;;; not the FDEFINITION of NAME. E.g.
452 ;;; NAME = (:FLET FOO BAR)
453 ;;; for the FLET function in
455 ;;; (FLET ((FOO (Y) (+ X Y)))
458 ;;; NAME = (:METHOD PRINT-OBJECT :AROUND (STARSHIP T))
459 ;;; for the function used to implement
460 ;;; (DEFMETHOD PRINT-OBJECT :AROUND ((SS STARSHIP) STREAM) ...).
461 (def-ir1-translator named-lambda ((name &rest rest) start cont)
462 (reference-leaf start
464 (if (legal-fun-name-p name)
465 (ir1-convert-lambda `(lambda ,@rest)
467 (ir1-convert-lambda `(lambda ,@rest)
472 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
473 ;;; (not symbols). %FUNCALL is used directly in some places where the
474 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
475 (deftransform funcall ((function &rest args) * *)
476 (let ((arg-names (make-gensym-list (length args))))
477 `(lambda (function ,@arg-names)
478 (%funcall ,(if (csubtypep (continuation-type function)
479 (specifier-type 'function))
481 '(%coerce-callable-to-fun function))
484 (def-ir1-translator %funcall ((function &rest args) start cont)
485 (let ((fun-cont (make-continuation)))
486 (ir1-convert start fun-cont function)
487 (assert-continuation-type fun-cont (specifier-type 'function))
488 (ir1-convert-combination-args fun-cont cont args)))
490 ;;; This source transform exists to reduce the amount of work for the
491 ;;; compiler. If the called function is a FUNCTION form, then convert
492 ;;; directly to %FUNCALL, instead of waiting around for type
494 (define-source-transform funcall (function &rest args)
495 (if (and (consp function) (eq (car function) 'function))
496 `(%funcall ,function ,@args)
499 (deftransform %coerce-callable-to-fun ((thing) (function) *
501 "optimize away possible call to FDEFINITION at runtime"
506 ;;;; (LET and LET* can't be implemented as macros due to the fact that
507 ;;;; any pervasive declarations also affect the evaluation of the
510 ;;; Given a list of binding specifiers in the style of LET, return:
511 ;;; 1. The list of var structures for the variables bound.
512 ;;; 2. The initial value form for each variable.
514 ;;; The variable names are checked for legality and globally special
515 ;;; variables are marked as such. Context is the name of the form, for
516 ;;; error reporting purposes.
517 (declaim (ftype (function (list symbol) (values list list))
519 (defun extract-let-vars (bindings context)
523 (flet ((get-var (name)
524 (varify-lambda-arg name
525 (if (eq context 'let*)
528 (dolist (spec bindings)
530 (let ((var (get-var spec)))
535 (unless (proper-list-of-length-p spec 1 2)
536 (compiler-error "The ~S binding spec ~S is malformed."
539 (let* ((name (first spec))
540 (var (get-var name)))
543 (vals (second spec)))))))
545 (values (vars) (vals))))
547 (def-ir1-translator let ((bindings &body body)
550 "LET ({(Var [Value]) | Var}*) Declaration* Form*
551 During evaluation of the Forms, bind the Vars to the result of evaluating the
552 Value forms. The variables are bound in parallel after all of the Values are
554 (multiple-value-bind (forms decls) (parse-body body nil)
555 (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
556 (let ((fun-cont (make-continuation)))
557 (let* ((*lexenv* (process-decls decls vars nil cont))
558 (fun (ir1-convert-lambda-body
560 :debug-name (debug-namify "LET ~S" bindings))))
561 (reference-leaf start fun-cont fun))
562 (ir1-convert-combination-args fun-cont cont values)))))
564 (def-ir1-translator let* ((bindings &body body)
567 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
568 Similar to LET, but the variables are bound sequentially, allowing each Value
569 form to reference any of the previous Vars."
570 (multiple-value-bind (forms decls) (parse-body body nil)
571 (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
572 (let ((*lexenv* (process-decls decls vars nil cont)))
573 (ir1-convert-aux-bindings start cont forms vars values)))))
575 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
576 ;;; and SYMBOL-MACROLET
578 ;;; Note that all these things need to preserve toplevel-formness,
579 ;;; but we don't need to worry about that within an IR1 translator,
580 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
581 ;;; forms before we hit the IR1 transform level.
582 (defun ir1-translate-locally (body start cont)
583 (declare (type list body) (type continuation start cont))
584 (multiple-value-bind (forms decls) (parse-body body nil)
585 (let ((*lexenv* (process-decls decls nil nil cont)))
586 (ir1-convert-aux-bindings start cont forms nil nil))))
588 (def-ir1-translator locally ((&body body) start cont)
590 "LOCALLY Declaration* Form*
591 Sequentially evaluate the Forms in a lexical environment where the
592 the Declarations have effect. If LOCALLY is a top level form, then
593 the Forms are also processed as top level forms."
594 (ir1-translate-locally body start cont))
598 ;;; Given a list of local function specifications in the style of
599 ;;; FLET, return lists of the function names and of the lambdas which
600 ;;; are their definitions.
602 ;;; The function names are checked for legality. CONTEXT is the name
603 ;;; of the form, for error reporting.
604 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
605 (defun extract-flet-vars (definitions context)
608 (dolist (def definitions)
609 (when (or (atom def) (< (length def) 2))
610 (compiler-error "The ~S definition spec ~S is malformed." context def))
612 (let ((name (first def)))
613 (check-fun-name name)
615 (multiple-value-bind (forms decls) (parse-body (cddr def))
616 (defs `(lambda ,(second def)
618 (block ,(fun-name-block-name name)
620 (values (names) (defs))))
622 (def-ir1-translator flet ((definitions &body body)
625 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
626 Evaluate the Body-Forms with some local function definitions. The bindings
627 do not enclose the definitions; any use of Name in the Forms will refer to
628 the lexically apparent function definition in the enclosing environment."
629 (multiple-value-bind (forms decls) (parse-body body nil)
630 (multiple-value-bind (names defs)
631 (extract-flet-vars definitions 'flet)
632 (let* ((fvars (mapcar (lambda (n d)
633 (ir1-convert-lambda d
635 :debug-name (debug-namify
638 (*lexenv* (make-lexenv
639 :default (process-decls decls nil fvars cont)
640 :funs (pairlis names fvars))))
641 (ir1-convert-progn-body start cont forms)))))
643 (def-ir1-translator labels ((definitions &body body) start cont)
645 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
646 Evaluate the Body-Forms with some local function definitions. The bindings
647 enclose the new definitions, so the defined functions can call themselves or
649 (multiple-value-bind (forms decls) (parse-body body nil)
650 (multiple-value-bind (names defs)
651 (extract-flet-vars definitions 'labels)
652 (let* (;; dummy LABELS functions, to be used as placeholders
653 ;; during construction of real LABELS functions
654 (placeholder-funs (mapcar (lambda (name)
657 :%debug-name (debug-namify
658 "LABELS placeholder ~S"
661 ;; (like PAIRLIS but guaranteed to preserve ordering:)
662 (placeholder-fenv (mapcar #'cons names placeholder-funs))
663 ;; the real LABELS functions, compiled in a LEXENV which
664 ;; includes the dummy LABELS functions
666 (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
667 (mapcar (lambda (name def)
668 (ir1-convert-lambda def
670 :debug-name (debug-namify
674 ;; Modify all the references to the dummy function leaves so
675 ;; that they point to the real function leaves.
676 (loop for real-fun in real-funs and
677 placeholder-cons in placeholder-fenv do
678 (substitute-leaf real-fun (cdr placeholder-cons))
679 (setf (cdr placeholder-cons) real-fun))
682 (let ((*lexenv* (make-lexenv
683 :default (process-decls decls nil real-funs cont)
684 ;; Use a proper FENV here (not the
685 ;; placeholder used earlier) so that if the
686 ;; lexical environment is used for inline
687 ;; expansion we'll get the right functions.
688 :funs (pairlis names real-funs))))
689 (ir1-convert-progn-body start cont forms))))))
691 ;;;; the THE special operator, and friends
693 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
694 ;;; continuation that the assertion applies to, TYPE is the type
695 ;;; specifier and LEXENV is the current lexical environment. NAME is
696 ;;; the name of the declaration we are doing, for use in error
699 ;;; This is somewhat involved, since a type assertion may only be made
700 ;;; on a continuation, not on a node. We can't just set the
701 ;;; continuation asserted type and let it go at that, since there may
702 ;;; be parallel THE's for the same continuation, i.e.
707 ;;; In this case, our representation can do no better than the union
708 ;;; of these assertions. And if there is a branch with no assertion,
709 ;;; we have nothing at all. We really need to recognize scoping, since
710 ;;; we need to be able to discern between parallel assertions (which
711 ;;; we union) and nested ones (which we intersect).
713 ;;; We represent the scoping by throwing our innermost (intersected)
714 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
715 ;;; intersect our assertions together. If CONT has no uses yet, we
716 ;;; have not yet bottomed out on the first COND branch; in this case
717 ;;; we optimistically assume that this type will be the one we end up
718 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
719 ;;; than the type that we have the first time we bottom out. Later
720 ;;; THE's (or the absence thereof) can only weaken this result.
722 ;;; We make this work by getting USE-CONTINUATION to do the unioning
723 ;;; across COND branches. We can't do it here, since we don't know how
724 ;;; many branches there are going to be.
725 (defun ir1ize-the-or-values (type cont lexenv place)
726 (declare (type continuation cont) (type lexenv lexenv))
727 (let* ((ctype (if (typep type 'ctype) type (compiler-values-specifier-type type)))
728 (old-type (or (lexenv-find cont type-restrictions)
730 (intersects (values-types-equal-or-intersect old-type ctype))
731 (new (values-type-intersection old-type ctype)))
732 (when (null (find-uses cont))
733 (setf (continuation-asserted-type cont) new))
734 (when (and (not intersects)
735 ;; FIXME: Is it really right to look at *LEXENV* here,
736 ;; instead of looking at the LEXENV argument? Why?
737 (not (policy *lexenv*
738 (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
740 "The type ~S ~A conflicts with an enclosing assertion:~% ~S"
741 (type-specifier ctype)
743 (type-specifier old-type)))
744 (make-lexenv :type-restrictions `((,cont . ,new))
747 ;;; Assert that FORM evaluates to the specified type (which may be a
750 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
751 ;;; this didn't seem to expand into an assertion, at least for ALIEN
752 ;;; values. Check that SBCL doesn't have this problem.
753 (def-ir1-translator the ((type value) start cont)
754 (with-continuation-type-assertion (cont (compiler-values-specifier-type type)
755 "in THE declaration")
756 (ir1-convert start cont value)))
758 ;;; This is like the THE special form, except that it believes
759 ;;; whatever you tell it. It will never generate a type check, but
760 ;;; will cause a warning if the compiler can prove the assertion is
763 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
764 ;;; its uses's types, setting it won't work. Instead we must intersect
765 ;;; the type with the uses's DERIVED-TYPE.
766 (def-ir1-translator truly-the ((type value) start cont)
768 (declare (inline member))
769 (let ((type (compiler-values-specifier-type type))
770 (old (find-uses cont)))
771 (ir1-convert start cont value)
773 (unless (member use old :test #'eq)
774 (derive-node-type use type)))))
778 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
779 ;;; look at the global information. If the name is for a constant,
781 (def-ir1-translator setq ((&whole source &rest things) start cont)
782 (let ((len (length things)))
784 (compiler-error "odd number of args to SETQ: ~S" source))
786 (let* ((name (first things))
787 (leaf (or (lexenv-find name vars)
788 (find-free-var name))))
791 (when (constant-p leaf)
792 (compiler-error "~S is a constant and thus can't be set." name))
793 (when (lambda-var-p leaf)
794 (let ((home-lambda (continuation-home-lambda-or-null start)))
796 (pushnew leaf (lambda-calls-or-closes home-lambda))))
797 (when (lambda-var-ignorep leaf)
798 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
799 ;; requires that this be a STYLE-WARNING, not a full warning.
801 "~S is being set even though it was declared to be ignored."
803 (setq-var start cont leaf (second things)))
805 (aver (eq (car leaf) 'MACRO))
806 (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
808 (ir1-convert start cont
809 `(%set-heap-alien ',leaf ,(second things))))))
811 (do ((thing things (cddr thing)))
813 (ir1-convert-progn-body start cont (sets)))
814 (sets `(setq ,(first thing) ,(second thing))))))))
816 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
817 ;;; This should only need to be called in SETQ.
818 (defun setq-var (start cont var value)
819 (declare (type continuation start cont) (type basic-var var))
820 (let ((dest (make-continuation)))
821 (setf (continuation-asserted-type dest) (leaf-type var))
822 (ir1-convert start dest value)
823 (let ((res (make-set :var var :value dest)))
824 (setf (continuation-dest dest) res)
825 (setf (leaf-ever-used var) t)
826 (push res (basic-var-sets var))
827 (link-node-to-previous-continuation res dest)
828 (use-continuation res cont))))
830 ;;;; CATCH, THROW and UNWIND-PROTECT
832 ;;; We turn THROW into a multiple-value-call of a magical function,
833 ;;; since as as far as IR1 is concerned, it has no interesting
834 ;;; properties other than receiving multiple-values.
835 (def-ir1-translator throw ((tag result) start cont)
838 Do a non-local exit, return the values of Form from the CATCH whose tag
839 evaluates to the same thing as Tag."
840 (ir1-convert start cont
841 `(multiple-value-call #'%throw ,tag ,result)))
843 ;;; This is a special special form used to instantiate a cleanup as
844 ;;; the current cleanup within the body. KIND is the kind of cleanup
845 ;;; to make, and MESS-UP is a form that does the mess-up action. We
846 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
847 ;;; and introduce the cleanup into the lexical environment. We
848 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
849 ;;; cleanup, since this inner cleanup is the interesting one.
850 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
851 (let ((dummy (make-continuation))
852 (dummy2 (make-continuation)))
853 (ir1-convert start dummy mess-up)
854 (let* ((mess-node (continuation-use dummy))
855 (cleanup (make-cleanup :kind kind
857 (old-cup (lexenv-cleanup *lexenv*))
858 (*lexenv* (make-lexenv :cleanup cleanup)))
859 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
860 (ir1-convert dummy dummy2 '(%cleanup-point))
861 (ir1-convert-progn-body dummy2 cont body))))
863 ;;; This is a special special form that makes an "escape function"
864 ;;; which returns unknown values from named block. We convert the
865 ;;; function, set its kind to :ESCAPE, and then reference it. The
866 ;;; :ESCAPE kind indicates that this function's purpose is to
867 ;;; represent a non-local control transfer, and that it might not
868 ;;; actually have to be compiled.
870 ;;; Note that environment analysis replaces references to escape
871 ;;; functions with references to the corresponding NLX-INFO structure.
872 (def-ir1-translator %escape-fun ((tag) start cont)
873 (let ((fun (ir1-convert-lambda
875 (return-from ,tag (%unknown-values)))
876 :debug-name (debug-namify "escape function for ~S" tag))))
877 (setf (functional-kind fun) :escape)
878 (reference-leaf start cont fun)))
880 ;;; Yet another special special form. This one looks up a local
881 ;;; function and smashes it to a :CLEANUP function, as well as
883 (def-ir1-translator %cleanup-fun ((name) start cont)
884 (let ((fun (lexenv-find name funs)))
885 (aver (lambda-p fun))
886 (setf (functional-kind fun) :cleanup)
887 (reference-leaf start cont fun)))
889 ;;; We represent the possibility of the control transfer by making an
890 ;;; "escape function" that does a lexical exit, and instantiate the
891 ;;; cleanup using %WITHIN-CLEANUP.
892 (def-ir1-translator catch ((tag &body body) start cont)
895 Evaluates Tag and instantiates it as a catcher while the body forms are
896 evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
897 scope of the body, then control will be transferred to the end of the body
898 and the thrown values will be returned."
901 (let ((exit-block (gensym "EXIT-BLOCK-")))
905 (%catch (%escape-fun ,exit-block) ,tag)
908 ;;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
909 ;;; cleanup forms into a local function so that they can be referenced
910 ;;; both in the case where we are unwound and in any local exits. We
911 ;;; use %CLEANUP-FUN on this to indicate that reference by
912 ;;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
914 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
916 "Unwind-Protect Protected Cleanup*
917 Evaluate the form Protected, returning its values. The cleanup forms are
918 evaluated whenever the dynamic scope of the Protected form is exited (either
919 due to normal completion or a non-local exit such as THROW)."
922 (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
923 (drop-thru-tag (gensym "DROP-THRU-TAG-"))
924 (exit-tag (gensym "EXIT-TAG-"))
925 (next (gensym "NEXT"))
926 (start (gensym "START"))
927 (count (gensym "COUNT")))
928 `(flet ((,cleanup-fun () ,@cleanup nil))
929 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
930 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
931 ;; and something can be done to make %ESCAPE-FUN have
932 ;; dynamic extent too.
933 (block ,drop-thru-tag
934 (multiple-value-bind (,next ,start ,count)
938 (%unwind-protect (%escape-fun ,exit-tag)
939 (%cleanup-fun ,cleanup-fun))
940 (return-from ,drop-thru-tag ,protected)))
942 (%continue-unwind ,next ,start ,count)))))))
944 ;;;; multiple-value stuff
946 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
949 ;;; If there are no arguments, then we convert to a normal
950 ;;; combination, ensuring that a MV-COMBINATION always has at least
951 ;;; one argument. This can be regarded as an optimization, but it is
952 ;;; more important for simplifying compilation of MV-COMBINATIONS.
953 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
955 "MULTIPLE-VALUE-CALL Function Values-Form*
956 Call Function, passing all the values of each Values-Form as arguments,
957 values from the first Values-Form making up the first argument, etc."
958 (let* ((fun-cont (make-continuation))
960 (make-mv-combination fun-cont)
961 (make-combination fun-cont))))
962 (ir1-convert start fun-cont
963 (if (and (consp fun) (eq (car fun) 'function))
965 `(%coerce-callable-to-fun ,fun)))
966 (setf (continuation-dest fun-cont) node)
967 (assert-continuation-type fun-cont
968 (specifier-type '(or function symbol)))
969 (collect ((arg-conts))
970 (let ((this-start fun-cont))
972 (let ((this-cont (make-continuation node)))
973 (ir1-convert this-start this-cont arg)
974 (setq this-start this-cont)
975 (arg-conts this-cont)))
976 (link-node-to-previous-continuation node this-start)
977 (use-continuation node cont)
978 (setf (basic-combination-args node) (arg-conts))))))
980 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
981 ;;; the result code use result continuation (CONT), but transfer
982 ;;; control to the evaluation of the body. In other words, the result
983 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
986 ;;; In order to get the control flow right, we convert the result with
987 ;;; a dummy result continuation, then convert all the uses of the
988 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
989 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
990 ;;; that they are consistent. Note that this doesn't amount to
991 ;;; changing the exit target, since the control destination of an exit
992 ;;; is determined by the block successor; we are just indicating the
993 ;;; continuation that the result is delivered to.
995 ;;; We then convert the body, using another dummy continuation in its
996 ;;; own block as the result. After we are done converting the body, we
997 ;;; move all predecessors of the dummy end block to CONT's block.
999 ;;; Note that we both exploit and maintain the invariant that the CONT
1000 ;;; to an IR1 convert method either has no block or starts the block
1001 ;;; that control should transfer to after completion for the form.
1002 ;;; Nested MV-PROG1's work because during conversion of the result
1003 ;;; form, we use dummy continuation whose block is the true control
1005 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
1007 "MULTIPLE-VALUE-PROG1 Values-Form Form*
1008 Evaluate Values-Form and then the Forms, but return all the values of
1010 (continuation-starts-block cont)
1011 (let* ((dummy-result (make-continuation))
1012 (dummy-start (make-continuation))
1013 (cont-block (continuation-block cont)))
1014 (continuation-starts-block dummy-start)
1015 (ir1-convert start dummy-start result)
1017 (with-continuation-type-assertion
1018 (cont (continuation-asserted-type dummy-start)
1019 "of the first form")
1020 (substitute-continuation-uses cont dummy-start))
1022 (continuation-starts-block dummy-result)
1023 (ir1-convert-progn-body dummy-start dummy-result forms)
1024 (let ((end-block (continuation-block dummy-result)))
1025 (dolist (pred (block-pred end-block))
1026 (unlink-blocks pred end-block)
1027 (link-blocks pred cont-block))
1028 (aver (not (continuation-dest dummy-result)))
1029 (delete-continuation dummy-result)
1030 (remove-from-dfo end-block))))
1032 ;;;; interface to defining macros
1035 ;;;; classic CMU CL comment:
1036 ;;;; DEFMACRO and DEFUN expand into calls to %DEFxxx functions
1037 ;;;; so that we get a chance to see what is going on. We define
1038 ;;;; IR1 translators for these functions which look at the
1039 ;;;; definition and then generate a call to the %%DEFxxx function.
1040 ;;;; Alas, this implementation doesn't do the right thing for
1041 ;;;; non-toplevel uses of these forms, so this should probably
1042 ;;;; be changed to use EVAL-WHEN instead.
1044 ;;; Return a new source path with any stuff intervening between the
1045 ;;; current path and the first form beginning with NAME stripped off.
1046 ;;; This is used to hide the guts of DEFmumble macros to prevent
1047 ;;; annoying error messages.
1048 (defun revert-source-path (name)
1049 (do ((path *current-path* (cdr path)))
1050 ((null path) *current-path*)
1051 (let ((first (first path)))
1052 (when (or (eq first name)
1053 (eq first 'original-source-start))
1056 (def-ir1-translator %define-compiler-macro ((name def lambda-list doc)
1059 (let ((name (eval name))
1060 (def (second def))) ; We don't want to make a function just yet...
1062 (when (eq (info :function :kind name) :special-form)
1063 (compiler-error "attempt to define a compiler-macro for special form ~S"
1066 (setf (info :function :compiler-macro-function name)
1067 (coerce def 'function))
1069 (let* ((*current-path* (revert-source-path 'define-compiler-macro))
1070 (fun (ir1-convert-lambda def
1071 :debug-name (debug-namify
1072 "DEFINE-COMPILER-MACRO ~S"
1074 (setf (functional-arg-documentation fun) (eval lambda-list))
1076 (ir1-convert start cont `(%%define-compiler-macro ',name ,fun ,doc)))
1078 (when sb!xc:*compile-print*
1079 (compiler-mumble "~&; converted ~S~%" name))))