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