cd10e15e0538cf7628264d4ebf85ebb350e3457a
[sbcl.git] / src / compiler / ir1-translators.lisp
1 ;;;; the usual place for DEF-IR1-TRANSLATOR forms (and their
2 ;;;; close personal friends)
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!C")
14 \f
15 ;;;; special forms for control
16
17 (def-ir1-translator progn ((&rest forms) start next result)
18   #!+sb-doc
19   "PROGN form*
20
21 Evaluates each FORM in order, returning the values of the last form. With no
22 forms, returns NIL."
23   (ir1-convert-progn-body start next result forms))
24
25 (def-ir1-translator if ((test then &optional else) start next result)
26   #!+sb-doc
27   "IF predicate then [else]
28
29 If PREDICATE evaluates to true, evaluate THEN and return its values,
30 otherwise evaluate ELSE and return its values. ELSE defaults to NIL."
31   (let* ((pred-ctran (make-ctran))
32          (pred-lvar (make-lvar))
33          (then-ctran (make-ctran))
34          (then-block (ctran-starts-block then-ctran))
35          (else-ctran (make-ctran))
36          (else-block (ctran-starts-block else-ctran))
37          (maybe-instrument *instrument-if-for-code-coverage*)
38          (*instrument-if-for-code-coverage* t)
39          (node (make-if :test pred-lvar
40                         :consequent then-block
41                         :alternative else-block)))
42     ;; IR1-CONVERT-MAYBE-PREDICATE requires DEST to be CIF, so the
43     ;; order of the following two forms is important
44     (setf (lvar-dest pred-lvar) node)
45     (ir1-convert start pred-ctran pred-lvar test)
46     (link-node-to-previous-ctran node pred-ctran)
47
48     (let ((start-block (ctran-block pred-ctran)))
49       (setf (block-last start-block) node)
50       (ctran-starts-block next)
51
52       (link-blocks start-block then-block)
53       (link-blocks start-block else-block))
54
55     (let ((path (best-sub-source-path test)))
56       (ir1-convert (if (and path maybe-instrument)
57                        (let ((*current-path* path))
58                          (instrument-coverage then-ctran :then test))
59                        then-ctran)
60                    next result then)
61       (ir1-convert (if (and path maybe-instrument)
62                        (let ((*current-path* path))
63                          (instrument-coverage else-ctran :else test))
64                        else-ctran)
65                    next result else))))
66
67 ;;; To get even remotely sensible results for branch coverage
68 ;;; tracking, we need good source paths. If the macroexpansions
69 ;;; interfere enough the TEST of the conditional doesn't actually have
70 ;;; an original source location (e.g. (UNLESS FOO ...) -> (IF (NOT
71 ;;; FOO) ...). Look through the form, and try to find some subform
72 ;;; that has one.
73 (defun best-sub-source-path (form)
74   (if (policy *lexenv* (= store-coverage-data 0))
75       nil
76       (labels ((sub (form)
77                  (or (get-source-path form)
78                      (when (consp form)
79                        (unless (eq 'quote (car form))
80                          (somesub form)))))
81                (somesub (forms)
82                  (when (consp forms)
83                    (or (sub (car forms))
84                        (somesub (cdr forms))))))
85         (sub form))))
86 \f
87 ;;;; BLOCK and TAGBODY
88
89 ;;;; We make an ENTRY node to mark the start and a :ENTRY cleanup to
90 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an EXIT
91 ;;;; node.
92
93 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
94 ;;; body in the modified environment. We make NEXT start a block now,
95 ;;; since if it was done later, the block would be in the wrong
96 ;;; environment.
97 (def-ir1-translator block ((name &rest forms) start next result)
98   #!+sb-doc
99   "BLOCK name form*
100
101 Evaluate the FORMS as a PROGN. Within the lexical scope of the body,
102 RETURN-FROM can be used to exit the form."
103   (unless (symbolp name)
104     (compiler-error "The block name ~S is not a symbol." name))
105   (start-block start)
106   (ctran-starts-block next)
107   (let* ((dummy (make-ctran))
108          (entry (make-entry))
109          (cleanup (make-cleanup :kind :block
110                                 :mess-up entry)))
111     (push entry (lambda-entries (lexenv-lambda *lexenv*)))
112     (setf (entry-cleanup entry) cleanup)
113     (link-node-to-previous-ctran entry start)
114     (use-ctran entry dummy)
115
116     (let* ((env-entry (list entry next result))
117            (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
118                                   :cleanup cleanup)))
119       (ir1-convert-progn-body dummy next result forms))))
120
121 (def-ir1-translator return-from ((name &optional value) start next result)
122   #!+sb-doc
123   "RETURN-FROM block-name value-form
124
125 Evaluate the VALUE-FORM, returning its values from the lexically enclosing
126 block BLOCK-NAME. This is constrained to be used only within the dynamic
127 extent of the block."
128   ;; old comment:
129   ;;   We make NEXT start a block just so that it will have a block
130   ;;   assigned. People assume that when they pass a ctran into
131   ;;   IR1-CONVERT as NEXT, it will have a block when it is done.
132   ;; KLUDGE: Note that this block is basically fictitious. In the code
133   ;;   (BLOCK B (RETURN-FROM B) (SETQ X 3))
134   ;; it's the block which answers the question "which block is
135   ;; the (SETQ X 3) in?" when the right answer is that (SETQ X 3) is
136   ;; dead code and so doesn't really have a block at all. The existence
137   ;; of this block, and that way that it doesn't explicitly say
138   ;; "I'm actually nowhere at all" makes some logic (e.g.
139   ;; BLOCK-HOME-LAMBDA-OR-NULL) more obscure, and it might be better
140   ;; to get rid of it, perhaps using a special placeholder value
141   ;; to indicate the orphanedness of the code.
142   (declare (ignore result))
143   (ctran-starts-block next)
144   (let* ((found (or (lexenv-find name blocks)
145                     (compiler-error "return for unknown block: ~S" name)))
146          (exit-ctran (second found))
147          (value-ctran (make-ctran))
148          (value-lvar (make-lvar))
149          (entry (first found))
150          (exit (make-exit :entry entry
151                           :value value-lvar)))
152     (when (ctran-deleted-p exit-ctran)
153       (throw 'locall-already-let-converted exit-ctran))
154     (push exit (entry-exits entry))
155     (setf (lvar-dest value-lvar) exit)
156     (ir1-convert start value-ctran value-lvar value)
157     (link-node-to-previous-ctran exit value-ctran)
158     (let ((home-lambda (ctran-home-lambda-or-null start)))
159       (when home-lambda
160         (sset-adjoin entry (lambda-calls-or-closes home-lambda))))
161     (use-continuation exit exit-ctran (third found))))
162
163 ;;; Return a list of the segments of a TAGBODY. Each segment looks
164 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
165 ;;; tagbody into segments of non-tag statements, and explicitly
166 ;;; represent the drop-through with a GO. The first segment has a
167 ;;; dummy NIL tag, since it represents code before the first tag. Note
168 ;;; however that NIL may appear as the tag of an inner segment. The
169 ;;; last segment (which may also be the first segment) ends in NIL
170 ;;; rather than a GO.
171 (defun parse-tagbody (body)
172   (declare (list body))
173   (collect ((tags)
174             (segments))
175     (let ((current body))
176       (loop
177        (let ((next-segment (member-if #'atom current)))
178          (unless next-segment
179            (segments `(,@current nil))
180            (return))
181          (let ((tag (car next-segment)))
182            (when (member tag (tags))
183              (compiler-error
184               "The tag ~S appears more than once in a tagbody."
185               tag))
186            (unless (or (symbolp tag) (integerp tag))
187              (compiler-error "~S is not a legal go tag." tag))
188            (tags tag)
189            (segments `(,@(ldiff current next-segment) (go ,tag))))
190          (setq current (rest next-segment))))
191       (mapcar #'cons (cons nil (tags)) (segments)))))
192
193 ;;; Set up the cleanup, emitting the entry node. Then make a block for
194 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
195 ;;; Finally, convert each segment with the precomputed Start and Cont
196 ;;; values.
197 (def-ir1-translator tagbody ((&rest statements) start next result)
198   #!+sb-doc
199   "TAGBODY {tag | statement}*
200
201 Define tags for use with GO. The STATEMENTS are evaluated in order, skipping
202 TAGS, and NIL is returned. If a statement contains a GO to a defined TAG
203 within the lexical scope of the form, then control is transferred to the next
204 statement following that tag. A TAG must be an integer or a symbol. A
205 STATEMENT must be a list. Other objects are illegal within the body."
206   (start-block start)
207   (ctran-starts-block next)
208   (let* ((dummy (make-ctran))
209          (entry (make-entry))
210          (segments (parse-tagbody statements))
211          (cleanup (make-cleanup :kind :tagbody
212                                 :mess-up entry)))
213     (push entry (lambda-entries (lexenv-lambda *lexenv*)))
214     (setf (entry-cleanup entry) cleanup)
215     (link-node-to-previous-ctran entry start)
216     (use-ctran entry dummy)
217
218     (collect ((tags)
219               (starts)
220               (ctrans))
221       (starts dummy)
222       (dolist (segment (rest segments))
223         (let* ((tag-ctran (make-ctran))
224                (tag (list (car segment) entry tag-ctran)))
225           (ctrans tag-ctran)
226           (starts tag-ctran)
227           (ctran-starts-block tag-ctran)
228           (tags tag)))
229       (ctrans next)
230
231       (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
232         (mapc (lambda (segment start end)
233                 (ir1-convert-progn-body start end
234                                         (when (eq end next) result)
235                                         (rest segment)))
236               segments (starts) (ctrans))))))
237
238 ;;; Emit an EXIT node without any value.
239 (def-ir1-translator go ((tag) start next result)
240   #!+sb-doc
241   "GO tag
242
243 Transfer control to the named TAG in the lexically enclosing TAGBODY. This is
244 constrained to be used only within the dynamic extent of the TAGBODY."
245   (ctran-starts-block next)
246   (let* ((found (or (lexenv-find tag tags :test #'eql)
247                     (compiler-error "attempt to GO to nonexistent tag: ~S"
248                                     tag)))
249          (entry (first found))
250          (exit (make-exit :entry entry)))
251     (push exit (entry-exits entry))
252     (link-node-to-previous-ctran exit start)
253     (let ((home-lambda (ctran-home-lambda-or-null start)))
254       (when home-lambda
255         (sset-adjoin entry (lambda-calls-or-closes home-lambda))))
256     (use-ctran exit (second found))))
257 \f
258 ;;;; translators for compiler-magic special forms
259
260 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
261 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
262 ;;; so that they're never seen at this level.)
263 ;;;
264 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
265 ;;; of non-top-level EVAL-WHENs is very simple:
266 ;;;   EVAL-WHEN forms cause compile-time evaluation only at top level.
267 ;;;   Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
268 ;;;   are ignored for non-top-level forms. For non-top-level forms, an
269 ;;;   eval-when specifying the :EXECUTE situation is treated as an
270 ;;;   implicit PROGN including the forms in the body of the EVAL-WHEN
271 ;;;   form; otherwise, the forms in the body are ignored.
272 (def-ir1-translator eval-when ((situations &rest forms) start next result)
273   #!+sb-doc
274   "EVAL-WHEN (situation*) form*
275
276 Evaluate the FORMS in the specified SITUATIONS (any of :COMPILE-TOPLEVEL,
277 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
278   (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
279     (declare (ignore ct lt))
280     (ir1-convert-progn-body start next result (and e forms)))
281   (values))
282
283 ;;; common logic for MACROLET and SYMBOL-MACROLET
284 ;;;
285 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
286 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
287 ;;; call FUN (with no arguments).
288 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
289                                        definitionize-keyword
290                                        definitions
291                                        fun)
292   (declare (type function definitionize-fun fun))
293   (declare (type (member :vars :funs) definitionize-keyword))
294   (declare (type list definitions))
295   (unless (= (length definitions)
296              (length (remove-duplicates definitions :key #'first)))
297     (compiler-style-warn "duplicate definitions in ~S" definitions))
298   (let* ((processed-definitions (mapcar definitionize-fun definitions))
299          (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
300     ;; I wonder how much of an compiler performance penalty this
301     ;; non-constant keyword is.
302     (funcall fun definitionize-keyword processed-definitions)))
303
304 ;;; Tweak LEXENV to include the DEFINITIONS from a MACROLET, then
305 ;;; call FUN (with no arguments).
306 ;;;
307 ;;; This is split off from the IR1 convert method so that it can be
308 ;;; shared by the special-case top level MACROLET processing code, and
309 ;;; further split so that the special-case MACROLET processing code in
310 ;;; EVAL can likewise make use of it.
311 (defun macrolet-definitionize-fun (context lexenv)
312   (flet ((fail (control &rest args)
313            (ecase context
314              (:compile (apply #'compiler-error control args))
315              (:eval (error 'simple-program-error
316                            :format-control control
317                            :format-arguments args)))))
318     (lambda (definition)
319       (unless (list-of-length-at-least-p definition 2)
320         (fail "The list ~S is too short to be a legal local macro definition."
321               definition))
322       (destructuring-bind (name arglist &body body) definition
323         (unless (symbolp name)
324           (fail "The local macro name ~S is not a symbol." name))
325         (when (fboundp name)
326           (program-assert-symbol-home-package-unlocked
327            context name "binding ~A as a local macro"))
328         (unless (listp arglist)
329           (fail "The local macro argument list ~S is not a list."
330                 arglist))
331         (with-unique-names (whole environment)
332           (multiple-value-bind (body local-decls)
333               (parse-defmacro arglist whole body name 'macrolet
334                               :environment environment)
335             `(,name macro .
336                     ,(compile-in-lexenv
337                       nil
338                       `(lambda (,whole ,environment)
339                          ,@local-decls
340                          ,body)
341                       lexenv))))))))
342
343 (defun funcall-in-macrolet-lexenv (definitions fun context)
344   (%funcall-in-foomacrolet-lexenv
345    (macrolet-definitionize-fun context (make-restricted-lexenv *lexenv*))
346    :funs
347    definitions
348    fun))
349
350 (def-ir1-translator macrolet ((definitions &rest body) start next result)
351   #!+sb-doc
352   "MACROLET ({(name lambda-list form*)}*) body-form*
353
354 Evaluate the BODY-FORMS in an environment with the specified local macros
355 defined. NAME is the local macro name, LAMBDA-LIST is a DEFMACRO style
356 destructuring lambda list, and the FORMS evaluate to the expansion."
357   (funcall-in-macrolet-lexenv
358    definitions
359    (lambda (&key funs)
360      (declare (ignore funs))
361      (ir1-translate-locally body start next result))
362    :compile))
363
364 (defun symbol-macrolet-definitionize-fun (context)
365   (flet ((fail (control &rest args)
366            (ecase context
367              (:compile (apply #'compiler-error control args))
368              (:eval (error 'simple-program-error
369                            :format-control control
370                            :format-arguments args)))))
371     (lambda (definition)
372       (unless (proper-list-of-length-p definition 2)
373         (fail "malformed symbol/expansion pair: ~S" definition))
374       (destructuring-bind (name expansion) definition
375         (unless (symbolp name)
376           (fail "The local symbol macro name ~S is not a symbol." name))
377         (when (or (boundp name) (eq (info :variable :kind name) :macro))
378           (program-assert-symbol-home-package-unlocked
379            context name "binding ~A as a local symbol-macro"))
380         (let ((kind (info :variable :kind name)))
381           (when (member kind '(:special :constant :global))
382             (fail "Attempt to bind a ~(~A~) variable with SYMBOL-MACROLET: ~S"
383                   kind name)))
384         ;; A magical cons that MACROEXPAND-1 understands.
385         `(,name . (macro . ,expansion))))))
386
387 (defun funcall-in-symbol-macrolet-lexenv (definitions fun context)
388   (%funcall-in-foomacrolet-lexenv
389    (symbol-macrolet-definitionize-fun context)
390    :vars
391    definitions
392    fun))
393
394 (def-ir1-translator symbol-macrolet
395     ((macrobindings &body body) start next result)
396   #!+sb-doc
397   "SYMBOL-MACROLET ({(name expansion)}*) decl* form*
398
399 Define the NAMES as symbol macros with the given EXPANSIONS. Within the
400 body, references to a NAME will effectively be replaced with the EXPANSION."
401   (funcall-in-symbol-macrolet-lexenv
402    macrobindings
403    (lambda (&key vars)
404      (ir1-translate-locally body start next result :vars vars))
405    :compile))
406 \f
407 ;;;; %PRIMITIVE
408 ;;;;
409 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
410 ;;;; into a funny function.
411
412 ;;; Carefully evaluate a list of forms, returning a list of the results.
413 (defun eval-info-args (args)
414   (declare (list args))
415   (handler-case (mapcar #'eval args)
416     (error (condition)
417       (compiler-error "Lisp error during evaluation of info args:~%~A"
418                       condition))))
419
420 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
421 ;;; the template, the second is a list of the results of any
422 ;;; codegen-info args, and the remaining arguments are the runtime
423 ;;; arguments.
424 ;;;
425 ;;; We do various error checking now so that we don't bomb out with
426 ;;; a fatal error during IR2 conversion.
427 ;;;
428 ;;; KLUDGE: It's confusing having multiple names floating around for
429 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
430 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
431 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
432 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
433 ;;; VOP or %VOP.. -- WHN 2001-06-11
434 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
435 (def-ir1-translator %primitive ((name &rest args) start next result)
436   (declare (type symbol name))
437   (let* ((template (or (gethash name *backend-template-names*)
438                        (bug "undefined primitive ~A" name)))
439          (required (length (template-arg-types template)))
440          (info (template-info-arg-count template))
441          (min (+ required info))
442          (nargs (length args)))
443     (if (template-more-args-type template)
444         (when (< nargs min)
445           (bug "Primitive ~A was called with ~R argument~:P, ~
446                 but wants at least ~R."
447                name
448                nargs
449                min))
450         (unless (= nargs min)
451           (bug "Primitive ~A was called with ~R argument~:P, ~
452                 but wants exactly ~R."
453                name
454                nargs
455                min)))
456
457     (when (template-conditional-p template)
458       (bug "%PRIMITIVE was used with a conditional template."))
459
460     (when (template-more-results-type template)
461       (bug "%PRIMITIVE was used with an unknown values template."))
462
463     (ir1-convert start next result
464                  `(%%primitive ',template
465                                ',(eval-info-args
466                                   (subseq args required min))
467                                ,@(subseq args 0 required)
468                                ,@(subseq args min)))))
469 \f
470 ;;;; QUOTE
471
472 (def-ir1-translator quote ((thing) start next result)
473   #!+sb-doc
474   "QUOTE value
475
476 Return VALUE without evaluating it."
477   (reference-constant start next result thing))
478 \f
479 (defun name-context ()
480   ;; Name of the outermost non-NIL BLOCK, or the source namestring
481   ;; of the source file.
482   (let ((context
483           (or (car (find-if (lambda (b)
484                               (let ((name (pop b)))
485                                 (and name
486                                      ;; KLUDGE: High debug adds this block on
487                                      ;; some platforms.
488                                      #!-unwind-to-frame-and-call-vop
489                                      (neq 'return-value-tag name)
490                                      ;; KLUDGE: CATCH produces blocks whose
491                                      ;; cleanup is :CATCH.
492                                      (neq :catch (cleanup-kind (entry-cleanup (pop b)))))))
493                             (lexenv-blocks *lexenv*) :from-end t))
494               *source-namestring*
495               (let ((p (or *compile-file-truename* *load-truename*)))
496                 (when p (namestring p))))))
497     (when context
498       (list :in context))))
499
500 ;;;; FUNCTION and NAMED-LAMBDA
501 (defun name-lambdalike (thing)
502   (case (car thing)
503     ((named-lambda)
504      (or (second thing)
505          `(lambda ,(third thing) ,(name-context))))
506     ((lambda)
507      `(lambda ,(second thing) ,@(name-context)))
508     ((lambda-with-lexenv)
509      ;; FIXME: Get the original DEFUN name here.
510      `(lambda ,(fifth thing)))
511     (otherwise
512      (compiler-error "Not a valid lambda expression:~%  ~S"
513                      thing))))
514
515 (defun fun-name-leaf (thing)
516   (if (consp thing)
517       (cond
518         ((member (car thing)
519                  '(lambda named-lambda lambda-with-lexenv))
520          (values (ir1-convert-lambdalike
521                   thing
522                   :debug-name (name-lambdalike thing))
523                  t))
524         ((legal-fun-name-p thing)
525          (values (find-lexically-apparent-fun
526                   thing "as the argument to FUNCTION")
527                  nil))
528         (t
529          (compiler-error "~S is not a legal function name." thing)))
530       (values (find-lexically-apparent-fun
531                thing "as the argument to FUNCTION")
532               nil)))
533
534 (def-ir1-translator %%allocate-closures ((&rest leaves) start next result)
535   (aver (eq result 'nil))
536   (let ((lambdas leaves))
537     (ir1-convert start next result `(%allocate-closures ',lambdas))
538     (let ((allocator (node-dest (ctran-next start))))
539       (dolist (lambda lambdas)
540         (setf (functional-allocator lambda) allocator)))))
541
542 (defmacro with-fun-name-leaf ((leaf thing start &key global-function) &body body)
543   `(multiple-value-bind (,leaf allocate-p)
544        (if ,global-function
545            (find-global-fun ,thing t)
546            (fun-name-leaf ,thing))
547      (if allocate-p
548          (let ((.new-start. (make-ctran)))
549            (ir1-convert ,start .new-start. nil `(%%allocate-closures ,leaf))
550            (let ((,start .new-start.))
551              ,@body))
552          (locally
553              ,@body))))
554
555 (def-ir1-translator function ((thing) start next result)
556   #!+sb-doc
557   "FUNCTION name
558
559 Return the lexically apparent definition of the function NAME. NAME may also
560 be a lambda expression."
561   (with-fun-name-leaf (leaf thing start)
562     (reference-leaf start next result leaf)))
563
564 ;;; Like FUNCTION, but ignores local definitions and inline
565 ;;; expansions, and doesn't nag about undefined functions.
566 ;;; Used for optimizing things like (FUNCALL 'FOO).
567 (def-ir1-translator global-function ((thing) start next result)
568   (with-fun-name-leaf (leaf thing start :global-function t)
569     (reference-leaf start next result leaf)))
570
571 (defun constant-global-fun-name (thing)
572   (let ((constantp (sb!xc:constantp thing)))
573     (when constantp
574       (let ((name (constant-form-value thing)))
575         (when (legal-fun-name-p name)
576           name)))))
577
578 (defun lvar-constant-global-fun-name (lvar)
579   (when (constant-lvar-p lvar)
580     (let ((name (lvar-value lvar)))
581       (when (legal-fun-name-p name)
582         name))))
583
584 (defun ensure-source-fun-form (source &optional give-up)
585   (let ((op (when (consp source) (car source))))
586     (cond ((eq op '%coerce-callable-to-fun)
587            (ensure-source-fun-form (second source)))
588           ((member op '(function global-function lambda named-lambda))
589            (values source nil))
590           (t
591            (let ((cname (constant-global-fun-name source)))
592              (if cname
593                  (values `(global-function ,cname) nil)
594                  (values `(%coerce-callable-to-fun ,source) give-up)))))))
595
596 (defun ensure-lvar-fun-form (lvar lvar-name &optional give-up)
597   (aver (and lvar-name (symbolp lvar-name)))
598   (if (csubtypep (lvar-type lvar) (specifier-type 'function))
599       lvar-name
600       (let ((cname (lvar-constant-global-fun-name lvar)))
601         (cond (cname
602                `(global-function ,cname))
603               (give-up
604                (give-up-ir1-transform give-up))
605               (t
606                `(%coerce-callable-to-fun ,lvar-name))))))
607 \f
608 ;;;; FUNCALL
609
610 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
611 ;;; (not symbols). %FUNCALL is used directly in some places where the
612 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
613 (deftransform funcall ((function &rest args) * *)
614   (let ((arg-names (make-gensym-list (length args))))
615     `(lambda (function ,@arg-names)
616        (declare (ignorable function))
617        `(%funcall ,(ensure-lvar-fun-form function 'function) ,@arg-names))))
618
619 (def-ir1-translator %funcall ((function &rest args) start next result)
620   ;; MACROEXPAND so that (LAMBDA ...) forms arriving here don't get an
621   ;; extra cast inserted for them.
622   (let* ((function (%macroexpand function *lexenv*))
623          (op (when (consp function) (car function))))
624     (cond ((eq op 'function)
625            (compiler-destructuring-bind (thing) (cdr function)
626                function
627              (with-fun-name-leaf (leaf thing start)
628                (ir1-convert start next result `(,leaf ,@args)))))
629           ((eq op 'global-function)
630            (compiler-destructuring-bind (thing) (cdr function)
631                global-function
632              (with-fun-name-leaf (leaf thing start :global-function t)
633                (ir1-convert start next result `(,leaf ,@args)))))
634           (t
635            (let ((ctran (make-ctran))
636                  (fun-lvar (make-lvar)))
637              (ir1-convert start ctran fun-lvar `(the function ,function))
638              (ir1-convert-combination-args fun-lvar ctran next result args))))))
639
640 ;;; This source transform exists to reduce the amount of work for the
641 ;;; compiler. If the called function is a FUNCTION form, then convert
642 ;;; directly to %FUNCALL, instead of waiting around for type
643 ;;; inference.
644 (define-source-transform funcall (function &rest args)
645   `(%funcall ,(ensure-source-fun-form function) ,@args))
646
647 (deftransform %coerce-callable-to-fun ((thing) * *)
648   (ensure-lvar-fun-form thing 'thing "optimize away possible call to FDEFINITION at runtime"))
649
650 (define-source-transform %coerce-callable-to-fun (thing)
651   (ensure-source-fun-form thing t))
652 \f
653 ;;;; LET and LET*
654 ;;;;
655 ;;;; (LET and LET* can't be implemented as macros due to the fact that
656 ;;;; any pervasive declarations also affect the evaluation of the
657 ;;;; arguments.)
658
659 ;;; Given a list of binding specifiers in the style of LET, return:
660 ;;;  1. The list of var structures for the variables bound.
661 ;;;  2. The initial value form for each variable.
662 ;;;
663 ;;; The variable names are checked for legality and globally special
664 ;;; variables are marked as such. Context is the name of the form, for
665 ;;; error reporting purposes.
666 (declaim (ftype (function (list symbol) (values list list))
667                 extract-let-vars))
668 (defun extract-let-vars (bindings context)
669   (collect ((vars)
670             (vals)
671             (names))
672     (flet ((get-var (name)
673              (varify-lambda-arg name
674                                 (if (eq context 'let*)
675                                     nil
676                                     (names))
677                                 context)))
678       (dolist (spec bindings)
679         (cond ((atom spec)
680                (let ((var (get-var spec)))
681                  (vars var)
682                  (names spec)
683                  (vals nil)))
684               (t
685                (unless (proper-list-of-length-p spec 1 2)
686                  (compiler-error "The ~S binding spec ~S is malformed."
687                                  context
688                                  spec))
689                (let* ((name (first spec))
690                       (var (get-var name)))
691                  (vars var)
692                  (names name)
693                  (vals (second spec)))))))
694     (dolist (name (names))
695       (when (eq (info :variable :kind name) :macro)
696         (program-assert-symbol-home-package-unlocked
697          :compile name "lexically binding symbol-macro ~A")))
698     (values (vars) (vals))))
699
700 (def-ir1-translator let ((bindings &body body) start next result)
701   #!+sb-doc
702   "LET ({(var [value]) | var}*) declaration* form*
703
704 During evaluation of the FORMS, bind the VARS to the result of evaluating the
705 VALUE forms. The variables are bound in parallel after all of the VALUES forms
706 have been evaluated."
707   (cond ((null bindings)
708          (ir1-translate-locally body start next result))
709         ((listp bindings)
710          (multiple-value-bind (forms decls)
711              (parse-body body :doc-string-allowed nil)
712            (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
713              (binding* ((ctran (make-ctran))
714                         (fun-lvar (make-lvar))
715                         ((next result)
716                          (processing-decls (decls vars nil next result
717                                                   post-binding-lexenv)
718                            (let ((fun (ir1-convert-lambda-body
719                                        forms
720                                        vars
721                                        :post-binding-lexenv post-binding-lexenv
722                                        :debug-name (debug-name 'let bindings))))
723                              (reference-leaf start ctran fun-lvar fun))
724                            (values next result))))
725                (ir1-convert-combination-args fun-lvar ctran next result values)))))
726         (t
727          (compiler-error "Malformed LET bindings: ~S." bindings))))
728
729 (def-ir1-translator let* ((bindings &body body)
730                           start next result)
731   #!+sb-doc
732   "LET* ({(var [value]) | var}*) declaration* form*
733
734 Similar to LET, but the variables are bound sequentially, allowing each VALUE
735 form to reference any of the previous VARS."
736   (if (listp bindings)
737       (multiple-value-bind (forms decls)
738           (parse-body body :doc-string-allowed nil)
739         (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
740           (processing-decls (decls vars nil next result post-binding-lexenv)
741             (ir1-convert-aux-bindings start
742                                       next
743                                       result
744                                       forms
745                                       vars
746                                       values
747                                       post-binding-lexenv))))
748       (compiler-error "Malformed LET* bindings: ~S." bindings)))
749
750 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
751 ;;; and SYMBOL-MACROLET
752 ;;;
753 ;;; Note that all these things need to preserve toplevel-formness,
754 ;;; but we don't need to worry about that within an IR1 translator,
755 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
756 ;;; forms before we hit the IR1 transform level.
757 (defun ir1-translate-locally (body start next result &key vars funs)
758   (declare (type ctran start next) (type (or lvar null) result)
759            (type list body))
760   (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
761     (processing-decls (decls vars funs next result)
762       (ir1-convert-progn-body start next result forms))))
763
764 (def-ir1-translator locally ((&body body) start next result)
765   #!+sb-doc
766   "LOCALLY declaration* form*
767
768 Sequentially evaluate the FORMS in a lexical environment where the
769 DECLARATIONS have effect. If LOCALLY is a top level form, then the FORMS are
770 also processed as top level forms."
771   (ir1-translate-locally body start next result))
772 \f
773 ;;;; FLET and LABELS
774
775 ;;; Given a list of local function specifications in the style of
776 ;;; FLET, return lists of the function names and of the lambdas which
777 ;;; are their definitions.
778 ;;;
779 ;;; The function names are checked for legality. CONTEXT is the name
780 ;;; of the form, for error reporting.
781 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
782 (defun extract-flet-vars (definitions context)
783   (collect ((names)
784             (defs))
785     (dolist (def definitions)
786       (when (or (atom def) (< (length def) 2))
787         (compiler-error "The ~S definition spec ~S is malformed." context def))
788
789       (let ((name (first def)))
790         (check-fun-name name)
791         (when (fboundp name)
792           (program-assert-symbol-home-package-unlocked
793            :compile name "binding ~A as a local function"))
794         (names name)
795         (multiple-value-bind (forms decls doc) (parse-body (cddr def))
796           (defs `(lambda ,(second def)
797                    ,@(when doc (list doc))
798                    ,@decls
799                    (block ,(fun-name-block-name name)
800                      . ,forms))))))
801     (values (names) (defs))))
802
803 (defun ir1-convert-fbindings (start next result funs body)
804   (let ((ctran (make-ctran))
805         (dx-p (find-if #'leaf-dynamic-extent funs)))
806     (when dx-p
807       (ctran-starts-block ctran)
808       (ctran-starts-block next))
809     (ir1-convert start ctran nil `(%%allocate-closures ,@funs))
810     (cond (dx-p
811            (let* ((dummy (make-ctran))
812                   (entry (make-entry))
813                   (cleanup (make-cleanup :kind :dynamic-extent
814                                          :mess-up entry
815                                          :info (list (node-dest
816                                                       (ctran-next start))))))
817              (push entry (lambda-entries (lexenv-lambda *lexenv*)))
818              (setf (entry-cleanup entry) cleanup)
819              (link-node-to-previous-ctran entry ctran)
820              (use-ctran entry dummy)
821
822              (let ((*lexenv* (make-lexenv :cleanup cleanup)))
823                (ir1-convert-progn-body dummy next result body))))
824           (t (ir1-convert-progn-body ctran next result body)))))
825
826 (def-ir1-translator flet ((definitions &body body)
827                           start next result)
828   #!+sb-doc
829   "FLET ({(name lambda-list declaration* form*)}*) declaration* body-form*
830
831 Evaluate the BODY-FORMS with local function definitions. The bindings do
832 not enclose the definitions; any use of NAME in the FORMS will refer to the
833 lexically apparent function definition in the enclosing environment."
834   (multiple-value-bind (forms decls)
835       (parse-body body :doc-string-allowed nil)
836     (multiple-value-bind (names defs)
837         (extract-flet-vars definitions 'flet)
838       (let ((fvars (mapcar (lambda (n d)
839                              (ir1-convert-lambda
840                               d :source-name n
841                                 :maybe-add-debug-catch t
842                                 :debug-name
843                                 (debug-name 'flet n t)))
844                            names defs)))
845         (processing-decls (decls nil fvars next result)
846           (let ((*lexenv* (make-lexenv :funs (pairlis names fvars))))
847             (ir1-convert-fbindings start next result fvars forms)))))))
848
849 (def-ir1-translator labels ((definitions &body body) start next result)
850   #!+sb-doc
851   "LABELS ({(name lambda-list declaration* form*)}*) declaration* body-form*
852
853 Evaluate the BODY-FORMS with local function definitions. The bindings enclose
854 the new definitions, so the defined functions can call themselves or each
855 other."
856   (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
857     (multiple-value-bind (names defs)
858         (extract-flet-vars definitions 'labels)
859       (let* (;; dummy LABELS functions, to be used as placeholders
860              ;; during construction of real LABELS functions
861              (placeholder-funs (mapcar (lambda (name)
862                                          (make-functional
863                                           :%source-name name
864                                           :%debug-name (debug-name
865                                                         'labels-placeholder
866                                                         name)))
867                                        names))
868              ;; (like PAIRLIS but guaranteed to preserve ordering:)
869              (placeholder-fenv (mapcar #'cons names placeholder-funs))
870              ;; the real LABELS functions, compiled in a LEXENV which
871              ;; includes the dummy LABELS functions
872              (real-funs
873               (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
874                 (mapcar (lambda (name def)
875                           (ir1-convert-lambda def
876                                               :source-name name
877                                               :maybe-add-debug-catch t
878                                               :debug-name (debug-name 'labels name t)))
879                         names defs))))
880
881         ;; Modify all the references to the dummy function leaves so
882         ;; that they point to the real function leaves.
883         (loop for real-fun in real-funs and
884               placeholder-cons in placeholder-fenv do
885               (substitute-leaf real-fun (cdr placeholder-cons))
886               (setf (cdr placeholder-cons) real-fun))
887
888         ;; Voila.
889         (processing-decls (decls nil real-funs next result)
890           (let ((*lexenv* (make-lexenv
891                            ;; Use a proper FENV here (not the
892                            ;; placeholder used earlier) so that if the
893                            ;; lexical environment is used for inline
894                            ;; expansion we'll get the right functions.
895                            :funs (pairlis names real-funs))))
896             (ir1-convert-fbindings start next result real-funs forms)))))))
897
898 \f
899 ;;;; the THE special operator, and friends
900
901 ;;; A logic shared among THE and TRULY-THE.
902 (defun the-in-policy (type value policy start next result)
903   (let ((type (if (ctype-p type) type
904                    (compiler-values-specifier-type type))))
905     (cond ((or (eq type *wild-type*)
906                (eq type *universal-type*)
907                (and (leaf-p value)
908                     (values-subtypep (make-single-value-type (leaf-type value))
909                                      type))
910                (and (sb!xc:constantp value)
911                     (ctypep (constant-form-value value)
912                             (single-value-type type))))
913            (ir1-convert start next result value))
914           (t (let ((value-ctran (make-ctran))
915                    (value-lvar (make-lvar)))
916                (ir1-convert start value-ctran value-lvar value)
917                (let ((cast (make-cast value-lvar type policy)))
918                  (link-node-to-previous-ctran cast value-ctran)
919                  (setf (lvar-dest value-lvar) cast)
920                  (use-continuation cast next result)))))))
921
922 ;;; Assert that FORM evaluates to the specified type (which may be a
923 ;;; VALUES type). TYPE may be a type specifier or (as a hack) a CTYPE.
924 (def-ir1-translator the ((value-type form) start next result)
925   #!+sb-doc
926   "Specifies that the values returned by FORM conform to the VALUE-TYPE.
927
928 CLHS specifies that the consequences are undefined if any result is
929 not of the declared type, but SBCL treats declarations as assertions
930 as long as SAFETY is at least 2, in which case incorrect type
931 information will result in a runtime type-error instead of leading to
932 eg. heap corruption. This is however expressly non-portable: use
933 CHECK-TYPE instead of THE to catch type-errors at runtime. THE is best
934 considered an optimization tool to inform the compiler about types it
935 is unable to derive from other declared types."
936   (the-in-policy value-type form (lexenv-policy *lexenv*) start next result))
937
938 ;;; This is like the THE special form, except that it believes
939 ;;; whatever you tell it. It will never generate a type check, but
940 ;;; will cause a warning if the compiler can prove the assertion is
941 ;;; wrong.
942 ;;;
943 ;;; For the benefit of code-walkers we also add a macro-expansion. (Using INFO
944 ;;; directly to get around safeguards for adding a macro-expansion for special
945 ;;; operator.) Because :FUNCTION :KIND remains :SPECIAL-FORM, the compiler
946 ;;; never uses the macro -- but manually calling its MACRO-FUNCTION or
947 ;;; MACROEXPANDing TRULY-THE forms does.
948 (def-ir1-translator truly-the ((value-type form) start next result)
949   #!+sb-doc
950   "Specifies that the values returned by FORM conform to the
951 VALUE-TYPE, and causes the compiler to trust this information
952 unconditionally.
953
954 Consequences are undefined if any result is not of the declared type
955 -- typical symptoms including memory corruptions. Use with great
956 care."
957   (the-in-policy value-type form '((type-check . 0)) start next result))
958
959 #-sb-xc-host
960 (setf (info :function :macro-function 'truly-the)
961       (lambda (whole env)
962         (declare (ignore env))
963         `(the ,@(cdr whole))))
964 \f
965 ;;;; SETQ
966
967 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
968 ;;; look at the global information. If the name is for a constant,
969 ;;; then error out.
970 (def-ir1-translator setq ((&whole source &rest things) start next result)
971   (let ((len (length things)))
972     (when (oddp len)
973       (compiler-error "odd number of args to SETQ: ~S" source))
974     (if (= len 2)
975         (let* ((name (first things))
976                (value-form (second things))
977                (leaf (or (lexenv-find name vars) (find-free-var name))))
978           (etypecase leaf
979             (leaf
980              (when (constant-p leaf)
981                (compiler-error "~S is a constant and thus can't be set." name))
982              (when (lambda-var-p leaf)
983                (let ((home-lambda (ctran-home-lambda-or-null start)))
984                  (when home-lambda
985                    (sset-adjoin leaf (lambda-calls-or-closes home-lambda))))
986                (when (lambda-var-ignorep leaf)
987                  ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
988                  ;; requires that this be a STYLE-WARNING, not a full warning.
989                  (compiler-style-warn
990                   "~S is being set even though it was declared to be ignored."
991                   name)))
992              (if (and (global-var-p leaf) (eq :unknown (global-var-kind leaf)))
993                  ;; For undefined variables go through SET, so that we can catch
994                  ;; constant modifications.
995                  (ir1-convert start next result `(set ',name ,value-form))
996                  (setq-var start next result leaf value-form)))
997             (cons
998              (aver (eq (car leaf) 'macro))
999              ;; FIXME: [Free] type declaration. -- APD, 2002-01-26
1000              (ir1-convert start next result
1001                           `(setf ,(cdr leaf) ,(second things))))
1002             (heap-alien-info
1003              (ir1-convert start next result
1004                           `(%set-heap-alien ',leaf ,(second things))))))
1005         (collect ((sets))
1006           (do ((thing things (cddr thing)))
1007               ((endp thing)
1008                (ir1-convert-progn-body start next result (sets)))
1009             (sets `(setq ,(first thing) ,(second thing))))))))
1010
1011 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
1012 ;;; This should only need to be called in SETQ.
1013 (defun setq-var (start next result var value)
1014   (declare (type ctran start next) (type (or lvar null) result)
1015            (type basic-var var))
1016   (let ((dest-ctran (make-ctran))
1017         (dest-lvar (make-lvar))
1018         (type (or (lexenv-find var type-restrictions)
1019                   (leaf-type var))))
1020     (ir1-convert start dest-ctran dest-lvar `(the ,(type-specifier type)
1021                                                   ,value))
1022     (let ((res (make-set :var var :value dest-lvar)))
1023       (setf (lvar-dest dest-lvar) res)
1024       (setf (leaf-ever-used var) t)
1025       (push res (basic-var-sets var))
1026       (link-node-to-previous-ctran res dest-ctran)
1027       (use-continuation res next result))))
1028 \f
1029 ;;;; CATCH, THROW and UNWIND-PROTECT
1030
1031 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
1032 ;;; since as as far as IR1 is concerned, it has no interesting
1033 ;;; properties other than receiving multiple-values.
1034 (def-ir1-translator throw ((tag result) start next result-lvar)
1035   #!+sb-doc
1036   "THROW tag form
1037
1038 Do a non-local exit, return the values of FORM from the CATCH whose tag is EQ
1039 to TAG."
1040   (ir1-convert start next result-lvar
1041                `(multiple-value-call #'%throw ,tag ,result)))
1042
1043 ;;; This is a special special form used to instantiate a cleanup as
1044 ;;; the current cleanup within the body. KIND is the kind of cleanup
1045 ;;; to make, and MESS-UP is a form that does the mess-up action. We
1046 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
1047 ;;; and introduce the cleanup into the lexical environment. We
1048 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
1049 ;;; cleanup, since this inner cleanup is the interesting one.
1050 (def-ir1-translator %within-cleanup
1051     ((kind mess-up &body body) start next result)
1052   (let ((dummy (make-ctran))
1053         (dummy2 (make-ctran)))
1054     (ir1-convert start dummy nil mess-up)
1055     (let* ((mess-node (ctran-use dummy))
1056            (cleanup (make-cleanup :kind kind
1057                                   :mess-up mess-node))
1058            (old-cup (lexenv-cleanup *lexenv*))
1059            (*lexenv* (make-lexenv :cleanup cleanup)))
1060       (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
1061       (ir1-convert dummy dummy2 nil '(%cleanup-point))
1062       (ir1-convert-progn-body dummy2 next result body))))
1063
1064 ;;; This is a special special form that makes an "escape function"
1065 ;;; which returns unknown values from named block. We convert the
1066 ;;; function, set its kind to :ESCAPE, and then reference it. The
1067 ;;; :ESCAPE kind indicates that this function's purpose is to
1068 ;;; represent a non-local control transfer, and that it might not
1069 ;;; actually have to be compiled.
1070 ;;;
1071 ;;; Note that environment analysis replaces references to escape
1072 ;;; functions with references to the corresponding NLX-INFO structure.
1073 (def-ir1-translator %escape-fun ((tag) start next result)
1074   (let ((fun (let ((*allow-instrumenting* nil))
1075                (ir1-convert-lambda
1076                 `(lambda ()
1077                    (return-from ,tag (%unknown-values)))
1078                 :debug-name (debug-name 'escape-fun tag))))
1079         (ctran (make-ctran)))
1080     (setf (functional-kind fun) :escape)
1081     (ir1-convert start ctran nil `(%%allocate-closures ,fun))
1082     (reference-leaf ctran next result fun)))
1083
1084 ;;; Yet another special special form. This one looks up a local
1085 ;;; function and smashes it to a :CLEANUP function, as well as
1086 ;;; referencing it.
1087 (def-ir1-translator %cleanup-fun ((name) start next result)
1088   ;; FIXME: Should this not be :TEST #'EQUAL? What happens to
1089   ;; (SETF FOO) here?
1090   (let ((fun (lexenv-find name funs)))
1091     (aver (lambda-p fun))
1092     (setf (functional-kind fun) :cleanup)
1093     (reference-leaf start next result fun)))
1094
1095 (def-ir1-translator catch ((tag &body body) start next result)
1096   #!+sb-doc
1097   "CATCH tag form*
1098
1099 Evaluate TAG and instantiate it as a catcher while the body forms are
1100 evaluated in an implicit PROGN. If a THROW is done to TAG within the dynamic
1101 scope of the body, then control will be transferred to the end of the body and
1102 the thrown values will be returned."
1103   ;; We represent the possibility of the control transfer by making an
1104   ;; "escape function" that does a lexical exit, and instantiate the
1105   ;; cleanup using %WITHIN-CLEANUP.
1106   (ir1-convert
1107    start next result
1108    (with-unique-names (exit-block)
1109      `(block ,exit-block
1110         (%within-cleanup
1111          :catch (%catch (%escape-fun ,exit-block) ,tag)
1112          ,@body)))))
1113
1114 (def-ir1-translator unwind-protect
1115     ((protected &body cleanup) start next result)
1116   #!+sb-doc
1117   "UNWIND-PROTECT protected cleanup*
1118
1119 Evaluate the form PROTECTED, returning its values. The CLEANUP forms are
1120 evaluated whenever the dynamic scope of the PROTECTED form is exited (either
1121 due to normal completion or a non-local exit such as THROW)."
1122   ;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
1123   ;; cleanup forms into a local function so that they can be referenced
1124   ;; both in the case where we are unwound and in any local exits. We
1125   ;; use %CLEANUP-FUN on this to indicate that reference by
1126   ;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
1127   ;; an XEP.
1128   (ir1-convert
1129    start next result
1130    (with-unique-names (cleanup-fun drop-thru-tag exit-tag next start count)
1131      `(flet ((,cleanup-fun ()
1132                ,@cleanup
1133                nil))
1134         ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
1135         ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
1136         ;; and something can be done to make %ESCAPE-FUN have
1137         ;; dynamic extent too.
1138         (declare (dynamic-extent #',cleanup-fun))
1139         (block ,drop-thru-tag
1140           (multiple-value-bind (,next ,start ,count)
1141               (block ,exit-tag
1142                 (%within-cleanup
1143                     :unwind-protect
1144                     (%unwind-protect (%escape-fun ,exit-tag)
1145                                      (%cleanup-fun ,cleanup-fun))
1146                   (return-from ,drop-thru-tag ,protected)))
1147             (declare (optimize (insert-debug-catch 0)))
1148             (,cleanup-fun)
1149             (%continue-unwind ,next ,start ,count)))))))
1150 \f
1151 ;;;; multiple-value stuff
1152
1153 (def-ir1-translator multiple-value-call ((fun &rest args) start next result)
1154   #!+sb-doc
1155   "MULTIPLE-VALUE-CALL function values-form*
1156
1157 Call FUNCTION, passing all the values of each VALUES-FORM as arguments,
1158 values from the first VALUES-FORM making up the first argument, etc."
1159   (let* ((ctran (make-ctran))
1160          (fun-lvar (make-lvar))
1161          (node (if args
1162                    ;; If there are arguments, MULTIPLE-VALUE-CALL
1163                    ;; turns into an MV-COMBINATION.
1164                    (make-mv-combination fun-lvar)
1165                    ;; If there are no arguments, then we convert to a
1166                    ;; normal combination, ensuring that a MV-COMBINATION
1167                    ;; always has at least one argument. This can be
1168                    ;; regarded as an optimization, but it is more
1169                    ;; important for simplifying compilation of
1170                    ;; MV-COMBINATIONS.
1171                    (make-combination fun-lvar))))
1172     (ir1-convert start ctran fun-lvar (ensure-source-fun-form fun))
1173     (setf (lvar-dest fun-lvar) node)
1174     (collect ((arg-lvars))
1175       (let ((this-start ctran))
1176         (dolist (arg args)
1177           (let ((this-ctran (make-ctran))
1178                 (this-lvar (make-lvar node)))
1179             (ir1-convert this-start this-ctran this-lvar arg)
1180             (setq this-start this-ctran)
1181             (arg-lvars this-lvar)))
1182         (link-node-to-previous-ctran node this-start)
1183         (use-continuation node next result)
1184         (setf (basic-combination-args node) (arg-lvars))))))
1185
1186 (def-ir1-translator multiple-value-prog1
1187     ((values-form &rest forms) start next result)
1188   #!+sb-doc
1189   "MULTIPLE-VALUE-PROG1 values-form form*
1190
1191 Evaluate VALUES-FORM and then the FORMS, but return all the values of
1192 VALUES-FORM."
1193   (let ((dummy (make-ctran)))
1194     (ctran-starts-block dummy)
1195     (ir1-convert start dummy result values-form)
1196     (ir1-convert-progn-body dummy next nil forms)))
1197 \f
1198 ;;;; interface to defining macros
1199
1200 ;;; Old CMUCL comment:
1201 ;;;
1202 ;;;   Return a new source path with any stuff intervening between the
1203 ;;;   current path and the first form beginning with NAME stripped
1204 ;;;   off.  This is used to hide the guts of DEFmumble macros to
1205 ;;;   prevent annoying error messages.
1206 ;;;
1207 ;;; Now that we have implementations of DEFmumble macros in terms of
1208 ;;; EVAL-WHEN, this function is no longer used.  However, it might be
1209 ;;; worth figuring out why it was used, and maybe doing analogous
1210 ;;; munging to the functions created in the expanders for the macros.
1211 (defun revert-source-path (name)
1212   (do ((path *current-path* (cdr path)))
1213       ((null path) *current-path*)
1214     (let ((first (first path)))
1215       (when (or (eq first name)
1216                 (eq first 'original-source-start))
1217         (return path)))))