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