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