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 ;; IR1-CONVERT-MAYBE-PREDICATE requires DEST to be CIF, so the
39 ;; order of the following two forms is important
40 (setf (continuation-dest pred) node)
41 (ir1-convert start pred test)
42 (link-node-to-previous-continuation node pred)
43 (use-continuation node dummy-cont)
45 (let ((start-block (continuation-block pred)))
46 (setf (block-last start-block) node)
47 (continuation-starts-block cont)
49 (link-blocks start-block then-block)
50 (link-blocks start-block else-block))
52 (ir1-convert then-cont cont then)
53 (ir1-convert else-cont cont else)))
55 ;;;; BLOCK and TAGBODY
57 ;;;; We make an ENTRY node to mark the start and a :ENTRY cleanup to
58 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an EXIT
61 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
62 ;;; body in the modified environment. We make CONT start a block now,
63 ;;; since if it was done later, the block would be in the wrong
65 (def-ir1-translator block ((name &rest forms) start cont)
68 Evaluate the Forms as a PROGN. Within the lexical scope of the body,
69 (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
70 result of Value-Form."
71 (unless (symbolp name)
72 (compiler-error "The block name ~S is not a symbol." name))
73 (continuation-starts-block cont)
74 (let* ((dummy (make-continuation))
76 (cleanup (make-cleanup :kind :block
78 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
79 (setf (entry-cleanup entry) cleanup)
80 (link-node-to-previous-continuation entry start)
81 (use-continuation entry dummy)
83 (let* ((env-entry (list entry cont))
84 (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
86 (push env-entry (continuation-lexenv-uses cont))
87 (ir1-convert-progn-body dummy cont forms))))
89 (def-ir1-translator return-from ((name &optional value) start cont)
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 (link-node-to-previous-continuation exit value-cont)
120 (let ((home-lambda (continuation-home-lambda-or-null start)))
122 (push entry (lambda-calls-or-closes home-lambda))))
123 (use-continuation exit (second found))))
125 ;;; Return a list of the segments of a TAGBODY. Each segment looks
126 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
127 ;;; tagbody into segments of non-tag statements, and explicitly
128 ;;; represent the drop-through with a GO. The first segment has a
129 ;;; dummy NIL tag, since it represents code before the first tag. The
130 ;;; last segment (which may also be the first segment) ends in NIL
131 ;;; rather than a GO.
132 (defun parse-tagbody (body)
133 (declare (list body))
134 (collect ((segments))
135 (let ((current (cons nil body)))
137 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
139 (segments `(,@current nil))
141 (let ((tag (elt current tag-pos)))
142 (when (assoc tag (segments))
144 "The tag ~S appears more than once in the tagbody."
146 (unless (or (symbolp tag) (integerp tag))
147 (compiler-error "~S is not a legal tagbody statement." tag))
148 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
149 (setq current (nthcdr tag-pos current)))))
152 ;;; Set up the cleanup, emitting the entry node. Then make a block for
153 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
154 ;;; Finally, convert each segment with the precomputed Start and Cont
156 (def-ir1-translator tagbody ((&rest statements) start cont)
158 "Tagbody {Tag | Statement}*
159 Define tags for used with GO. The Statements are evaluated in order
160 (skipping Tags) and NIL is returned. If a statement contains a GO to a
161 defined Tag within the lexical scope of the form, then control is transferred
162 to the next statement following that tag. A Tag must an integer or a
163 symbol. A statement must be a list. Other objects are illegal within the
165 (continuation-starts-block cont)
166 (let* ((dummy (make-continuation))
168 (segments (parse-tagbody statements))
169 (cleanup (make-cleanup :kind :tagbody
171 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
172 (setf (entry-cleanup entry) cleanup)
173 (link-node-to-previous-continuation entry start)
174 (use-continuation entry dummy)
180 (dolist (segment (rest segments))
181 (let* ((tag-cont (make-continuation))
182 (tag (list (car segment) entry tag-cont)))
185 (continuation-starts-block tag-cont)
187 (push (cdr tag) (continuation-lexenv-uses tag-cont))))
190 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
191 (mapc (lambda (segment start cont)
192 (ir1-convert-progn-body start cont (rest segment)))
193 segments (starts) (conts))))))
195 ;;; Emit an EXIT node without any value.
196 (def-ir1-translator go ((tag) start cont)
199 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
200 is constrained to be used only within the dynamic extent of the TAGBODY."
201 (continuation-starts-block cont)
202 (let* ((found (or (lexenv-find tag tags :test #'eql)
203 (compiler-error "attempt to GO to nonexistent tag: ~S"
205 (entry (first found))
206 (exit (make-exit :entry entry)))
207 (push exit (entry-exits entry))
208 (link-node-to-previous-continuation exit start)
209 (let ((home-lambda (continuation-home-lambda-or-null start)))
211 (push entry (lambda-calls-or-closes home-lambda))))
212 (use-continuation exit (second found))))
214 ;;;; translators for compiler-magic special forms
216 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
217 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
218 ;;; so that they're never seen at this level.)
220 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
221 ;;; of non-top-level EVAL-WHENs is very simple:
222 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
223 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
224 ;;; are ignored for non-top-level forms. For non-top-level forms, an
225 ;;; eval-when specifying the :EXECUTE situation is treated as an
226 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
227 ;;; form; otherwise, the forms in the body are ignored.
228 (def-ir1-translator eval-when ((situations &rest forms) start cont)
230 "EVAL-WHEN (Situation*) Form*
231 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
232 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
233 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
234 (declare (ignore ct lt))
235 (ir1-convert-progn-body start cont (and e forms)))
238 ;;; common logic for MACROLET and SYMBOL-MACROLET
240 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
241 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
242 ;;; call FUN (with no arguments).
243 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
244 definitionize-keyword
247 (declare (type function definitionize-fun fun))
248 (declare (type (member :vars :funs) definitionize-keyword))
249 (declare (type list definitions))
250 (unless (= (length definitions)
251 (length (remove-duplicates definitions :key #'first)))
252 (compiler-style-warn "duplicate definitions in ~S" definitions))
253 (let* ((processed-definitions (mapcar definitionize-fun definitions))
254 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
255 (funcall fun definitionize-keyword processed-definitions)))
257 ;;; Tweak LEXENV to include the DEFINITIONS from a MACROLET, then
258 ;;; call FUN (with no arguments).
260 ;;; This is split off from the IR1 convert method so that it can be
261 ;;; shared by the special-case top level MACROLET processing code, and
262 ;;; further split so that the special-case MACROLET processing code in
263 ;;; EVAL can likewise make use of it.
264 (defmacro macrolet-definitionize-fun (context lexenv)
265 (flet ((make-error-form (control &rest args)
267 (:compile `(compiler-error ,control ,@args))
268 (:eval `(error 'simple-program-error
269 :format-control ,control
270 :format-arguments (list ,@args))))))
271 `(lambda (definition)
272 (unless (list-of-length-at-least-p definition 2)
273 ,(make-error-form "The list ~S is too short to be a legal local macro definition." 'definition))
274 (destructuring-bind (name arglist &body body) definition
275 (unless (symbolp name)
276 ,(make-error-form "The local macro name ~S is not a symbol." 'name))
277 (unless (listp arglist)
278 ,(make-error-form "The local macro argument list ~S is not a list." 'arglist))
279 (let ((whole (gensym "WHOLE"))
280 (environment (gensym "ENVIRONMENT")))
281 (multiple-value-bind (body local-decls)
282 (parse-defmacro arglist whole body name 'macrolet
283 :environment environment)
287 `(lambda (,whole ,environment)
292 (defun funcall-in-macrolet-lexenv (definitions fun)
293 (%funcall-in-foomacrolet-lexenv
294 (macrolet-definitionize-fun :compile (make-restricted-lexenv *lexenv*))
299 (def-ir1-translator macrolet ((definitions &rest body) start cont)
301 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
302 Evaluate the Body-Forms in an environment with the specified local macros
303 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
304 destructuring lambda list, and the Forms evaluate to the expansion.."
305 (funcall-in-macrolet-lexenv
308 (declare (ignore funs))
309 (ir1-translate-locally body start cont))))
311 (defmacro symbol-macrolet-definitionize-fun (context)
312 (flet ((make-error-form (control &rest args)
314 (:compile `(compiler-error ,control ,@args))
315 (:eval `(error 'simple-program-error
316 :format-control ,control
317 :format-arguments (list ,@args))))))
318 `(lambda (definition)
319 (unless (proper-list-of-length-p definition 2)
320 ,(make-error-form "malformed symbol/expansion pair: ~S" 'definition))
321 (destructuring-bind (name expansion) definition
322 (unless (symbolp name)
324 "The local symbol macro name ~S is not a symbol."
326 (let ((kind (info :variable :kind name)))
327 (when (member kind '(:special :constant))
329 "Attempt to bind a ~(~A~) variable with SYMBOL-MACROLET: ~S"
331 `(,name . (MACRO . ,expansion))))))1
333 (defun funcall-in-symbol-macrolet-lexenv (definitions fun)
334 (%funcall-in-foomacrolet-lexenv
335 (symbol-macrolet-definitionize-fun :compile)
340 (def-ir1-translator symbol-macrolet ((macrobindings &body body) start cont)
342 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
343 Define the Names as symbol macros with the given Expansions. Within the
344 body, references to a Name will effectively be replaced with the Expansion."
345 (funcall-in-symbol-macrolet-lexenv
348 (ir1-translate-locally body start cont :vars vars))))
352 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
353 ;;;; into a funny function.
355 ;;; Carefully evaluate a list of forms, returning a list of the results.
356 (defun eval-info-args (args)
357 (declare (list args))
358 (handler-case (mapcar #'eval args)
360 (compiler-error "Lisp error during evaluation of info args:~%~A"
363 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
364 ;;; the template, the second is a list of the results of any
365 ;;; codegen-info args, and the remaining arguments are the runtime
368 ;;; We do various error checking now so that we don't bomb out with
369 ;;; a fatal error during IR2 conversion.
371 ;;; KLUDGE: It's confusing having multiple names floating around for
372 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
373 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
374 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
375 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
376 ;;; VOP or %VOP.. -- WHN 2001-06-11
377 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
378 (def-ir1-translator %primitive ((name &rest args) start cont)
379 (declare (type symbol name))
380 (let* ((template (or (gethash name *backend-template-names*)
381 (bug "undefined primitive ~A" name)))
382 (required (length (template-arg-types template)))
383 (info (template-info-arg-count template))
384 (min (+ required info))
385 (nargs (length args)))
386 (if (template-more-args-type template)
388 (bug "Primitive ~A was called with ~R argument~:P, ~
389 but wants at least ~R."
393 (unless (= nargs min)
394 (bug "Primitive ~A was called with ~R argument~:P, ~
395 but wants exactly ~R."
400 (when (eq (template-result-types template) :conditional)
401 (bug "%PRIMITIVE was used with a conditional template."))
403 (when (template-more-results-type template)
404 (bug "%PRIMITIVE was used with an unknown values template."))
408 `(%%primitive ',template
410 (subseq args required min))
411 ,@(subseq args 0 required)
412 ,@(subseq args min)))))
416 (def-ir1-translator quote ((thing) start cont)
419 Return Value without evaluating it."
420 (reference-constant start cont thing))
422 ;;;; FUNCTION and NAMED-LAMBDA
424 (def-ir1-translator function ((thing) start cont)
427 Return the lexically apparent definition of the function Name. Name may also
428 be a lambda expression."
431 ((lambda named-lambda instance-lambda lambda-with-lexenv)
432 (reference-leaf start
434 (ir1-convert-lambdalike
436 :debug-name (debug-namify "#'~S" thing)
437 :allow-debug-catch-tag t)))
438 ((setf sb!pcl::class-predicate sb!pcl::slot-accessor)
439 (let ((var (find-lexically-apparent-fun
440 thing "as the argument to FUNCTION")))
441 (reference-leaf start cont var)))
443 (compiler-error "~S is not a legal function name." thing)))
444 (let ((var (find-lexically-apparent-fun
445 thing "as the argument to FUNCTION")))
446 (reference-leaf start cont var))))
450 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
451 ;;; (not symbols). %FUNCALL is used directly in some places where the
452 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
453 (deftransform funcall ((function &rest args) * *)
454 (let ((arg-names (make-gensym-list (length args))))
455 `(lambda (function ,@arg-names)
456 (%funcall ,(if (csubtypep (continuation-type function)
457 (specifier-type 'function))
459 '(%coerce-callable-to-fun function))
462 (def-ir1-translator %funcall ((function &rest args) start cont)
463 (let ((fun-cont (make-continuation)))
464 (ir1-convert start fun-cont function)
465 (assert-continuation-type fun-cont (specifier-type 'function)
466 (lexenv-policy *lexenv*))
467 (ir1-convert-combination-args fun-cont cont args)))
469 ;;; This source transform exists to reduce the amount of work for the
470 ;;; compiler. If the called function is a FUNCTION form, then convert
471 ;;; directly to %FUNCALL, instead of waiting around for type
473 (define-source-transform funcall (function &rest args)
474 (if (and (consp function) (eq (car function) 'function))
475 `(%funcall ,function ,@args)
478 (deftransform %coerce-callable-to-fun ((thing) (function) *
480 "optimize away possible call to FDEFINITION at runtime"
485 ;;;; (LET and LET* can't be implemented as macros due to the fact that
486 ;;;; any pervasive declarations also affect the evaluation of the
489 ;;; Given a list of binding specifiers in the style of LET, return:
490 ;;; 1. The list of var structures for the variables bound.
491 ;;; 2. The initial value form for each variable.
493 ;;; The variable names are checked for legality and globally special
494 ;;; variables are marked as such. Context is the name of the form, for
495 ;;; error reporting purposes.
496 (declaim (ftype (function (list symbol) (values list list))
498 (defun extract-let-vars (bindings context)
502 (flet ((get-var (name)
503 (varify-lambda-arg name
504 (if (eq context 'let*)
507 (dolist (spec bindings)
509 (let ((var (get-var spec)))
514 (unless (proper-list-of-length-p spec 1 2)
515 (compiler-error "The ~S binding spec ~S is malformed."
518 (let* ((name (first spec))
519 (var (get-var name)))
522 (vals (second spec)))))))
524 (values (vars) (vals))))
526 (def-ir1-translator let ((bindings &body body)
529 "LET ({(Var [Value]) | Var}*) Declaration* Form*
530 During evaluation of the Forms, bind the Vars to the result of evaluating the
531 Value forms. The variables are bound in parallel after all of the Values are
534 (ir1-translate-locally body start cont)
535 (multiple-value-bind (forms decls) (parse-body body nil)
536 (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
537 (let ((fun-cont (make-continuation)))
538 (let* ((*lexenv* (process-decls decls vars nil cont))
539 (fun (ir1-convert-lambda-body
541 :debug-name (debug-namify "LET ~S" bindings))))
542 (reference-leaf start fun-cont fun))
543 (ir1-convert-combination-args fun-cont cont values))))))
545 (def-ir1-translator let* ((bindings &body body)
548 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
549 Similar to LET, but the variables are bound sequentially, allowing each Value
550 form to reference any of the previous Vars."
551 (multiple-value-bind (forms decls) (parse-body body nil)
552 (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
553 (let ((*lexenv* (process-decls decls vars nil cont)))
554 (ir1-convert-aux-bindings start cont forms vars values)))))
556 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
557 ;;; and SYMBOL-MACROLET
559 ;;; Note that all these things need to preserve toplevel-formness,
560 ;;; but we don't need to worry about that within an IR1 translator,
561 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
562 ;;; forms before we hit the IR1 transform level.
563 (defun ir1-translate-locally (body start cont &key vars funs)
564 (declare (type list body) (type continuation start cont))
565 (multiple-value-bind (forms decls) (parse-body body nil)
566 (let ((*lexenv* (process-decls decls vars funs cont)))
567 (ir1-convert-aux-bindings start cont forms nil nil))))
569 (def-ir1-translator locally ((&body body) start cont)
571 "LOCALLY Declaration* Form*
572 Sequentially evaluate the Forms in a lexical environment where the
573 the Declarations have effect. If LOCALLY is a top level form, then
574 the Forms are also processed as top level forms."
575 (ir1-translate-locally body start cont))
579 ;;; Given a list of local function specifications in the style of
580 ;;; FLET, return lists of the function names and of the lambdas which
581 ;;; are their definitions.
583 ;;; The function names are checked for legality. CONTEXT is the name
584 ;;; of the form, for error reporting.
585 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
586 (defun extract-flet-vars (definitions context)
589 (dolist (def definitions)
590 (when (or (atom def) (< (length def) 2))
591 (compiler-error "The ~S definition spec ~S is malformed." context def))
593 (let ((name (first def)))
594 (check-fun-name name)
596 (multiple-value-bind (forms decls) (parse-body (cddr def))
597 (defs `(lambda ,(second def)
599 (block ,(fun-name-block-name name)
601 (values (names) (defs))))
603 (def-ir1-translator flet ((definitions &body body)
606 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
607 Evaluate the Body-Forms with some local function definitions. The bindings
608 do not enclose the definitions; any use of Name in the Forms will refer to
609 the lexically apparent function definition in the enclosing environment."
610 (multiple-value-bind (forms decls) (parse-body body nil)
611 (multiple-value-bind (names defs)
612 (extract-flet-vars definitions 'flet)
613 (let* ((fvars (mapcar (lambda (n d)
614 (ir1-convert-lambda d
616 :debug-name (debug-namify
618 :allow-debug-catch-tag t))
620 (*lexenv* (make-lexenv
621 :default (process-decls decls nil fvars cont)
622 :funs (pairlis names fvars))))
623 (ir1-convert-progn-body start cont forms)))))
625 (def-ir1-translator labels ((definitions &body body) start cont)
627 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
628 Evaluate the Body-Forms with some local function definitions. The bindings
629 enclose the new definitions, so the defined functions can call themselves or
631 (multiple-value-bind (forms decls) (parse-body body nil)
632 (multiple-value-bind (names defs)
633 (extract-flet-vars definitions 'labels)
634 (let* (;; dummy LABELS functions, to be used as placeholders
635 ;; during construction of real LABELS functions
636 (placeholder-funs (mapcar (lambda (name)
639 :%debug-name (debug-namify
640 "LABELS placeholder ~S"
643 ;; (like PAIRLIS but guaranteed to preserve ordering:)
644 (placeholder-fenv (mapcar #'cons names placeholder-funs))
645 ;; the real LABELS functions, compiled in a LEXENV which
646 ;; includes the dummy LABELS functions
648 (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
649 (mapcar (lambda (name def)
650 (ir1-convert-lambda def
652 :debug-name (debug-namify
654 :allow-debug-catch-tag t))
657 ;; Modify all the references to the dummy function leaves so
658 ;; that they point to the real function leaves.
659 (loop for real-fun in real-funs and
660 placeholder-cons in placeholder-fenv do
661 (substitute-leaf real-fun (cdr placeholder-cons))
662 (setf (cdr placeholder-cons) real-fun))
665 (let ((*lexenv* (make-lexenv
666 :default (process-decls decls nil real-funs cont)
667 ;; Use a proper FENV here (not the
668 ;; placeholder used earlier) so that if the
669 ;; lexical environment is used for inline
670 ;; expansion we'll get the right functions.
671 :funs (pairlis names real-funs))))
672 (ir1-convert-progn-body start cont forms))))))
674 ;;;; the THE special operator, and friends
676 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
677 ;;; continuation that the assertion applies to, TYPE is the type
678 ;;; specifier and LEXENV is the current lexical environment. NAME is
679 ;;; the name of the declaration we are doing, for use in error
682 ;;; This is somewhat involved, since a type assertion may only be made
683 ;;; on a continuation, not on a node. We can't just set the
684 ;;; continuation asserted type and let it go at that, since there may
685 ;;; be parallel THE's for the same continuation, i.e.
690 ;;; In this case, our representation can do no better than the union
691 ;;; of these assertions. And if there is a branch with no assertion,
692 ;;; we have nothing at all. We really need to recognize scoping, since
693 ;;; we need to be able to discern between parallel assertions (which
694 ;;; we union) and nested ones (which we intersect).
696 ;;; We represent the scoping by throwing our innermost (intersected)
697 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
698 ;;; intersect our assertions together. If CONT has no uses yet, we
699 ;;; have not yet bottomed out on the first COND branch; in this case
700 ;;; we optimistically assume that this type will be the one we end up
701 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
702 ;;; than the type that we have the first time we bottom out. Later
703 ;;; THE's (or the absence thereof) can only weaken this result.
705 ;;; We make this work by getting USE-CONTINUATION to do the unioning
706 ;;; across COND branches. We can't do it here, since we don't know how
707 ;;; many branches there are going to be.
708 (defun ir1ize-the-or-values (type cont lexenv place)
709 (declare (type continuation cont) (type lexenv lexenv))
710 (let* ((atype (if (typep type 'ctype) type (compiler-values-specifier-type type)))
711 (old-atype (or (lexenv-find cont type-restrictions)
713 (old-ctype (or (lexenv-find cont weakend-type-restrictions)
715 (intersects (values-types-equal-or-intersect old-atype atype))
716 (new-atype (values-type-intersection old-atype atype))
717 (new-ctype (values-type-intersection
718 old-ctype (maybe-weaken-check atype (lexenv-policy lexenv)))))
719 (when (null (find-uses cont))
720 (setf (continuation-asserted-type cont) new-atype)
721 (setf (continuation-type-to-check cont) new-ctype))
722 (when (and (not intersects)
723 ;; FIXME: Is it really right to look at *LEXENV* here,
724 ;; instead of looking at the LEXENV argument? Why?
725 (not (policy *lexenv*
726 (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
728 "The type ~S ~A conflicts with an enclosing assertion:~% ~S"
729 (type-specifier atype)
731 (type-specifier old-atype)))
732 (make-lexenv :type-restrictions `((,cont . ,new-atype))
733 :weakend-type-restrictions `((,cont . ,new-ctype))
736 ;;; Assert that FORM evaluates to the specified type (which may be a
739 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
740 ;;; this didn't seem to expand into an assertion, at least for ALIEN
741 ;;; values. Check that SBCL doesn't have this problem.
742 (def-ir1-translator the ((type value) start cont)
743 (with-continuation-type-assertion (cont (compiler-values-specifier-type type)
744 "in THE declaration")
745 (ir1-convert start cont value)))
747 ;;; This is like the THE special form, except that it believes
748 ;;; whatever you tell it. It will never generate a type check, but
749 ;;; will cause a warning if the compiler can prove the assertion is
752 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
753 ;;; its uses's types, setting it won't work. Instead we must intersect
754 ;;; the type with the uses's DERIVED-TYPE.
755 (def-ir1-translator truly-the ((type value) start cont)
757 (declare (inline member))
758 (let ((type (compiler-values-specifier-type type))
759 (old (find-uses cont)))
760 (ir1-convert start cont value)
762 (unless (member use old :test #'eq)
763 (derive-node-type use type)))))
767 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
768 ;;; look at the global information. If the name is for a constant,
770 (def-ir1-translator setq ((&whole source &rest things) start cont)
771 (let ((len (length things)))
773 (compiler-error "odd number of args to SETQ: ~S" source))
775 (let* ((name (first things))
776 (leaf (or (lexenv-find name vars)
777 (find-free-var name))))
780 (when (constant-p leaf)
781 (compiler-error "~S is a constant and thus can't be set." name))
782 (when (lambda-var-p leaf)
783 (let ((home-lambda (continuation-home-lambda-or-null start)))
785 (pushnew leaf (lambda-calls-or-closes home-lambda))))
786 (when (lambda-var-ignorep leaf)
787 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
788 ;; requires that this be a STYLE-WARNING, not a full warning.
790 "~S is being set even though it was declared to be ignored."
792 (setq-var start cont leaf (second things)))
794 (aver (eq (car leaf) 'MACRO))
795 (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
797 (ir1-convert start cont
798 `(%set-heap-alien ',leaf ,(second things))))))
800 (do ((thing things (cddr thing)))
802 (ir1-convert-progn-body start cont (sets)))
803 (sets `(setq ,(first thing) ,(second thing))))))))
805 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
806 ;;; This should only need to be called in SETQ.
807 (defun setq-var (start cont var value)
808 (declare (type continuation start cont) (type basic-var var))
809 (let ((dest (make-continuation)))
810 (ir1-convert start dest value)
811 (assert-continuation-type dest
812 (or (lexenv-find var type-restrictions)
814 (lexenv-policy *lexenv*))
815 (let ((res (make-set :var var :value dest)))
816 (setf (continuation-dest dest) res)
817 (setf (leaf-ever-used var) t)
818 (push res (basic-var-sets var))
819 (link-node-to-previous-continuation res dest)
820 (use-continuation res cont))))
822 ;;;; CATCH, THROW and UNWIND-PROTECT
824 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
825 ;;; since as as far as IR1 is concerned, it has no interesting
826 ;;; properties other than receiving multiple-values.
827 (def-ir1-translator throw ((tag result) start cont)
830 Do a non-local exit, return the values of Form from the CATCH whose tag
831 evaluates to the same thing as Tag."
832 (ir1-convert start cont
833 `(multiple-value-call #'%throw ,tag ,result)))
835 ;;; This is a special special form used to instantiate a cleanup as
836 ;;; the current cleanup within the body. KIND is the kind of cleanup
837 ;;; to make, and MESS-UP is a form that does the mess-up action. We
838 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
839 ;;; and introduce the cleanup into the lexical environment. We
840 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
841 ;;; cleanup, since this inner cleanup is the interesting one.
842 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
843 (let ((dummy (make-continuation))
844 (dummy2 (make-continuation)))
845 (ir1-convert start dummy mess-up)
846 (let* ((mess-node (continuation-use dummy))
847 (cleanup (make-cleanup :kind kind
849 (old-cup (lexenv-cleanup *lexenv*))
850 (*lexenv* (make-lexenv :cleanup cleanup)))
851 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
852 (ir1-convert dummy dummy2 '(%cleanup-point))
853 (ir1-convert-progn-body dummy2 cont body))))
855 ;;; This is a special special form that makes an "escape function"
856 ;;; which returns unknown values from named block. We convert the
857 ;;; function, set its kind to :ESCAPE, and then reference it. The
858 ;;; :ESCAPE kind indicates that this function's purpose is to
859 ;;; represent a non-local control transfer, and that it might not
860 ;;; actually have to be compiled.
862 ;;; Note that environment analysis replaces references to escape
863 ;;; functions with references to the corresponding NLX-INFO structure.
864 (def-ir1-translator %escape-fun ((tag) start cont)
865 (let ((fun (ir1-convert-lambda
867 (return-from ,tag (%unknown-values)))
868 :debug-name (debug-namify "escape function for ~S" tag))))
869 (setf (functional-kind fun) :escape)
870 (reference-leaf start cont fun)))
872 ;;; Yet another special special form. This one looks up a local
873 ;;; function and smashes it to a :CLEANUP function, as well as
875 (def-ir1-translator %cleanup-fun ((name) start cont)
876 (let ((fun (lexenv-find name funs)))
877 (aver (lambda-p fun))
878 (setf (functional-kind fun) :cleanup)
879 (reference-leaf start cont fun)))
881 ;;; We represent the possibility of the control transfer by making an
882 ;;; "escape function" that does a lexical exit, and instantiate the
883 ;;; cleanup using %WITHIN-CLEANUP.
884 (def-ir1-translator catch ((tag &body body) start cont)
887 Evaluates Tag and instantiates it as a catcher while the body forms are
888 evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
889 scope of the body, then control will be transferred to the end of the body
890 and the thrown values will be returned."
893 (let ((exit-block (gensym "EXIT-BLOCK-")))
897 (%catch (%escape-fun ,exit-block) ,tag)
900 ;;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
901 ;;; cleanup forms into a local function so that they can be referenced
902 ;;; both in the case where we are unwound and in any local exits. We
903 ;;; use %CLEANUP-FUN on this to indicate that reference by
904 ;;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
906 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
908 "Unwind-Protect Protected Cleanup*
909 Evaluate the form Protected, returning its values. The cleanup forms are
910 evaluated whenever the dynamic scope of the Protected form is exited (either
911 due to normal completion or a non-local exit such as THROW)."
914 (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
915 (drop-thru-tag (gensym "DROP-THRU-TAG-"))
916 (exit-tag (gensym "EXIT-TAG-"))
917 (next (gensym "NEXT"))
918 (start (gensym "START"))
919 (count (gensym "COUNT")))
920 `(flet ((,cleanup-fun () ,@cleanup nil))
921 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
922 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
923 ;; and something can be done to make %ESCAPE-FUN have
924 ;; dynamic extent too.
925 (block ,drop-thru-tag
926 (multiple-value-bind (,next ,start ,count)
930 (%unwind-protect (%escape-fun ,exit-tag)
931 (%cleanup-fun ,cleanup-fun))
932 (return-from ,drop-thru-tag ,protected)))
934 (%continue-unwind ,next ,start ,count)))))))
936 ;;;; multiple-value stuff
938 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
941 ;;; If there are no arguments, then we convert to a normal
942 ;;; combination, ensuring that a MV-COMBINATION always has at least
943 ;;; one argument. This can be regarded as an optimization, but it is
944 ;;; more important for simplifying compilation of MV-COMBINATIONS.
945 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
947 "MULTIPLE-VALUE-CALL Function Values-Form*
948 Call Function, passing all the values of each Values-Form as arguments,
949 values from the first Values-Form making up the first argument, etc."
950 (let* ((fun-cont (make-continuation))
952 (make-mv-combination fun-cont)
953 (make-combination fun-cont))))
954 (ir1-convert start fun-cont
955 (if (and (consp fun) (eq (car fun) 'function))
957 `(%coerce-callable-to-fun ,fun)))
958 (setf (continuation-dest fun-cont) node)
959 (collect ((arg-conts))
960 (let ((this-start fun-cont))
962 (let ((this-cont (make-continuation node)))
963 (ir1-convert this-start this-cont arg)
964 (setq this-start this-cont)
965 (arg-conts this-cont)))
966 (link-node-to-previous-continuation node this-start)
967 (use-continuation node cont)
968 (setf (basic-combination-args node) (arg-conts))))))
970 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
971 ;;; the result code use result continuation (CONT), but transfer
972 ;;; control to the evaluation of the body. In other words, the result
973 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
976 ;;; In order to get the control flow right, we convert the result with
977 ;;; a dummy result continuation, then convert all the uses of the
978 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
979 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
980 ;;; that they are consistent. Note that this doesn't amount to
981 ;;; changing the exit target, since the control destination of an exit
982 ;;; is determined by the block successor; we are just indicating the
983 ;;; continuation that the result is delivered to.
985 ;;; We then convert the body, using another dummy continuation in its
986 ;;; own block as the result. After we are done converting the body, we
987 ;;; move all predecessors of the dummy end block to CONT's block.
989 ;;; Note that we both exploit and maintain the invariant that the CONT
990 ;;; to an IR1 convert method either has no block or starts the block
991 ;;; that control should transfer to after completion for the form.
992 ;;; Nested MV-PROG1's work because during conversion of the result
993 ;;; form, we use dummy continuation whose block is the true control
995 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
997 "MULTIPLE-VALUE-PROG1 Values-Form Form*
998 Evaluate Values-Form and then the Forms, but return all the values of
1000 (continuation-starts-block cont)
1001 (let* ((dummy-result (make-continuation))
1002 (dummy-start (make-continuation))
1003 (cont-block (continuation-block cont)))
1004 (continuation-starts-block dummy-start)
1005 (ir1-convert start dummy-start result)
1007 (with-continuation-type-assertion
1009 (cont (continuation-asserted-type dummy-start)
1010 "of the first form")
1011 (substitute-continuation-uses cont dummy-start))
1013 (continuation-starts-block dummy-result)
1014 (ir1-convert-progn-body dummy-start dummy-result forms)
1015 (let ((end-block (continuation-block dummy-result)))
1016 (dolist (pred (block-pred end-block))
1017 (unlink-blocks pred end-block)
1018 (link-blocks pred cont-block))
1019 (aver (not (continuation-dest dummy-result)))
1020 (delete-continuation dummy-result)
1021 (remove-from-dfo end-block))))
1023 ;;;; interface to defining macros
1025 ;;; Old CMUCL comment:
1027 ;;; Return a new source path with any stuff intervening between the
1028 ;;; current path and the first form beginning with NAME stripped
1029 ;;; off. This is used to hide the guts of DEFmumble macros to
1030 ;;; prevent annoying error messages.
1032 ;;; Now that we have implementations of DEFmumble macros in terms of
1033 ;;; EVAL-WHEN, this function is no longer used. However, it might be
1034 ;;; worth figuring out why it was used, and maybe doing analogous
1035 ;;; munging to the functions created in the expanders for the macros.
1036 (defun revert-source-path (name)
1037 (do ((path *current-path* (cdr path)))
1038 ((null path) *current-path*)
1039 (let ((first (first path)))
1040 (when (or (eq first name)
1041 (eq first 'original-source-start))