Fix "Cosmetic problem" per remark in compiler/generic/parms
[sbcl.git] / src / compiler / ir1-translators.lisp
blob5cfa54894f3c2af24a237438cf4070f8c1b78d34
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 (type (member :vars :funs) definitionize-keyword))
296 (unless (listp definitions)
297 (compiler-error "Malformed ~s definitions: ~s"
298 (case definitionize-keyword
299 (:vars 'symbol-macrolet)
300 (:funs 'macrolet))
301 definitions))
302 (let* ((processed-definitions (mapcar definitionize-fun definitions))
303 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
304 ;; Do this after processing, since the definitions can be malformed.
305 (unless (= (length definitions)
306 (length (remove-duplicates definitions :key #'first)))
307 (compiler-style-warn "Duplicate definitions in ~S" definitions))
308 ;; I wonder how much of an compiler performance penalty this
309 ;; non-constant keyword is.
310 (funcall fun definitionize-keyword processed-definitions)))
312 ;;; Tweak LEXENV to include the DEFINITIONS from a MACROLET, then
313 ;;; call FUN (with no arguments).
315 ;;; This is split off from the IR1 convert method so that it can be
316 ;;; shared by the special-case top level MACROLET processing code, and
317 ;;; further split so that the special-case MACROLET processing code in
318 ;;; EVAL can likewise make use of it.
319 (defun macrolet-definitionize-fun (context lexenv)
320 (flet ((fail (control &rest args)
321 (ecase context
322 (:compile (apply #'compiler-error control args))
323 (:eval (error 'simple-program-error
324 :format-control control
325 :format-arguments args)))))
326 (lambda (definition)
327 (unless (list-of-length-at-least-p definition 2)
328 (fail "The list ~S is too short to be a legal local macro definition."
329 definition))
330 (destructuring-bind (name arglist &body body) definition
331 (unless (symbolp name)
332 (fail "The local macro name ~S is not a symbol." name))
333 (when (fboundp name)
334 (program-assert-symbol-home-package-unlocked
335 context name "binding ~A as a local macro"))
336 (unless (listp arglist)
337 (fail "The local macro argument list ~S is not a list."
338 arglist))
339 `(,name macro .
340 ,(compile-in-lexenv
342 (make-macro-lambda nil arglist body 'macrolet name)
343 lexenv))))))
345 (defun funcall-in-macrolet-lexenv (definitions fun context)
346 (%funcall-in-foomacrolet-lexenv
347 (macrolet-definitionize-fun context (make-restricted-lexenv *lexenv*))
348 :funs
349 definitions
350 fun))
352 (def-ir1-translator macrolet ((definitions &rest body) start next result)
353 #!+sb-doc
354 "MACROLET ({(name lambda-list form*)}*) body-form*
356 Evaluate the BODY-FORMS in an environment with the specified local macros
357 defined. NAME is the local macro name, LAMBDA-LIST is a DEFMACRO style
358 destructuring lambda list, and the FORMS evaluate to the expansion."
359 (funcall-in-macrolet-lexenv
360 definitions
361 (lambda (&key funs)
362 (declare (ignore funs))
363 (ir1-translate-locally body start next result))
364 :compile))
366 (defun symbol-macrolet-definitionize-fun (context)
367 (flet ((fail (control &rest args)
368 (ecase context
369 (:compile (apply #'compiler-error control args))
370 (:eval (error 'simple-program-error
371 :format-control control
372 :format-arguments args)))))
373 (lambda (definition)
374 (unless (proper-list-of-length-p definition 2)
375 (fail "malformed symbol/expansion pair: ~S" definition))
376 (destructuring-bind (name expansion) definition
377 (unless (symbolp name)
378 (fail "The local symbol macro name ~S is not a symbol." name))
379 (when (or (boundp name) (eq (info :variable :kind name) :macro))
380 (program-assert-symbol-home-package-unlocked
381 context name "binding ~A as a local symbol-macro"))
382 (let ((kind (info :variable :kind name)))
383 (when (member kind '(:special :constant :global))
384 (fail "Attempt to bind a ~(~A~) variable with SYMBOL-MACROLET: ~S"
385 kind name)))
386 ;; A magical cons that MACROEXPAND-1 understands.
387 `(,name . (macro . ,expansion))))))
389 (defun funcall-in-symbol-macrolet-lexenv (definitions fun context)
390 (%funcall-in-foomacrolet-lexenv
391 (symbol-macrolet-definitionize-fun context)
392 :vars
393 definitions
394 fun))
396 (def-ir1-translator symbol-macrolet
397 ((macrobindings &body body) start next result)
398 #!+sb-doc
399 "SYMBOL-MACROLET ({(name expansion)}*) decl* form*
401 Define the NAMES as symbol macros with the given EXPANSIONS. Within the
402 body, references to a NAME will effectively be replaced with the EXPANSION."
403 (funcall-in-symbol-macrolet-lexenv
404 macrobindings
405 (lambda (&key vars)
406 (ir1-translate-locally body start next result :vars vars))
407 :compile))
409 ;;;; %PRIMITIVE
410 ;;;;
411 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
412 ;;;; into a funny function.
414 ;;; Carefully evaluate a list of forms, returning a list of the results.
415 (defun eval-info-args (args)
416 (declare (list args))
417 (handler-case (mapcar #'eval args)
418 (error (condition)
419 (compiler-error "Lisp error during evaluation of info args:~%~A"
420 condition))))
422 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
423 ;;; the template, the second is a list of the results of any
424 ;;; codegen-info args, and the remaining arguments are the runtime
425 ;;; arguments.
427 ;;; We do various error checking now so that we don't bomb out with
428 ;;; a fatal error during IR2 conversion.
430 ;;; KLUDGE: It's confusing having multiple names floating around for
431 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
432 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
433 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
434 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
435 ;;; VOP or %VOP.. -- WHN 2001-06-11
436 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
437 (def-ir1-translator %primitive ((name &rest args) start next result)
438 (declare (type symbol name))
439 (let* ((template (or (gethash name *backend-template-names*)
440 (bug "undefined primitive ~A" name)))
441 (required (length (template-arg-types template)))
442 (info (template-info-arg-count template))
443 (min (+ required info))
444 (nargs (length args)))
445 (if (template-more-args-type template)
446 (when (< nargs min)
447 (bug "Primitive ~A was called with ~R argument~:P, ~
448 but wants at least ~R."
449 name
450 nargs
451 min))
452 (unless (= nargs min)
453 (bug "Primitive ~A was called with ~R argument~:P, ~
454 but wants exactly ~R."
455 name
456 nargs
457 min)))
459 (when (template-conditional-p template)
460 (bug "%PRIMITIVE was used with a conditional template."))
462 (when (template-more-results-type template)
463 (bug "%PRIMITIVE was used with an unknown values template."))
465 (ir1-convert start next result
466 `(%%primitive ',template
467 ',(eval-info-args
468 (subseq args required min))
469 ,@(subseq args 0 required)
470 ,@(subseq args min)))))
472 ;;;; QUOTE
474 (def-ir1-translator quote ((thing) start next result)
475 #!+sb-doc
476 "QUOTE value
478 Return VALUE without evaluating it."
479 (reference-constant start next result thing))
481 (defun name-context ()
482 ;; Name of the outermost non-NIL BLOCK, or the source namestring
483 ;; of the source file.
484 (let ((context
485 (or (car (find-if (lambda (b)
486 (let ((name (pop b)))
487 (and name
488 ;; KLUDGE: High debug adds this block on
489 ;; some platforms.
490 #!-unwind-to-frame-and-call-vop
491 (neq 'return-value-tag name)
492 ;; KLUDGE: CATCH produces blocks whose
493 ;; cleanup is :CATCH.
494 (neq :catch (cleanup-kind (entry-cleanup (pop b)))))))
495 (lexenv-blocks *lexenv*) :from-end t))
496 *source-namestring*
497 (let* ((p (or sb!xc:*compile-file-truename* *load-truename*)))
498 (when p
499 #+sb-xc-host (lpnify-namestring (namestring p) (pathname-directory p) (pathname-type p))
500 #-sb-xc-host (namestring p))))))
501 (when context
502 (list :in context))))
504 ;;;; FUNCTION and NAMED-LAMBDA
505 (defun name-lambdalike (thing)
506 (case (car thing)
507 ((named-lambda)
508 (or (second thing)
509 `(lambda ,(strip-lambda-list (third thing) :name) ,(name-context))))
510 ((lambda)
511 `(lambda ,(strip-lambda-list (second thing) :name) ,@(name-context)))
512 ((lambda-with-lexenv)
513 ;; FIXME: Get the original DEFUN name here.
514 `(lambda ,(fifth thing)))
515 (otherwise
516 (compiler-error "Not a valid lambda expression:~% ~S"
517 thing))))
519 (defun fun-name-leaf (thing)
520 (cond
521 ((typep thing
522 '(cons (member lambda named-lambda lambda-with-lexenv)))
523 (values (ir1-convert-lambdalike
524 thing :debug-name (name-lambdalike thing))
526 ((legal-fun-name-p thing)
527 (values (find-lexically-apparent-fun
528 thing "as the argument to FUNCTION")
529 nil))
531 (compiler-error "~S is not a legal function name." thing))))
533 (def-ir1-translator %%allocate-closures ((&rest leaves) start next result)
534 (aver (eq result 'nil))
535 (let ((lambdas leaves))
536 (ir1-convert start next result `(%allocate-closures ',lambdas))
537 (let ((allocator (node-dest (ctran-next start))))
538 (dolist (lambda lambdas)
539 (setf (functional-allocator lambda) allocator)))))
541 (defmacro with-fun-name-leaf ((leaf thing start &key global-function) &body body)
542 `(multiple-value-bind (,leaf allocate-p)
543 (if ,global-function
544 (find-global-fun ,thing t)
545 (fun-name-leaf ,thing))
546 (if allocate-p
547 (let ((.new-start. (make-ctran)))
548 (ir1-convert ,start .new-start. nil `(%%allocate-closures ,leaf))
549 (let ((,start .new-start.))
550 ,@body))
551 (locally
552 ,@body))))
554 (def-ir1-translator function ((thing) start next result)
555 #!+sb-doc
556 "FUNCTION name
558 Return the lexically apparent definition of the function NAME. NAME may also
559 be a lambda expression."
560 (with-fun-name-leaf (leaf thing start)
561 (reference-leaf start next result leaf)))
563 ;;; Like FUNCTION, but ignores local definitions and inline
564 ;;; expansions, and doesn't nag about undefined functions.
565 ;;; Used for optimizing things like (FUNCALL 'FOO).
566 (def-ir1-translator global-function ((thing) start next result)
567 (with-fun-name-leaf (leaf thing start :global-function t)
568 (reference-leaf start next result leaf)))
570 (defun constant-global-fun-name (thing)
571 (let ((constantp (sb!xc:constantp thing)))
572 (when constantp
573 (let ((name (constant-form-value thing)))
574 (when (legal-fun-name-p name)
575 name)))))
577 (defun lvar-constant-global-fun-name (lvar)
578 (when (constant-lvar-p lvar)
579 (let ((name (lvar-value lvar)))
580 (when (legal-fun-name-p name)
581 name))))
583 (defun ensure-source-fun-form (source &optional give-up)
584 (let ((op (when (consp source) (car source))))
585 (cond ((eq op '%coerce-callable-to-fun)
586 (ensure-source-fun-form (second source)))
587 ((member op '(function global-function lambda named-lambda))
588 (values source nil))
590 (let ((cname (constant-global-fun-name source)))
591 (if cname
592 (values `(global-function ,cname) nil)
593 (values `(%coerce-callable-to-fun ,source) give-up)))))))
595 (defun ensure-lvar-fun-form (lvar lvar-name &optional give-up)
596 (aver (and lvar-name (symbolp lvar-name)))
597 (if (csubtypep (lvar-type lvar) (specifier-type 'function))
598 lvar-name
599 (let ((cname (lvar-constant-global-fun-name lvar)))
600 (cond (cname
601 `(global-function ,cname))
602 (give-up
603 (give-up-ir1-transform "not known to be a function"))
605 `(%coerce-callable-to-fun ,lvar-name))))))
607 ;;;; FUNCALL
609 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
610 ;;; (not symbols). %FUNCALL is used directly in some places where the
611 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
612 (deftransform funcall ((function &rest args) * *)
613 (let ((arg-names (make-gensym-list (length args))))
614 `(lambda (function ,@arg-names)
615 (declare (ignorable function))
616 `(%funcall ,(ensure-lvar-fun-form function 'function) ,@arg-names))))
618 (def-ir1-translator %funcall ((function &rest args) start next result)
619 ;; MACROEXPAND so that (LAMBDA ...) forms arriving here don't get an
620 ;; extra cast inserted for them.
621 (let ((function (%macroexpand function *lexenv*)))
622 (if (typep function '(cons (member function global-function) (cons t null)))
623 (with-fun-name-leaf (leaf (cadr function) start
624 :global-function (eq (car function)
625 'global-function))
626 (ir1-convert start next result `(,leaf ,@args)))
627 (let ((ctran (make-ctran))
628 (fun-lvar (make-lvar)))
629 (ir1-convert start ctran fun-lvar `(the function ,function))
630 (ir1-convert-combination-args fun-lvar ctran next result args)))))
632 ;;; This source transform exists to reduce the amount of work for the
633 ;;; compiler. If the called function is a FUNCTION form, then convert
634 ;;; directly to %FUNCALL, instead of waiting around for type
635 ;;; inference.
636 (define-source-transform funcall (function &rest args)
637 `(%funcall ,(ensure-source-fun-form function) ,@args))
639 (deftransform %coerce-callable-to-fun ((thing) * * :node node)
640 "optimize away possible call to FDEFINITION at runtime"
641 (ensure-lvar-fun-form thing 'thing t))
643 (define-source-transform %coerce-callable-to-fun (thing)
644 (ensure-source-fun-form thing t))
646 ;;;; LET and LET*
647 ;;;;
648 ;;;; (LET and LET* can't be implemented as macros due to the fact that
649 ;;;; any pervasive declarations also affect the evaluation of the
650 ;;;; arguments.)
652 ;;; Given a list of binding specifiers in the style of LET, return:
653 ;;; 1. The list of var structures for the variables bound.
654 ;;; 2. The initial value form for each variable.
656 ;;; The variable names are checked for legality and globally special
657 ;;; variables are marked as such. Context is the name of the form, for
658 ;;; error reporting purposes.
659 (declaim (ftype (function (list symbol) (values list list))
660 extract-let-vars))
661 (defun extract-let-vars (bindings context)
662 (collect ((vars)
663 (vals)
664 (names))
665 (flet ((get-var (name)
666 (varify-lambda-arg name
667 (if (eq context 'let*)
669 (names))
670 context)))
671 (dolist (spec bindings)
672 (cond ((atom spec)
673 (let ((var (get-var spec)))
674 (vars var)
675 (names spec)
676 (vals nil)))
678 (unless (proper-list-of-length-p spec 1 2)
679 (compiler-error "The ~S binding spec ~S is malformed."
680 context
681 spec))
682 (let* ((name (first spec))
683 (var (get-var name)))
684 (vars var)
685 (names name)
686 (vals (second spec)))))))
687 (dolist (name (names))
688 (when (eq (info :variable :kind name) :macro)
689 (program-assert-symbol-home-package-unlocked
690 :compile name "lexically binding symbol-macro ~A")))
691 (values (vars) (vals))))
693 (def-ir1-translator let ((bindings &body body) start next result)
694 #!+sb-doc
695 "LET ({(var [value]) | var}*) declaration* form*
697 During evaluation of the FORMS, bind the VARS to the result of evaluating the
698 VALUE forms. The variables are bound in parallel after all of the VALUES forms
699 have been evaluated."
700 (cond ((null bindings)
701 (ir1-translate-locally body start next result))
702 ((listp bindings)
703 (multiple-value-bind (forms decls) (parse-body body nil)
704 (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
705 (binding* ((ctran (make-ctran))
706 (fun-lvar (make-lvar))
707 ((next result)
708 (processing-decls (decls vars nil next result
709 post-binding-lexenv)
710 (let ((fun (ir1-convert-lambda-body
711 forms
712 vars
713 :post-binding-lexenv post-binding-lexenv
714 :debug-name (debug-name 'let bindings))))
715 (reference-leaf start ctran fun-lvar fun))
716 (values next result))))
717 (ir1-convert-combination-args fun-lvar ctran next result values)))))
719 (compiler-error "Malformed LET bindings: ~S." bindings))))
721 (def-ir1-translator let* ((bindings &body body)
722 start next result)
723 #!+sb-doc
724 "LET* ({(var [value]) | var}*) declaration* form*
726 Similar to LET, but the variables are bound sequentially, allowing each VALUE
727 form to reference any of the previous VARS."
728 (if (listp bindings)
729 (multiple-value-bind (forms decls) (parse-body body nil)
730 (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
731 (processing-decls (decls vars nil next result post-binding-lexenv)
732 (ir1-convert-aux-bindings start
733 next
734 result
735 forms
736 vars
737 values
738 post-binding-lexenv))))
739 (compiler-error "Malformed LET* bindings: ~S." bindings)))
741 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
742 ;;; and SYMBOL-MACROLET
744 ;;; Note that all these things need to preserve toplevel-formness,
745 ;;; but we don't need to worry about that within an IR1 translator,
746 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
747 ;;; forms before we hit the IR1 transform level.
748 (defun ir1-translate-locally (body start next result &key vars funs)
749 (declare (type ctran start next) (type (or lvar null) result)
750 (type list body))
751 (multiple-value-bind (forms decls) (parse-body body nil)
752 (processing-decls (decls vars funs next result)
753 (ir1-convert-progn-body start next result forms))))
755 (def-ir1-translator locally ((&body body) start next result)
756 #!+sb-doc
757 "LOCALLY declaration* form*
759 Sequentially evaluate the FORMS in a lexical environment where the
760 DECLARATIONS have effect. If LOCALLY is a top level form, then the FORMS are
761 also processed as top level forms."
762 (ir1-translate-locally body start next result))
764 ;;;; FLET and LABELS
766 ;;; Given a list of local function specifications in the style of
767 ;;; FLET, return lists of the function names and of the lambdas which
768 ;;; are their definitions.
770 ;;; The function names are checked for legality. CONTEXT is the name
771 ;;; of the form, for error reporting.
772 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
773 (defun extract-flet-vars (definitions context)
774 (collect ((names)
775 (defs))
776 (dolist (def definitions)
777 (when (or (atom def) (< (length def) 2))
778 (compiler-error "The ~S definition spec ~S is malformed." context def))
780 (let ((name (first def)))
781 (check-fun-name name)
782 (when (fboundp name)
783 (program-assert-symbol-home-package-unlocked
784 :compile name "binding ~A as a local function"))
785 (names name)
786 (multiple-value-bind (forms decls doc) (parse-body (cddr def) t)
787 (defs `(lambda ,(second def)
788 ,@(when doc (list doc))
789 ,@decls
790 (block ,(fun-name-block-name name)
791 . ,forms))))))
792 (values (names) (defs))))
794 (defun ir1-convert-fbindings (start next result funs body)
795 (let ((ctran (make-ctran))
796 (dx-p (find-if #'leaf-dynamic-extent funs)))
797 (when dx-p
798 (ctran-starts-block ctran)
799 (ctran-starts-block next))
800 (ir1-convert start ctran nil `(%%allocate-closures ,@funs))
801 (cond (dx-p
802 (let* ((dummy (make-ctran))
803 (entry (make-entry))
804 (cleanup (make-cleanup :kind :dynamic-extent
805 :mess-up entry
806 :info (list (node-dest
807 (ctran-next start))))))
808 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
809 (setf (entry-cleanup entry) cleanup)
810 (link-node-to-previous-ctran entry ctran)
811 (use-ctran entry dummy)
813 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
814 (ir1-convert-progn-body dummy next result body))))
815 (t (ir1-convert-progn-body ctran next result body)))))
817 (def-ir1-translator flet ((definitions &body body)
818 start next result)
819 #!+sb-doc
820 "FLET ({(name lambda-list declaration* form*)}*) declaration* body-form*
822 Evaluate the BODY-FORMS with local function definitions. The bindings do
823 not enclose the definitions; any use of NAME in the FORMS will refer to the
824 lexically apparent function definition in the enclosing environment."
825 (multiple-value-bind (forms decls) (parse-body body nil)
826 (unless (listp definitions)
827 (compiler-error "Malformed FLET definitions: ~s" definitions))
828 (multiple-value-bind (names defs)
829 (extract-flet-vars definitions 'flet)
830 (let ((fvars (mapcar (lambda (n d)
831 (ir1-convert-lambda
832 d :source-name n
833 :maybe-add-debug-catch t
834 :debug-name
835 (debug-name 'flet n t)))
836 names defs)))
837 (processing-decls (decls nil fvars next result)
838 (let ((*lexenv* (make-lexenv :funs (pairlis names fvars))))
839 (ir1-convert-fbindings start next result fvars forms)))))))
841 (def-ir1-translator labels ((definitions &body body) start next result)
842 #!+sb-doc
843 "LABELS ({(name lambda-list declaration* form*)}*) declaration* body-form*
845 Evaluate the BODY-FORMS with local function definitions. The bindings enclose
846 the new definitions, so the defined functions can call themselves or each
847 other."
848 (multiple-value-bind (forms decls) (parse-body body nil)
849 (unless (listp definitions)
850 (compiler-error "Malformed LABELS definitions: ~s" definitions))
851 (multiple-value-bind (names defs)
852 (extract-flet-vars definitions 'labels)
853 (let* (;; dummy LABELS functions, to be used as placeholders
854 ;; during construction of real LABELS functions
855 (placeholder-funs (mapcar (lambda (name)
856 (make-functional
857 :%source-name name
858 :%debug-name (debug-name
859 'labels-placeholder
860 name)))
861 names))
862 ;; (like PAIRLIS but guaranteed to preserve ordering:)
863 (placeholder-fenv (mapcar #'cons names placeholder-funs))
864 ;; the real LABELS functions, compiled in a LEXENV which
865 ;; includes the dummy LABELS functions
866 (real-funs
867 (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
868 (mapcar (lambda (name def)
869 (ir1-convert-lambda def
870 :source-name name
871 :maybe-add-debug-catch t
872 :debug-name (debug-name 'labels name t)))
873 names defs))))
875 ;; Modify all the references to the dummy function leaves so
876 ;; that they point to the real function leaves.
877 (loop for real-fun in real-funs and
878 placeholder-cons in placeholder-fenv do
879 (substitute-leaf real-fun (cdr placeholder-cons))
880 (setf (cdr placeholder-cons) real-fun))
882 ;; Voila.
883 (processing-decls (decls nil real-funs next result)
884 (let ((*lexenv* (make-lexenv
885 ;; Use a proper FENV here (not the
886 ;; placeholder used earlier) so that if the
887 ;; lexical environment is used for inline
888 ;; expansion we'll get the right functions.
889 :funs (pairlis names real-funs))))
890 (ir1-convert-fbindings start next result real-funs forms)))))))
893 ;;;; the THE special operator, and friends
895 ;;; A logic shared among THE and TRULY-THE.
896 (defun the-in-policy (type value policy start next result)
897 (let ((type (if (ctype-p type) type
898 (compiler-values-specifier-type type))))
899 (cond ((or (eq type *wild-type*)
900 (eq type *universal-type*)
901 (and (leaf-p value)
902 (values-subtypep (make-single-value-type (leaf-type value))
903 type))
904 (and (sb!xc:constantp value)
905 (or (not (values-type-p type))
906 (values-type-may-be-single-value-p type))
907 (ctypep (constant-form-value value)
908 (single-value-type type))))
909 (ir1-convert start next result value))
910 (t (let ((value-ctran (make-ctran))
911 (value-lvar (make-lvar)))
912 (ir1-convert start value-ctran value-lvar value)
913 (let ((cast (make-cast value-lvar type policy)))
914 (link-node-to-previous-ctran cast value-ctran)
915 (setf (lvar-dest value-lvar) cast)
916 (use-continuation cast next result)))))))
918 ;;; Assert that FORM evaluates to the specified type (which may be a
919 ;;; VALUES type). TYPE may be a type specifier or (as a hack) a CTYPE.
920 (def-ir1-translator the ((value-type form) start next result)
921 #!+sb-doc
922 "Specifies that the values returned by FORM conform to the VALUE-TYPE.
924 CLHS specifies that the consequences are undefined if any result is
925 not of the declared type, but SBCL treats declarations as assertions
926 as long as SAFETY is at least 2, in which case incorrect type
927 information will result in a runtime type-error instead of leading to
928 eg. heap corruption. This is however expressly non-portable: use
929 CHECK-TYPE instead of THE to catch type-errors at runtime. THE is best
930 considered an optimization tool to inform the compiler about types it
931 is unable to derive from other declared types."
932 (the-in-policy value-type form (lexenv-policy *lexenv*) start next result))
934 ;;; This is like the THE special form, except that it believes
935 ;;; whatever you tell it. It will never generate a type check, but
936 ;;; will cause a warning if the compiler can prove the assertion is
937 ;;; wrong.
939 ;;; For the benefit of code-walkers we also add a macro-expansion. (Using INFO
940 ;;; directly to get around safeguards for adding a macro-expansion for special
941 ;;; operator.) Because :FUNCTION :KIND remains :SPECIAL-FORM, the compiler
942 ;;; never uses the macro -- but manually calling its MACRO-FUNCTION or
943 ;;; MACROEXPANDing TRULY-THE forms does.
944 (def-ir1-translator truly-the ((value-type form) start next result)
945 #!+sb-doc
946 "Specifies that the values returned by FORM conform to the
947 VALUE-TYPE, and causes the compiler to trust this information
948 unconditionally.
950 Consequences are undefined if any result is not of the declared type
951 -- typical symptoms including memory corruptions. Use with great
952 care."
953 (the-in-policy value-type form **zero-typecheck-policy** start next result))
955 (def-ir1-translator bound-cast ((array bound index) start next result)
956 (let ((check-bound-tran (make-ctran))
957 (index-ctran (make-ctran))
958 (index-lvar (make-lvar)))
959 ;; CHECK-BOUND transform ensure that INDEX won't be evaluated twice
960 (ir1-convert start check-bound-tran nil `(%check-bound ,array ,bound ,index))
961 (ir1-convert check-bound-tran index-ctran index-lvar index)
962 (let* ((check-bound-combination (ctran-use check-bound-tran))
963 (array (first (combination-args check-bound-combination)))
964 (bound (second (combination-args check-bound-combination)))
965 (derived (constant-lvar-p bound))
966 (type (specifier-type (if derived
967 `(integer 0 (,(lvar-value bound)))
968 '(and unsigned-byte fixnum))))
969 (cast (make-bound-cast :value index-lvar
970 :asserted-type type
971 :type-to-check type
972 :derived-type (coerce-to-values type)
973 :check check-bound-combination
974 :derived derived
975 :array array
976 :bound bound)))
977 (link-node-to-previous-ctran cast index-ctran)
978 (setf (lvar-dest index-lvar) cast)
979 (use-continuation cast next result))))
980 #-sb-xc-host
981 (setf (info :function :macro-function 'truly-the)
982 (lambda (whole env)
983 (declare (ignore env))
984 `(the ,@(cdr whole))))
986 ;;;; SETQ
988 (defun explode-setq (form err-fun)
989 (collect ((sets))
990 (do ((op (car form))
991 (thing (cdr form) (cddr thing)))
992 ((endp thing) (sets))
993 (if (endp (cdr thing))
994 (funcall err-fun "odd number of args to ~A: ~S" op form)
995 (sets `(,op ,(first thing) ,(second thing)))))))
997 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
998 ;;; look at the global information. If the name is for a constant,
999 ;;; then error out.
1000 (def-ir1-translator setq ((&whole source &rest things) start next result)
1001 (if (proper-list-of-length-p things 2)
1002 (let* ((name (first things))
1003 (value-form (second things))
1004 (leaf (or (lexenv-find name vars) (find-free-var name))))
1005 (etypecase leaf
1006 (leaf
1007 (when (constant-p leaf)
1008 (compiler-error "~S is a constant and thus can't be set." name))
1009 (when (lambda-var-p leaf)
1010 (let ((home-lambda (ctran-home-lambda-or-null start)))
1011 (when home-lambda
1012 (sset-adjoin leaf (lambda-calls-or-closes home-lambda))))
1013 (when (lambda-var-ignorep leaf)
1014 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
1015 ;; requires that this be a STYLE-WARNING, not a full warning.
1016 (compiler-style-warn
1017 "~S is being set even though it was declared to be ignored."
1018 name)))
1019 (if (and (global-var-p leaf) (eq :unknown (global-var-kind leaf)))
1020 ;; For undefined variables go through SET, so that we can catch
1021 ;; constant modifications.
1022 (ir1-convert start next result `(set ',name ,value-form))
1023 (setq-var start next result leaf value-form)))
1024 (cons
1025 (aver (eq (car leaf) 'macro))
1026 ;; Allow *MACROEXPAND-HOOK* to see NAME get expanded,
1027 ;; not just see a use of SETF on the new place.
1028 (ir1-convert start next result `(setf ,name ,(second things))))
1029 (heap-alien-info
1030 (ir1-convert start next result
1031 `(%set-heap-alien ',leaf ,(second things))))))
1032 (ir1-convert-progn-body start next result
1033 (explode-setq source 'compiler-error))))
1035 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
1036 ;;; This should only need to be called in SETQ.
1037 (defun setq-var (start next result var value)
1038 (declare (type ctran start next) (type (or lvar null) result)
1039 (type basic-var var))
1040 (let ((dest-ctran (make-ctran))
1041 (dest-lvar (make-lvar))
1042 (type (or (lexenv-find var type-restrictions)
1043 (leaf-type var))))
1044 (ir1-convert start dest-ctran dest-lvar `(the ,(type-specifier type)
1045 ,value))
1046 (let ((res (make-set :var var :value dest-lvar)))
1047 (setf (lvar-dest dest-lvar) res)
1048 (setf (leaf-ever-used var) t)
1049 (push res (basic-var-sets var))
1050 (link-node-to-previous-ctran res dest-ctran)
1051 (use-continuation res next result))))
1053 ;;;; CATCH, THROW and UNWIND-PROTECT
1055 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
1056 ;;; since as as far as IR1 is concerned, it has no interesting
1057 ;;; properties other than receiving multiple-values.
1058 (def-ir1-translator throw ((tag result) start next result-lvar)
1059 #!+sb-doc
1060 "THROW tag form
1062 Do a non-local exit, return the values of FORM from the CATCH whose tag is EQ
1063 to TAG."
1064 (ir1-convert start next result-lvar
1065 `(multiple-value-call #'%throw ,tag ,result)))
1067 ;;; This is a special special form used to instantiate a cleanup as
1068 ;;; the current cleanup within the body. KIND is the kind of cleanup
1069 ;;; to make, and MESS-UP is a form that does the mess-up action. We
1070 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
1071 ;;; and introduce the cleanup into the lexical environment. We
1072 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
1073 ;;; cleanup, since this inner cleanup is the interesting one.
1074 (def-ir1-translator %within-cleanup
1075 ((kind mess-up &body body) start next result)
1076 (let ((dummy (make-ctran))
1077 (dummy2 (make-ctran)))
1078 (ir1-convert start dummy nil mess-up)
1079 (let* ((mess-node (ctran-use dummy))
1080 (cleanup (make-cleanup :kind kind
1081 :mess-up mess-node))
1082 (old-cup (lexenv-cleanup *lexenv*))
1083 (*lexenv* (make-lexenv :cleanup cleanup)))
1084 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
1085 (ir1-convert dummy dummy2 nil '(%cleanup-point))
1086 (ir1-convert-progn-body dummy2 next result body))))
1088 ;;; This is a special special form that makes an "escape function"
1089 ;;; which returns unknown values from named block. We convert the
1090 ;;; function, set its kind to :ESCAPE, and then reference it. The
1091 ;;; :ESCAPE kind indicates that this function's purpose is to
1092 ;;; represent a non-local control transfer, and that it might not
1093 ;;; actually have to be compiled.
1095 ;;; Note that environment analysis replaces references to escape
1096 ;;; functions with references to the corresponding NLX-INFO structure.
1097 (def-ir1-translator %escape-fun ((tag) start next result)
1098 (let ((fun (let ((*allow-instrumenting* nil))
1099 (ir1-convert-lambda
1100 `(lambda ()
1101 (return-from ,tag (%unknown-values)))
1102 :debug-name (debug-name 'escape-fun tag))))
1103 (ctran (make-ctran)))
1104 (setf (functional-kind fun) :escape)
1105 (ir1-convert start ctran nil `(%%allocate-closures ,fun))
1106 (reference-leaf ctran next result fun)))
1108 ;;; Yet another special special form. This one looks up a local
1109 ;;; function and smashes it to a :CLEANUP function, as well as
1110 ;;; referencing it.
1111 (def-ir1-translator %cleanup-fun ((name) start next result)
1112 ;; FIXME: Should this not be :TEST #'EQUAL? What happens to
1113 ;; (SETF FOO) here?
1114 (let ((fun (lexenv-find name funs)))
1115 (aver (lambda-p fun))
1116 (setf (functional-kind fun) :cleanup)
1117 (reference-leaf start next result fun)))
1119 (def-ir1-translator catch ((tag &body body) start next result)
1120 #!+sb-doc
1121 "CATCH tag form*
1123 Evaluate TAG and instantiate it as a catcher while the body forms are
1124 evaluated in an implicit PROGN. If a THROW is done to TAG within the dynamic
1125 scope of the body, then control will be transferred to the end of the body and
1126 the thrown values will be returned."
1127 ;; We represent the possibility of the control transfer by making an
1128 ;; "escape function" that does a lexical exit, and instantiate the
1129 ;; cleanup using %WITHIN-CLEANUP.
1130 (ir1-convert
1131 start next result
1132 (with-unique-names (exit-block)
1133 `(block ,exit-block
1134 (%within-cleanup
1135 :catch (%catch (%escape-fun ,exit-block) ,tag)
1136 ,@body)))))
1138 (def-ir1-translator unwind-protect
1139 ((protected &body cleanup) start next result)
1140 #!+sb-doc
1141 "UNWIND-PROTECT protected cleanup*
1143 Evaluate the form PROTECTED, returning its values. The CLEANUP forms are
1144 evaluated whenever the dynamic scope of the PROTECTED form is exited (either
1145 due to normal completion or a non-local exit such as THROW)."
1146 ;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
1147 ;; cleanup forms into a local function so that they can be referenced
1148 ;; both in the case where we are unwound and in any local exits. We
1149 ;; use %CLEANUP-FUN on this to indicate that reference by
1150 ;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
1151 ;; an XEP.
1152 (ir1-convert
1153 start next result
1154 (with-unique-names (cleanup-fun drop-thru-tag exit-tag next start count)
1155 `(flet ((,cleanup-fun ()
1156 ,@cleanup
1157 nil))
1158 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
1159 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
1160 ;; and something can be done to make %ESCAPE-FUN have
1161 ;; dynamic extent too.
1162 (declare (dynamic-extent #',cleanup-fun))
1163 (block ,drop-thru-tag
1164 (multiple-value-bind (,next ,start ,count)
1165 (block ,exit-tag
1166 (%within-cleanup
1167 :unwind-protect
1168 (%unwind-protect (%escape-fun ,exit-tag)
1169 (%cleanup-fun ,cleanup-fun))
1170 (return-from ,drop-thru-tag ,protected)))
1171 (declare (optimize (insert-debug-catch 0)))
1172 (,cleanup-fun)
1173 (%continue-unwind ,next ,start ,count)))))))
1175 ;;;; multiple-value stuff
1177 (def-ir1-translator multiple-value-call ((fun &rest args) start next result)
1178 #!+sb-doc
1179 "MULTIPLE-VALUE-CALL function values-form*
1181 Call FUNCTION, passing all the values of each VALUES-FORM as arguments,
1182 values from the first VALUES-FORM making up the first argument, etc."
1183 (let* ((ctran (make-ctran))
1184 (fun-lvar (make-lvar))
1185 (node (if args
1186 ;; If there are arguments, MULTIPLE-VALUE-CALL
1187 ;; turns into an MV-COMBINATION.
1188 (make-mv-combination fun-lvar)
1189 ;; If there are no arguments, then we convert to a
1190 ;; normal combination, ensuring that a MV-COMBINATION
1191 ;; always has at least one argument. This can be
1192 ;; regarded as an optimization, but it is more
1193 ;; important for simplifying compilation of
1194 ;; MV-COMBINATIONS.
1195 (make-combination fun-lvar))))
1196 (ir1-convert start ctran fun-lvar (ensure-source-fun-form fun))
1197 (setf (lvar-dest fun-lvar) node)
1198 (collect ((arg-lvars))
1199 (let ((this-start ctran))
1200 (dolist (arg args)
1201 (let ((this-ctran (make-ctran))
1202 (this-lvar (make-lvar node)))
1203 (ir1-convert this-start this-ctran this-lvar arg)
1204 (setq this-start this-ctran)
1205 (arg-lvars this-lvar)))
1206 (link-node-to-previous-ctran node this-start)
1207 (use-continuation node next result)
1208 (setf (basic-combination-args node) (arg-lvars))))))
1210 (def-ir1-translator multiple-value-prog1
1211 ((values-form &rest forms) start next result)
1212 #!+sb-doc
1213 "MULTIPLE-VALUE-PROG1 values-form form*
1215 Evaluate VALUES-FORM and then the FORMS, but return all the values of
1216 VALUES-FORM."
1217 (let ((dummy (make-ctran)))
1218 (ctran-starts-block dummy)
1219 (ir1-convert start dummy result values-form)
1220 (ir1-convert-progn-body dummy next nil forms)))
1222 ;;;; interface to defining macros
1224 ;;; Old CMUCL comment:
1226 ;;; Return a new source path with any stuff intervening between the
1227 ;;; current path and the first form beginning with NAME stripped
1228 ;;; off. This is used to hide the guts of DEFmumble macros to
1229 ;;; prevent annoying error messages.
1231 ;;; Now that we have implementations of DEFmumble macros in terms of
1232 ;;; EVAL-WHEN, this function is no longer used. However, it might be
1233 ;;; worth figuring out why it was used, and maybe doing analogous
1234 ;;; munging to the functions created in the expanders for the macros.
1235 (defun revert-source-path (name)
1236 (do ((path *current-path* (cdr path)))
1237 ((null path) *current-path*)
1238 (let ((first (first path)))
1239 (when (or (eq first name)
1240 (eq first 'original-source-start))
1241 (return path)))))