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