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