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