better inner and anonymous function names
[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 an integer or a symbol. A STATEMENT
205 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 #'car (lexenv-blocks *lexenv*) :from-end t))
484               *source-namestring*
485               (let ((p (or *compile-file-truename* *load-truename*)))
486                 (when p (namestring p))))))
487     (when context
488       (list :in context))))
489
490 ;;;; FUNCTION and NAMED-LAMBDA
491 (defun name-lambdalike (thing)
492   (case (car thing)
493     ((named-lambda)
494      (or (second thing)
495          `(lambda ,(third thing) ,(name-context))))
496     ((lambda)
497      `(lambda ,(second thing) ,@(name-context)))
498     ((lambda-with-lexenv)
499      ;; FIXME: Get the original DEFUN name here.
500      `(lambda ,(fifth thing)))
501     (otherwise
502      (compiler-error "Not a valid lambda expression:~%  ~S"
503                      thing))))
504
505 (defun fun-name-leaf (thing)
506   (if (consp thing)
507       (cond
508         ((member (car thing)
509                  '(lambda named-lambda lambda-with-lexenv))
510          (values (ir1-convert-lambdalike
511                   thing
512                   :debug-name (name-lambdalike thing))
513                  t))
514         ((legal-fun-name-p thing)
515          (values (find-lexically-apparent-fun
516                   thing "as the argument to FUNCTION")
517                  nil))
518         (t
519          (compiler-error "~S is not a legal function name." thing)))
520       (values (find-lexically-apparent-fun
521                thing "as the argument to FUNCTION")
522               nil)))
523
524 (def-ir1-translator %%allocate-closures ((&rest leaves) start next result)
525   (aver (eq result 'nil))
526   (let ((lambdas leaves))
527     (ir1-convert start next result `(%allocate-closures ',lambdas))
528     (let ((allocator (node-dest (ctran-next start))))
529       (dolist (lambda lambdas)
530         (setf (functional-allocator lambda) allocator)))))
531
532 (defmacro with-fun-name-leaf ((leaf thing start &key global-function) &body body)
533   `(multiple-value-bind (,leaf allocate-p)
534        (if ,global-function
535            (find-global-fun ,thing t)
536            (fun-name-leaf ,thing))
537      (if allocate-p
538          (let ((.new-start. (make-ctran)))
539            (ir1-convert ,start .new-start. nil `(%%allocate-closures ,leaf))
540            (let ((,start .new-start.))
541              ,@body))
542          (locally
543              ,@body))))
544
545 (def-ir1-translator function ((thing) start next result)
546   #!+sb-doc
547   "FUNCTION name
548
549 Return the lexically apparent definition of the function NAME. NAME may also
550 be a lambda expression."
551   (with-fun-name-leaf (leaf thing start)
552     (reference-leaf start next result leaf)))
553
554 ;;; Like FUNCTION, but ignores local definitions and inline
555 ;;; expansions, and doesn't nag about undefined functions.
556 ;;; Used for optimizing things like (FUNCALL 'FOO).
557 (def-ir1-translator global-function ((thing) start next result)
558   (with-fun-name-leaf (leaf thing start :global-function t)
559     (reference-leaf start next result leaf)))
560
561 (defun constant-global-fun-name (thing)
562   (let ((constantp (sb!xc:constantp thing)))
563     (when constantp
564       (let ((name (constant-form-value thing)))
565         (when (legal-fun-name-p name)
566           name)))))
567
568 (defun lvar-constant-global-fun-name (lvar)
569   (when (constant-lvar-p lvar)
570     (let ((name (lvar-value lvar)))
571       (when (legal-fun-name-p name)
572         name))))
573
574 (defun ensure-source-fun-form (source &optional give-up)
575   (let ((op (when (consp source) (car source))))
576     (cond ((eq op '%coerce-callable-to-fun)
577            (ensure-source-fun-form (second source)))
578           ((member op '(function global-function lambda named-lambda))
579            (values source nil))
580           (t
581            (let ((cname (constant-global-fun-name source)))
582              (if cname
583                  (values `(global-function ,cname) nil)
584                  (values `(%coerce-callable-to-fun ,source) give-up)))))))
585
586 (defun ensure-lvar-fun-form (lvar lvar-name &optional give-up)
587   (aver (and lvar-name (symbolp lvar-name)))
588   (if (csubtypep (lvar-type lvar) (specifier-type 'function))
589       lvar-name
590       (let ((cname (lvar-constant-global-fun-name lvar)))
591         (cond (cname
592                `(global-function ,cname))
593               (give-up
594                (give-up-ir1-transform give-up))
595               (t
596                `(%coerce-callable-to-fun ,lvar-name))))))
597 \f
598 ;;;; FUNCALL
599
600 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
601 ;;; (not symbols). %FUNCALL is used directly in some places where the
602 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
603 (deftransform funcall ((function &rest args) * *)
604   (let ((arg-names (make-gensym-list (length args))))
605     `(lambda (function ,@arg-names)
606        (declare (ignorable function))
607        `(%funcall ,(ensure-lvar-fun-form function 'function) ,@arg-names))))
608
609 (def-ir1-translator %funcall ((function &rest args) start next result)
610   ;; MACROEXPAND so that (LAMBDA ...) forms arriving here don't get an
611   ;; extra cast inserted for them.
612   (let* ((function (%macroexpand function *lexenv*))
613          (op (when (consp function) (car function))))
614     (cond ((eq op 'function)
615            (compiler-destructuring-bind (thing) (cdr function)
616                function
617              (with-fun-name-leaf (leaf thing start)
618                (ir1-convert start next result `(,leaf ,@args)))))
619           ((eq op 'global-function)
620            (compiler-destructuring-bind (thing) (cdr function)
621                global-function
622              (with-fun-name-leaf (leaf thing start :global-function t)
623                (ir1-convert start next result `(,leaf ,@args)))))
624           (t
625            (let ((ctran (make-ctran))
626                  (fun-lvar (make-lvar)))
627              (ir1-convert start ctran fun-lvar `(the function ,function))
628              (ir1-convert-combination-args fun-lvar ctran next result args))))))
629
630 ;;; This source transform exists to reduce the amount of work for the
631 ;;; compiler. If the called function is a FUNCTION form, then convert
632 ;;; directly to %FUNCALL, instead of waiting around for type
633 ;;; inference.
634 (define-source-transform funcall (function &rest args)
635   `(%funcall ,(ensure-source-fun-form function) ,@args))
636
637 (deftransform %coerce-callable-to-fun ((thing) * *)
638   (ensure-lvar-fun-form thing 'thing "optimize away possible call to FDEFINITION at runtime"))
639
640 (define-source-transform %coerce-callable-to-fun (thing)
641   (ensure-source-fun-form thing t))
642 \f
643 ;;;; LET and LET*
644 ;;;;
645 ;;;; (LET and LET* can't be implemented as macros due to the fact that
646 ;;;; any pervasive declarations also affect the evaluation of the
647 ;;;; arguments.)
648
649 ;;; Given a list of binding specifiers in the style of LET, return:
650 ;;;  1. The list of var structures for the variables bound.
651 ;;;  2. The initial value form for each variable.
652 ;;;
653 ;;; The variable names are checked for legality and globally special
654 ;;; variables are marked as such. Context is the name of the form, for
655 ;;; error reporting purposes.
656 (declaim (ftype (function (list symbol) (values list list))
657                 extract-let-vars))
658 (defun extract-let-vars (bindings context)
659   (collect ((vars)
660             (vals)
661             (names))
662     (flet ((get-var (name)
663              (varify-lambda-arg name
664                                 (if (eq context 'let*)
665                                     nil
666                                     (names))
667                                 context)))
668       (dolist (spec bindings)
669         (cond ((atom spec)
670                (let ((var (get-var spec)))
671                  (vars var)
672                  (names spec)
673                  (vals nil)))
674               (t
675                (unless (proper-list-of-length-p spec 1 2)
676                  (compiler-error "The ~S binding spec ~S is malformed."
677                                  context
678                                  spec))
679                (let* ((name (first spec))
680                       (var (get-var name)))
681                  (vars var)
682                  (names name)
683                  (vals (second spec)))))))
684     (dolist (name (names))
685       (when (eq (info :variable :kind name) :macro)
686         (program-assert-symbol-home-package-unlocked
687          :compile name "lexically binding symbol-macro ~A")))
688     (values (vars) (vals))))
689
690 (def-ir1-translator let ((bindings &body body) start next result)
691   #!+sb-doc
692   "LET ({(var [value]) | var}*) declaration* form*
693
694 During evaluation of the FORMS, bind the VARS to the result of evaluating the
695 VALUE forms. The variables are bound in parallel after all of the VALUES forms
696 have been evaluated."
697   (cond ((null bindings)
698          (ir1-translate-locally body start next result))
699         ((listp bindings)
700          (multiple-value-bind (forms decls)
701              (parse-body body :doc-string-allowed nil)
702            (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
703              (binding* ((ctran (make-ctran))
704                         (fun-lvar (make-lvar))
705                         ((next result)
706                          (processing-decls (decls vars nil next result
707                                                   post-binding-lexenv)
708                            (let ((fun (ir1-convert-lambda-body
709                                        forms
710                                        vars
711                                        :post-binding-lexenv post-binding-lexenv
712                                        :debug-name (debug-name 'let bindings))))
713                              (reference-leaf start ctran fun-lvar fun))
714                            (values next result))))
715                (ir1-convert-combination-args fun-lvar ctran next result values)))))
716         (t
717          (compiler-error "Malformed LET bindings: ~S." bindings))))
718
719 (def-ir1-translator let* ((bindings &body body)
720                           start next result)
721   #!+sb-doc
722   "LET* ({(var [value]) | var}*) declaration* form*
723
724 Similar to LET, but the variables are bound sequentially, allowing each VALUE
725 form to reference any of the previous VARS."
726   (if (listp bindings)
727       (multiple-value-bind (forms decls)
728           (parse-body body :doc-string-allowed nil)
729         (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
730           (processing-decls (decls vars nil next result post-binding-lexenv)
731             (ir1-convert-aux-bindings start
732                                       next
733                                       result
734                                       forms
735                                       vars
736                                       values
737                                       post-binding-lexenv))))
738       (compiler-error "Malformed LET* bindings: ~S." bindings)))
739
740 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
741 ;;; and SYMBOL-MACROLET
742 ;;;
743 ;;; Note that all these things need to preserve toplevel-formness,
744 ;;; but we don't need to worry about that within an IR1 translator,
745 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
746 ;;; forms before we hit the IR1 transform level.
747 (defun ir1-translate-locally (body start next result &key vars funs)
748   (declare (type ctran start next) (type (or lvar null) result)
749            (type list body))
750   (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
751     (processing-decls (decls vars funs next result)
752       (ir1-convert-progn-body start next result forms))))
753
754 (def-ir1-translator locally ((&body body) start next result)
755   #!+sb-doc
756   "LOCALLY declaration* form*
757
758 Sequentially evaluate the FORMS in a lexical environment where the
759 DECLARATIONS have effect. If LOCALLY is a top level form, then the FORMS are
760 also processed as top level forms."
761   (ir1-translate-locally body start next result))
762 \f
763 ;;;; FLET and LABELS
764
765 ;;; Given a list of local function specifications in the style of
766 ;;; FLET, return lists of the function names and of the lambdas which
767 ;;; are their definitions.
768 ;;;
769 ;;; The function names are checked for legality. CONTEXT is the name
770 ;;; of the form, for error reporting.
771 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
772 (defun extract-flet-vars (definitions context)
773   (collect ((names)
774             (defs))
775     (dolist (def definitions)
776       (when (or (atom def) (< (length def) 2))
777         (compiler-error "The ~S definition spec ~S is malformed." context def))
778
779       (let ((name (first def)))
780         (check-fun-name name)
781         (when (fboundp name)
782           (program-assert-symbol-home-package-unlocked
783            :compile name "binding ~A as a local function"))
784         (names name)
785         (multiple-value-bind (forms decls doc) (parse-body (cddr def))
786           (defs `(lambda ,(second def)
787                    ,@(when doc (list doc))
788                    ,@decls
789                    (block ,(fun-name-block-name name)
790                      . ,forms))))))
791     (values (names) (defs))))
792
793 (defun ir1-convert-fbindings (start next result funs body)
794   (let ((ctran (make-ctran))
795         (dx-p (find-if #'leaf-dynamic-extent funs)))
796     (when dx-p
797       (ctran-starts-block ctran)
798       (ctran-starts-block next))
799     (ir1-convert start ctran nil `(%%allocate-closures ,@funs))
800     (cond (dx-p
801            (let* ((dummy (make-ctran))
802                   (entry (make-entry))
803                   (cleanup (make-cleanup :kind :dynamic-extent
804                                          :mess-up entry
805                                          :info (list (node-dest
806                                                       (ctran-next start))))))
807              (push entry (lambda-entries (lexenv-lambda *lexenv*)))
808              (setf (entry-cleanup entry) cleanup)
809              (link-node-to-previous-ctran entry ctran)
810              (use-ctran entry dummy)
811
812              (let ((*lexenv* (make-lexenv :cleanup cleanup)))
813                (ir1-convert-progn-body dummy next result body))))
814           (t (ir1-convert-progn-body ctran next result body)))))
815
816 (def-ir1-translator flet ((definitions &body body)
817                           start next result)
818   #!+sb-doc
819   "FLET ({(name lambda-list declaration* form*)}*) declaration* body-form*
820
821 Evaluate the BODY-FORMS with local function definitions. The bindings do
822 not enclose the definitions; any use of NAME in the FORMS will refer to the
823 lexically apparent function definition in the enclosing environment."
824   (multiple-value-bind (forms decls)
825       (parse-body body :doc-string-allowed nil)
826     (multiple-value-bind (names defs)
827         (extract-flet-vars definitions 'flet)
828       (let ((fvars (mapcar (lambda (n d)
829                              (ir1-convert-lambda
830                               d :source-name n
831                                 :maybe-add-debug-catch t
832                                 :debug-name
833                                 (debug-name 'flet n t)))
834                            names defs)))
835         (processing-decls (decls nil fvars next result)
836           (let ((*lexenv* (make-lexenv :funs (pairlis names fvars))))
837             (ir1-convert-fbindings start next result fvars forms)))))))
838
839 (def-ir1-translator labels ((definitions &body body) start next result)
840   #!+sb-doc
841   "LABELS ({(name lambda-list declaration* form*)}*) declaration* body-form*
842
843 Evaluate the BODY-FORMS with local function definitions. The bindings enclose
844 the new definitions, so the defined functions can call themselves or each
845 other."
846   (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
847     (multiple-value-bind (names defs)
848         (extract-flet-vars definitions 'labels)
849       (let* (;; dummy LABELS functions, to be used as placeholders
850              ;; during construction of real LABELS functions
851              (placeholder-funs (mapcar (lambda (name)
852                                          (make-functional
853                                           :%source-name name
854                                           :%debug-name (debug-name
855                                                         'labels-placeholder
856                                                         name)))
857                                        names))
858              ;; (like PAIRLIS but guaranteed to preserve ordering:)
859              (placeholder-fenv (mapcar #'cons names placeholder-funs))
860              ;; the real LABELS functions, compiled in a LEXENV which
861              ;; includes the dummy LABELS functions
862              (real-funs
863               (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
864                 (mapcar (lambda (name def)
865                           (ir1-convert-lambda def
866                                               :source-name name
867                                               :maybe-add-debug-catch t
868                                               :debug-name (debug-name 'labels name t)))
869                         names defs))))
870
871         ;; Modify all the references to the dummy function leaves so
872         ;; that they point to the real function leaves.
873         (loop for real-fun in real-funs and
874               placeholder-cons in placeholder-fenv do
875               (substitute-leaf real-fun (cdr placeholder-cons))
876               (setf (cdr placeholder-cons) real-fun))
877
878         ;; Voila.
879         (processing-decls (decls nil real-funs next result)
880           (let ((*lexenv* (make-lexenv
881                            ;; Use a proper FENV here (not the
882                            ;; placeholder used earlier) so that if the
883                            ;; lexical environment is used for inline
884                            ;; expansion we'll get the right functions.
885                            :funs (pairlis names real-funs))))
886             (ir1-convert-fbindings start next result real-funs forms)))))))
887
888 \f
889 ;;;; the THE special operator, and friends
890
891 ;;; A logic shared among THE and TRULY-THE.
892 (defun the-in-policy (type value policy start next result)
893   (let ((type (if (ctype-p type) type
894                    (compiler-values-specifier-type type))))
895     (cond ((or (eq type *wild-type*)
896                (eq type *universal-type*)
897                (and (leaf-p value)
898                     (values-subtypep (make-single-value-type (leaf-type value))
899                                      type))
900                (and (sb!xc:constantp value)
901                     (ctypep (constant-form-value value)
902                             (single-value-type type))))
903            (ir1-convert start next result value))
904           (t (let ((value-ctran (make-ctran))
905                    (value-lvar (make-lvar)))
906                (ir1-convert start value-ctran value-lvar value)
907                (let ((cast (make-cast value-lvar type policy)))
908                  (link-node-to-previous-ctran cast value-ctran)
909                  (setf (lvar-dest value-lvar) cast)
910                  (use-continuation cast next result)))))))
911
912 ;;; Assert that FORM evaluates to the specified type (which may be a
913 ;;; VALUES type). TYPE may be a type specifier or (as a hack) a CTYPE.
914 (def-ir1-translator the ((value-type form) start next result)
915   #!+sb-doc
916   "Specifies that the values returned by FORM conform to the VALUE-TYPE.
917
918 CLHS specifies that the consequences are undefined if any result is
919 not of the declared type, but SBCL treats declarations as assertions
920 as long as SAFETY is at least 2, in which case incorrect type
921 information will result in a runtime type-error instead of leading to
922 eg. heap corruption. This is however expressly non-portable: use
923 CHECK-TYPE instead of THE to catch type-errors at runtime. THE is best
924 considered an optimization tool to inform the compiler about types it
925 is unable to derive from other declared types."
926   (the-in-policy value-type form (lexenv-policy *lexenv*) start next result))
927
928 ;;; This is like the THE special form, except that it believes
929 ;;; whatever you tell it. It will never generate a type check, but
930 ;;; will cause a warning if the compiler can prove the assertion is
931 ;;; wrong.
932 ;;;
933 ;;; For the benefit of code-walkers we also add a macro-expansion. (Using INFO
934 ;;; directly to get around safeguards for adding a macro-expansion for special
935 ;;; operator.) Because :FUNCTION :KIND remains :SPECIAL-FORM, the compiler
936 ;;; never uses the macro -- but manually calling its MACRO-FUNCTION or
937 ;;; MACROEXPANDing TRULY-THE forms does.
938 (def-ir1-translator truly-the ((value-type form) start next result)
939   #!+sb-doc
940   "Specifies that the values returned by FORM conform to the
941 VALUE-TYPE, and causes the compiler to trust this information
942 unconditionally.
943
944 Consequences are undefined if any result is not of the declared type
945 -- typical symptoms including memory corruptions. Use with great
946 care."
947   (the-in-policy value-type form '((type-check . 0)) start next result))
948
949 #-sb-xc-host
950 (setf (info :function :macro-function 'truly-the)
951       (lambda (whole env)
952         (declare (ignore env))
953         `(the ,@(cdr whole))))
954 \f
955 ;;;; SETQ
956
957 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
958 ;;; look at the global information. If the name is for a constant,
959 ;;; then error out.
960 (def-ir1-translator setq ((&whole source &rest things) start next result)
961   (let ((len (length things)))
962     (when (oddp len)
963       (compiler-error "odd number of args to SETQ: ~S" source))
964     (if (= len 2)
965         (let* ((name (first things))
966                (value-form (second things))
967                (leaf (or (lexenv-find name vars) (find-free-var name))))
968           (etypecase leaf
969             (leaf
970              (when (constant-p leaf)
971                (compiler-error "~S is a constant and thus can't be set." name))
972              (when (lambda-var-p leaf)
973                (let ((home-lambda (ctran-home-lambda-or-null start)))
974                  (when home-lambda
975                    (sset-adjoin leaf (lambda-calls-or-closes home-lambda))))
976                (when (lambda-var-ignorep leaf)
977                  ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
978                  ;; requires that this be a STYLE-WARNING, not a full warning.
979                  (compiler-style-warn
980                   "~S is being set even though it was declared to be ignored."
981                   name)))
982              (if (and (global-var-p leaf) (eq :unknown (global-var-kind leaf)))
983                  ;; For undefined variables go through SET, so that we can catch
984                  ;; constant modifications.
985                  (ir1-convert start next result `(set ',name ,value-form))
986                  (setq-var start next result leaf value-form)))
987             (cons
988              (aver (eq (car leaf) 'macro))
989              ;; FIXME: [Free] type declaration. -- APD, 2002-01-26
990              (ir1-convert start next result
991                           `(setf ,(cdr leaf) ,(second things))))
992             (heap-alien-info
993              (ir1-convert start next result
994                           `(%set-heap-alien ',leaf ,(second things))))))
995         (collect ((sets))
996           (do ((thing things (cddr thing)))
997               ((endp thing)
998                (ir1-convert-progn-body start next result (sets)))
999             (sets `(setq ,(first thing) ,(second thing))))))))
1000
1001 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
1002 ;;; This should only need to be called in SETQ.
1003 (defun setq-var (start next result var value)
1004   (declare (type ctran start next) (type (or lvar null) result)
1005            (type basic-var var))
1006   (let ((dest-ctran (make-ctran))
1007         (dest-lvar (make-lvar))
1008         (type (or (lexenv-find var type-restrictions)
1009                   (leaf-type var))))
1010     (ir1-convert start dest-ctran dest-lvar `(the ,(type-specifier type)
1011                                                   ,value))
1012     (let ((res (make-set :var var :value dest-lvar)))
1013       (setf (lvar-dest dest-lvar) res)
1014       (setf (leaf-ever-used var) t)
1015       (push res (basic-var-sets var))
1016       (link-node-to-previous-ctran res dest-ctran)
1017       (use-continuation res next result))))
1018 \f
1019 ;;;; CATCH, THROW and UNWIND-PROTECT
1020
1021 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
1022 ;;; since as as far as IR1 is concerned, it has no interesting
1023 ;;; properties other than receiving multiple-values.
1024 (def-ir1-translator throw ((tag result) start next result-lvar)
1025   #!+sb-doc
1026   "THROW tag form
1027
1028 Do a non-local exit, return the values of FORM from the CATCH whose tag is EQ
1029 to TAG."
1030   (ir1-convert start next result-lvar
1031                `(multiple-value-call #'%throw ,tag ,result)))
1032
1033 ;;; This is a special special form used to instantiate a cleanup as
1034 ;;; the current cleanup within the body. KIND is the kind of cleanup
1035 ;;; to make, and MESS-UP is a form that does the mess-up action. We
1036 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
1037 ;;; and introduce the cleanup into the lexical environment. We
1038 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
1039 ;;; cleanup, since this inner cleanup is the interesting one.
1040 (def-ir1-translator %within-cleanup
1041     ((kind mess-up &body body) start next result)
1042   (let ((dummy (make-ctran))
1043         (dummy2 (make-ctran)))
1044     (ir1-convert start dummy nil mess-up)
1045     (let* ((mess-node (ctran-use dummy))
1046            (cleanup (make-cleanup :kind kind
1047                                   :mess-up mess-node))
1048            (old-cup (lexenv-cleanup *lexenv*))
1049            (*lexenv* (make-lexenv :cleanup cleanup)))
1050       (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
1051       (ir1-convert dummy dummy2 nil '(%cleanup-point))
1052       (ir1-convert-progn-body dummy2 next result body))))
1053
1054 ;;; This is a special special form that makes an "escape function"
1055 ;;; which returns unknown values from named block. We convert the
1056 ;;; function, set its kind to :ESCAPE, and then reference it. The
1057 ;;; :ESCAPE kind indicates that this function's purpose is to
1058 ;;; represent a non-local control transfer, and that it might not
1059 ;;; actually have to be compiled.
1060 ;;;
1061 ;;; Note that environment analysis replaces references to escape
1062 ;;; functions with references to the corresponding NLX-INFO structure.
1063 (def-ir1-translator %escape-fun ((tag) start next result)
1064   (let ((fun (let ((*allow-instrumenting* nil))
1065                (ir1-convert-lambda
1066                 `(lambda ()
1067                    (return-from ,tag (%unknown-values)))
1068                 :debug-name (debug-name 'escape-fun tag))))
1069         (ctran (make-ctran)))
1070     (setf (functional-kind fun) :escape)
1071     (ir1-convert start ctran nil `(%%allocate-closures ,fun))
1072     (reference-leaf ctran next result fun)))
1073
1074 ;;; Yet another special special form. This one looks up a local
1075 ;;; function and smashes it to a :CLEANUP function, as well as
1076 ;;; referencing it.
1077 (def-ir1-translator %cleanup-fun ((name) start next result)
1078   ;; FIXME: Should this not be :TEST #'EQUAL? What happens to
1079   ;; (SETF FOO) here?
1080   (let ((fun (lexenv-find name funs)))
1081     (aver (lambda-p fun))
1082     (setf (functional-kind fun) :cleanup)
1083     (reference-leaf start next result fun)))
1084
1085 (def-ir1-translator catch ((tag &body body) start next result)
1086   #!+sb-doc
1087   "CATCH tag form*
1088
1089 Evaluate TAG and instantiate it as a catcher while the body forms are
1090 evaluated in an implicit PROGN. If a THROW is done to TAG within the dynamic
1091 scope of the body, then control will be transferred to the end of the body and
1092 the thrown values will be returned."
1093   ;; We represent the possibility of the control transfer by making an
1094   ;; "escape function" that does a lexical exit, and instantiate the
1095   ;; cleanup using %WITHIN-CLEANUP.
1096   (ir1-convert
1097    start next result
1098    (with-unique-names (exit-block)
1099      `(block ,exit-block
1100         (%within-cleanup
1101          :catch (%catch (%escape-fun ,exit-block) ,tag)
1102          ,@body)))))
1103
1104 (def-ir1-translator unwind-protect
1105     ((protected &body cleanup) start next result)
1106   #!+sb-doc
1107   "UNWIND-PROTECT protected cleanup*
1108
1109 Evaluate the form PROTECTED, returning its values. The CLEANUP forms are
1110 evaluated whenever the dynamic scope of the PROTECTED form is exited (either
1111 due to normal completion or a non-local exit such as THROW)."
1112   ;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
1113   ;; cleanup forms into a local function so that they can be referenced
1114   ;; both in the case where we are unwound and in any local exits. We
1115   ;; use %CLEANUP-FUN on this to indicate that reference by
1116   ;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
1117   ;; an XEP.
1118   (ir1-convert
1119    start next result
1120    (with-unique-names (cleanup-fun drop-thru-tag exit-tag next start count)
1121      `(flet ((,cleanup-fun ()
1122                ,@cleanup
1123                nil))
1124         ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
1125         ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
1126         ;; and something can be done to make %ESCAPE-FUN have
1127         ;; dynamic extent too.
1128         (declare (dynamic-extent #',cleanup-fun))
1129         (block ,drop-thru-tag
1130           (multiple-value-bind (,next ,start ,count)
1131               (block ,exit-tag
1132                 (%within-cleanup
1133                     :unwind-protect
1134                     (%unwind-protect (%escape-fun ,exit-tag)
1135                                      (%cleanup-fun ,cleanup-fun))
1136                   (return-from ,drop-thru-tag ,protected)))
1137             (declare (optimize (insert-debug-catch 0)))
1138             (,cleanup-fun)
1139             (%continue-unwind ,next ,start ,count)))))))
1140 \f
1141 ;;;; multiple-value stuff
1142
1143 (def-ir1-translator multiple-value-call ((fun &rest args) start next result)
1144   #!+sb-doc
1145   "MULTIPLE-VALUE-CALL function values-form*
1146
1147 Call FUNCTION, passing all the values of each VALUES-FORM as arguments,
1148 values from the first VALUES-FORM making up the first argument, etc."
1149   (let* ((ctran (make-ctran))
1150          (fun-lvar (make-lvar))
1151          (node (if args
1152                    ;; If there are arguments, MULTIPLE-VALUE-CALL
1153                    ;; turns into an MV-COMBINATION.
1154                    (make-mv-combination fun-lvar)
1155                    ;; If there are no arguments, then we convert to a
1156                    ;; normal combination, ensuring that a MV-COMBINATION
1157                    ;; always has at least one argument. This can be
1158                    ;; regarded as an optimization, but it is more
1159                    ;; important for simplifying compilation of
1160                    ;; MV-COMBINATIONS.
1161                    (make-combination fun-lvar))))
1162     (ir1-convert start ctran fun-lvar (ensure-source-fun-form fun))
1163     (setf (lvar-dest fun-lvar) node)
1164     (collect ((arg-lvars))
1165       (let ((this-start ctran))
1166         (dolist (arg args)
1167           (let ((this-ctran (make-ctran))
1168                 (this-lvar (make-lvar node)))
1169             (ir1-convert this-start this-ctran this-lvar arg)
1170             (setq this-start this-ctran)
1171             (arg-lvars this-lvar)))
1172         (link-node-to-previous-ctran node this-start)
1173         (use-continuation node next result)
1174         (setf (basic-combination-args node) (arg-lvars))))))
1175
1176 (def-ir1-translator multiple-value-prog1
1177     ((values-form &rest forms) start next result)
1178   #!+sb-doc
1179   "MULTIPLE-VALUE-PROG1 values-form form*
1180
1181 Evaluate VALUES-FORM and then the FORMS, but return all the values of
1182 VALUES-FORM."
1183   (let ((dummy (make-ctran)))
1184     (ctran-starts-block dummy)
1185     (ir1-convert start dummy result values-form)
1186     (ir1-convert-progn-body dummy next nil forms)))
1187 \f
1188 ;;;; interface to defining macros
1189
1190 ;;; Old CMUCL comment:
1191 ;;;
1192 ;;;   Return a new source path with any stuff intervening between the
1193 ;;;   current path and the first form beginning with NAME stripped
1194 ;;;   off.  This is used to hide the guts of DEFmumble macros to
1195 ;;;   prevent annoying error messages.
1196 ;;;
1197 ;;; Now that we have implementations of DEFmumble macros in terms of
1198 ;;; EVAL-WHEN, this function is no longer used.  However, it might be
1199 ;;; worth figuring out why it was used, and maybe doing analogous
1200 ;;; munging to the functions created in the expanders for the macros.
1201 (defun revert-source-path (name)
1202   (do ((path *current-path* (cdr path)))
1203       ((null path) *current-path*)
1204     (let ((first (first path)))
1205       (when (or (eq first name)
1206                 (eq first 'original-source-start))
1207         (return path)))))