Call maybe-terminate-block when translating THE.
[sbcl.git] / src / compiler / ir1-translators.lisp
blob2fd9a675700997c79f7232fe7f19f20922729379
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 ;;; We now have a switch to decide whether relative pathnames
467 ;;; can be stored in fasl files as their source name.
468 ;;; Regardless of what ANSI says must be bound to *fooNAME* specials,
469 ;;; the only sane choice for this switch is to use untruenames,
470 ;;; because compilers should record source filenames _as_given_ for
471 ;;; later consumption by linkers and debuggers. Attempting to convert
472 ;;; to a truename fails miserably on symlink forests and
473 ;;; content-addressable filesystems. Compare to a typical C compiler:
474 ;;; $ mkdir -p /tmp/a/b/c/d/e/f/g
475 ;;; $ cd /tmp/a/b/c/d
476 ;;; $ touch e/f/g/h.i
477 ;;; $ cc -g3 -S -o - e/f/g/h.i | grep file
478 ;;; .file "h.i"
479 ;;; .file 1 "e/f/g/h.i"
480 ;;; It has emitted a debug info with just a tail, and a debug info with
481 ;;; the unaltered pathname, and not a truename in sight.
483 ;;; But in Lisp we merge pathnames with the defaults, because CLHS says that
484 ;;; "*compile-file-pathname* ... is bound to (pathname (merge-pathnames input-file))."
485 ;;; and similarly for *load-pathname*.
486 ;;; It's an implementation detail that we use the same information
487 ;;; to bind *compile-file-pathname* and to store in the fasl though.
488 ;;; But CLHS does *NOT* say that that the pathname of a source files as
489 ;;; represented in its corresponding compiled object is a fully merged name
490 ;;; - how would it? obtaining the source path isn't a specified thing -
491 ;;; and that's our loophole to allow the choice.
492 ;;; Another loophole is that *DEFAULT-PATHNAME-DEFAULTS* can always be #"".
494 ;;; We could invent internal variables, like *COMPILE-FILE-PATHNAME-UNMERGED*
495 ;;; to hold the right (useful) thing, when the *COMPILE-FILE-PATHNAME*
496 ;;; holds the un-useful thing.
497 ;;; Anyway, long story short, merging is all the more wrong when we went
498 ;;; to heroic efforts to reverse-engineer the original pathname by
499 ;;; scanning for "src/" as used to be done in LPNIFY-NAMESTRING.
500 ;;; So name-contexts, at least in self-build, should use untruenameized
501 ;;; un-merged pathnames. I'm not daring enough to change it for everyone.
502 ;;; It defaults to what it should, and is changed before saving the image.
504 (declaim (type (member pathname truename) *name-context-file-path-selector*))
505 (defglobal *name-context-file-path-selector* 'pathname)
507 (defun name-context ()
508 ;; Name of the outermost non-NIL BLOCK, or the source namestring
509 ;; of the source file.
510 (let ((context
511 (or (car (find-if (lambda (b)
512 (let ((name (pop b)))
513 (and name
514 ;; KLUDGE: High debug adds this block on
515 ;; some platforms.
516 #!-unwind-to-frame-and-call-vop
517 (neq 'return-value-tag name)
518 ;; KLUDGE: CATCH produces blocks whose
519 ;; cleanup is :CATCH.
520 (neq :catch (cleanup-kind (entry-cleanup (pop b)))))))
521 (lexenv-blocks *lexenv*) :from-end t))
522 *source-namestring*
523 (awhen (case *name-context-file-path-selector*
524 (pathname (or sb!xc:*compile-file-pathname* *load-pathname*))
525 (truename (or sb!xc:*compile-file-truename* *load-truename*)))
526 (namestring it)))))
527 (when context
528 (list :in context))))
530 ;;;; FUNCTION and NAMED-LAMBDA
531 (defun name-lambdalike (thing)
532 (case (car thing)
533 ((named-lambda)
534 (or (second thing)
535 `(lambda ,(strip-lambda-list (third thing) :name) ,(name-context))))
536 ((lambda)
537 `(lambda ,(strip-lambda-list (second thing) :name) ,@(name-context)))
538 ((lambda-with-lexenv)
539 ;; FIXME: Get the original DEFUN name here.
540 `(lambda ,(fifth thing)))
541 (otherwise
542 (compiler-error "Not a valid lambda expression:~% ~S"
543 thing))))
545 (defun fun-name-leaf (thing)
546 (cond
547 ((typep thing
548 '(cons (member lambda named-lambda lambda-with-lexenv)))
549 (values (ir1-convert-lambdalike
550 thing :debug-name (name-lambdalike thing))
552 ((legal-fun-name-p thing)
553 (values (find-lexically-apparent-fun
554 thing "as the argument to FUNCTION")
555 nil))
557 (compiler-error "~S is not a legal function name." thing))))
559 (def-ir1-translator %%allocate-closures ((&rest leaves) start next result)
560 (aver (eq result 'nil))
561 (let ((lambdas leaves))
562 (ir1-convert start next result `(%allocate-closures ',lambdas))
563 (let ((allocator (node-dest (ctran-next start))))
564 (dolist (lambda lambdas)
565 (setf (functional-allocator lambda) allocator)))))
567 (defmacro with-fun-name-leaf ((leaf thing start &key global-function) &body body)
568 `(multiple-value-bind (,leaf allocate-p)
569 (if ,global-function
570 (find-global-fun ,thing t)
571 (fun-name-leaf ,thing))
572 (if allocate-p
573 (let ((.new-start. (make-ctran)))
574 (ir1-convert ,start .new-start. nil `(%%allocate-closures ,leaf))
575 (let ((,start .new-start.))
576 ,@body))
577 (locally
578 ,@body))))
580 (def-ir1-translator function ((thing) start next result)
581 "FUNCTION name
583 Return the lexically apparent definition of the function NAME. NAME may also
584 be a lambda expression."
585 (with-fun-name-leaf (leaf thing start)
586 (reference-leaf start next result leaf)))
588 ;;; Like FUNCTION, but ignores local definitions and inline
589 ;;; expansions, and doesn't nag about undefined functions.
590 ;;; Used for optimizing things like (FUNCALL 'FOO).
591 (def-ir1-translator global-function ((thing) start next result)
592 (with-fun-name-leaf (leaf thing start :global-function t)
593 (reference-leaf start next result leaf)))
595 (defun constant-global-fun-name (thing)
596 (let ((constantp (sb!xc:constantp thing)))
597 (when constantp
598 (let ((name (constant-form-value thing)))
599 (when (legal-fun-name-p name)
600 name)))))
602 (defun lvar-constant-global-fun-name (lvar)
603 (when (constant-lvar-p lvar)
604 (let ((name (lvar-value lvar)))
605 (when (legal-fun-name-p name)
606 name))))
608 (defun ensure-source-fun-form (source &optional give-up)
609 (let ((op (when (consp source) (car source))))
610 (cond ((eq op '%coerce-callable-to-fun)
611 (ensure-source-fun-form (second source)))
612 ((member op '(function global-function lambda named-lambda))
613 (values source nil))
615 (let ((cname (constant-global-fun-name source)))
616 (if cname
617 (values `(global-function ,cname) nil)
618 (values `(%coerce-callable-to-fun ,source) give-up)))))))
620 (defun source-variable-or-else (lvar fallback)
621 (let ((uses (principal-lvar-use lvar)) leaf name)
622 (or (and (ref-p uses)
623 (leaf-has-source-name-p (setf leaf (ref-leaf uses)))
624 (symbolp (setf name (leaf-source-name leaf)))
625 ;; assume users don't hand-write gensyms
626 (symbol-package name)
627 name)
628 fallback)))
630 (def-ir1-translator global-function-preserve-cast
631 ((name original-lvar) start next result)
632 (let* ((cast (lvar-use original-lvar))
633 (value-ctran (make-ctran))
634 (value-lvar (make-lvar))
635 (new-cast (etypecase cast
636 (function-designator-cast
637 (make-function-designator-cast
638 :%type-check (cast-%type-check cast)
639 :asserted-type (cast-asserted-type cast)
640 :type-to-check (cast-type-to-check cast)
641 :value value-lvar
642 :derived-type (values-specifier-type '(values function &optional))
643 :deps (function-designator-cast-deps cast)
644 :arg-specs (function-designator-cast-arg-specs cast)
645 :result-specs (function-designator-cast-result-specs cast)
646 :caller (function-designator-cast-caller cast)
647 :source-path (cast-source-path cast)))
648 (cast
649 (%make-cast
650 :%type-check (cast-%type-check cast)
651 :asserted-type (cast-asserted-type cast)
652 :type-to-check (cast-type-to-check cast)
653 :value value-lvar
654 :derived-type (values-specifier-type '(values function &optional))
655 :source-path (cast-source-path cast))))))
656 (with-fun-name-leaf (leaf name start :global-function t)
657 (reference-leaf start value-ctran value-lvar leaf))
658 (link-node-to-previous-ctran new-cast value-ctran)
659 (setf (lvar-dest value-lvar) new-cast)
660 (use-continuation new-cast next result)))
662 (defun ensure-lvar-fun-form (lvar lvar-name &optional give-up)
663 (aver (and lvar-name (symbolp lvar-name)))
664 (if (csubtypep (lvar-type lvar) (specifier-type 'function))
665 lvar-name
666 (let ((cname (lvar-constant-global-fun-name lvar)))
667 (cond (cname
668 ;; Don't lose type restrictions
669 (if (and (cast-p (lvar-uses lvar))
670 (fun-type-p (cast-asserted-type (lvar-use lvar))))
671 `(global-function-preserve-cast ,cname ,lvar)
672 `(global-function ,cname)))
673 (give-up
674 (give-up-ir1-transform
675 ;; No ~S here because if fallback is shown, it wants no quotes.
676 "~A is not known to be a function"
677 ;; LVAR-NAME is not what to show - if only it were that easy.
678 (source-variable-or-else lvar "callable expression")))
680 `(%coerce-callable-to-fun ,lvar-name))))))
682 ;;;; FUNCALL
683 (def-ir1-translator %funcall ((function &rest args) start next result)
684 ;; MACROEXPAND so that (LAMBDA ...) forms arriving here don't get an
685 ;; extra cast inserted for them.
686 (let ((function (%macroexpand function *lexenv*)))
687 (if (typep function '(cons (member function global-function) (cons t null)))
688 (with-fun-name-leaf (leaf (cadr function) start
689 :global-function (eq (car function)
690 'global-function))
691 (ir1-convert start next result `(,leaf ,@args)))
692 (let ((ctran (make-ctran))
693 (fun-lvar (make-lvar)))
694 (ir1-convert start ctran fun-lvar `(the function ,function))
695 (ir1-convert-combination-args fun-lvar ctran next result args)))))
697 (def-ir1-translator %funcall-lvar ((function &rest args) start next result)
698 (ir1-convert-combination-args function start next result args))
700 ;;; This source transform exists to reduce the amount of work for the
701 ;;; compiler. If the called function is a FUNCTION form, then convert
702 ;;; directly to %FUNCALL, instead of waiting around for type
703 ;;; inference.
704 (define-source-transform funcall (function &rest args)
705 `(%funcall ,(ensure-source-fun-form function) ,@args))
707 (deftransform %coerce-callable-to-fun ((thing) * * :node node)
708 "optimize away possible call to FDEFINITION at runtime"
709 (ensure-lvar-fun-form thing 'thing t))
711 (define-source-transform %coerce-callable-to-fun (thing)
712 (ensure-source-fun-form thing t))
714 ;;;; LET and LET*
715 ;;;;
716 ;;;; (LET and LET* can't be implemented as macros due to the fact that
717 ;;;; any pervasive declarations also affect the evaluation of the
718 ;;;; arguments.)
720 ;;; Given a list of binding specifiers in the style of LET, return:
721 ;;; 1. The list of var structures for the variables bound.
722 ;;; 2. The initial value form for each variable.
724 ;;; The variable names are checked for legality and globally special
725 ;;; variables are marked as such. Context is the name of the form, for
726 ;;; error reporting purposes.
727 (declaim (ftype (function (list symbol) (values list list))
728 extract-let-vars))
729 (defun extract-let-vars (bindings context)
730 (collect ((vars)
731 (vals))
732 (let ((names (make-repeated-name-check :context context)))
733 (dolist (spec bindings)
734 (multiple-value-bind (name value)
735 (cond ((atom spec)
736 (values spec nil))
738 (unless (proper-list-of-length-p spec 1 2)
739 (compiler-error "The ~S binding spec ~S is malformed."
740 context spec))
741 (values (first spec) (second spec))))
742 (check-variable-name-for-binding
743 name :context context :allow-symbol-macro nil)
744 (unless (eq context 'let*)
745 (funcall names name))
746 (vars (varify-lambda-arg name))
747 (vals value))))
748 (values (vars) (vals))))
750 (def-ir1-translator let ((bindings &body body) start next result)
751 "LET ({(var [value]) | var}*) declaration* form*
753 During evaluation of the FORMS, bind the VARS to the result of evaluating the
754 VALUE forms. The variables are bound in parallel after all of the VALUES forms
755 have been evaluated."
756 (cond ((null bindings)
757 (ir1-translate-locally body start next result))
758 ;; This is just to avoid leaking non-standard special forms
759 ;; into macroexpanded code
760 #!-c-stack-is-control-stack
761 ((and (equal bindings '((*alien-stack-pointer* *alien-stack-pointer*))))
762 (ir1-convert start next result
763 (let ((nsp (gensym "NSP")))
764 `(let ((,nsp (%primitive current-nsp)))
765 (restoring-nsp ,nsp ,@body)))))
766 ((listp bindings)
767 (multiple-value-bind (forms decls) (parse-body body nil)
768 (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
769 (binding* ((ctran (make-ctran))
770 (fun-lvar (make-lvar))
771 ((next result)
772 (processing-decls (decls vars nil next result
773 post-binding-lexenv)
774 (let ((fun (ir1-convert-lambda-body
775 forms
776 vars
777 :post-binding-lexenv post-binding-lexenv
778 :debug-name (debug-name 'let bindings))))
779 (reference-leaf start ctran fun-lvar fun))
780 (values next result))))
781 (ir1-convert-combination-args fun-lvar ctran next result values)))))
783 (compiler-error "Malformed LET bindings: ~S." bindings))))
785 (def-ir1-translator let* ((bindings &body body)
786 start next result)
787 "LET* ({(var [value]) | var}*) declaration* form*
789 Similar to LET, but the variables are bound sequentially, allowing each VALUE
790 form to reference any of the previous VARS."
791 (if (listp bindings)
792 (multiple-value-bind (forms decls) (parse-body body nil)
793 (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
794 (processing-decls (decls vars nil next result post-binding-lexenv)
795 (ir1-convert-aux-bindings start
796 next
797 result
798 forms
799 vars
800 values
801 post-binding-lexenv))))
802 (compiler-error "Malformed LET* bindings: ~S." bindings)))
804 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
805 ;;; and SYMBOL-MACROLET
807 ;;; Note that all these things need to preserve toplevel-formness,
808 ;;; but we don't need to worry about that within an IR1 translator,
809 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
810 ;;; forms before we hit the IR1 transform level.
811 (defun ir1-translate-locally (body start next result &key vars funs)
812 (declare (type ctran start next) (type (or lvar null) result)
813 (type list body))
814 (multiple-value-bind (forms decls) (parse-body body nil)
815 (processing-decls (decls vars funs next result)
816 (ir1-convert-progn-body start next result forms))))
818 (def-ir1-translator locally ((&body body) start next result)
819 "LOCALLY declaration* form*
821 Sequentially evaluate the FORMS in a lexical environment where the
822 DECLARATIONS have effect. If LOCALLY is a top level form, then the FORMS are
823 also processed as top level forms."
824 (ir1-translate-locally body start next result))
826 ;;;; FLET and LABELS
828 ;;; Given a list of local function specifications in the style of
829 ;;; FLET, return lists of the function names and of the lambdas which
830 ;;; are their definitions.
832 ;;; The function names are checked for legality. CONTEXT is the name
833 ;;; of the form, for error reporting.
834 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
835 (defun extract-flet-vars (definitions context)
836 (collect ((names)
837 (defs))
838 (dolist (def definitions)
839 (when (or (atom def) (< (length def) 2))
840 (compiler-error "The ~S definition spec ~S is malformed." context def))
842 (let ((name (first def)))
843 (check-fun-name name)
844 (when (fboundp name)
845 (program-assert-symbol-home-package-unlocked
846 :compile name "binding ~A as a local function"))
847 (names name)
848 (multiple-value-bind (forms decls doc) (parse-body (cddr def) t)
849 (defs `(lambda ,(second def)
850 ,@(when doc (list doc))
851 ,@decls
852 (block ,(fun-name-block-name name)
853 . ,forms))))))
854 (values (names) (defs))))
856 (defun ir1-convert-fbindings (start next result funs body)
857 (let ((ctran (make-ctran))
858 (dx-p (find-if #'leaf-dynamic-extent funs)))
859 (when dx-p
860 (ctran-starts-block ctran)
861 (ctran-starts-block next))
862 (ir1-convert start ctran nil `(%%allocate-closures ,@funs))
863 (cond (dx-p
864 (let* ((dummy (make-ctran))
865 (entry (make-entry))
866 (cleanup (make-cleanup :kind :dynamic-extent
867 :mess-up entry
868 :info (list (node-dest
869 (ctran-next start))))))
870 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
871 (setf (entry-cleanup entry) cleanup)
872 (link-node-to-previous-ctran entry ctran)
873 (use-ctran entry dummy)
875 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
876 (ir1-convert-progn-body dummy next result body))))
877 (t (ir1-convert-progn-body ctran next result body)))))
879 (def-ir1-translator flet ((definitions &body body)
880 start next result)
881 "FLET ({(name lambda-list declaration* form*)}*) declaration* body-form*
883 Evaluate the BODY-FORMS with local function definitions. The bindings do
884 not enclose the definitions; any use of NAME in the FORMS will refer to the
885 lexically apparent function definition in the enclosing environment."
886 (multiple-value-bind (forms decls) (parse-body body nil)
887 (unless (listp definitions)
888 (compiler-error "Malformed FLET definitions: ~s" definitions))
889 (multiple-value-bind (names defs)
890 (extract-flet-vars definitions 'flet)
891 (let ((fvars (mapcar (lambda (n d)
892 (ir1-convert-lambda
893 d :source-name n
894 :maybe-add-debug-catch t
895 :debug-name
896 (let ((n (if (and (symbolp n) (not (symbol-package n)))
897 (string n)
898 n)))
899 (debug-name 'flet n t))))
900 names defs)))
901 (processing-decls (decls nil fvars next result)
902 (let ((*lexenv* (make-lexenv :funs (pairlis names fvars))))
903 (ir1-convert-fbindings start next result fvars forms)))))))
905 (def-ir1-translator labels ((definitions &body body) start next result)
906 "LABELS ({(name lambda-list declaration* form*)}*) declaration* body-form*
908 Evaluate the BODY-FORMS with local function definitions. The bindings enclose
909 the new definitions, so the defined functions can call themselves or each
910 other."
911 (multiple-value-bind (forms decls) (parse-body body nil)
912 (unless (listp definitions)
913 (compiler-error "Malformed LABELS definitions: ~s" definitions))
914 (multiple-value-bind (names defs)
915 (extract-flet-vars definitions 'labels)
916 (let* (;; dummy LABELS functions, to be used as placeholders
917 ;; during construction of real LABELS functions
918 (placeholder-funs (mapcar (lambda (name)
919 (make-functional
920 :%source-name name
921 :%debug-name (debug-name
922 'labels-placeholder
923 name)))
924 names))
925 ;; (like PAIRLIS but guaranteed to preserve ordering:)
926 (placeholder-fenv (mapcar #'cons names placeholder-funs))
927 ;; the real LABELS functions, compiled in a LEXENV which
928 ;; includes the dummy LABELS functions
929 (real-funs
930 (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
931 (mapcar (lambda (name def)
932 (ir1-convert-lambda def
933 :source-name name
934 :maybe-add-debug-catch t
935 :debug-name (debug-name 'labels name t)))
936 names defs))))
938 ;; Modify all the references to the dummy function leaves so
939 ;; that they point to the real function leaves.
940 (loop for real-fun in real-funs and
941 placeholder-cons in placeholder-fenv do
942 (substitute-leaf real-fun (cdr placeholder-cons))
943 (setf (cdr placeholder-cons) real-fun))
945 ;; Voila.
946 (processing-decls (decls nil real-funs next result)
947 (let ((*lexenv* (make-lexenv
948 ;; Use a proper FENV here (not the
949 ;; placeholder used earlier) so that if the
950 ;; lexical environment is used for inline
951 ;; expansion we'll get the right functions.
952 :funs (pairlis names real-funs))))
953 (ir1-convert-fbindings start next result real-funs forms)))))))
956 ;;;; the THE special operator, and friends
958 ;;; A logic shared among THE and TRULY-THE.
959 (defun the-in-policy (type value policy start next result)
960 (let ((type (cond ((ctype-p type)
961 type)
962 ((compiler-values-specifier-type type))
964 (ir1-convert start next result
965 `(error "Bad type specifier: ~a"
966 ,type))
967 (return-from the-in-policy)))))
968 (cond ((or (eq type *wild-type*)
969 (eq type *universal-type*)
970 (and (leaf-p value)
971 (not (fun-designator-type-p type))
972 (values-subtypep (make-single-value-type (leaf-type value))
973 type))
974 (and (not (fun-designator-type-p type))
975 (sb!xc:constantp value)
976 (or (not (values-type-p type))
977 (values-type-may-be-single-value-p type))
978 (ctypep (constant-form-value value)
979 (single-value-type type))))
980 (ir1-convert start next result value)
981 nil) ;; NIL is important, older SBCLs miscompiled (values &optional x) casts
983 (let* ((value-ctran (make-ctran))
984 (value-lvar (make-lvar))
985 (cast (make-cast value-lvar type policy)))
986 (ir1-convert start value-ctran value-lvar value)
987 (link-node-to-previous-ctran cast value-ctran)
988 (setf (lvar-dest value-lvar) cast)
989 (use-continuation cast next result)
990 (when (eq type *empty-type*)
991 (maybe-terminate-block cast t))
992 cast)))))
994 ;;; Assert that FORM evaluates to the specified type (which may be a
995 ;;; VALUES type). TYPE may be a type specifier or (as a hack) a CTYPE.
996 (def-ir1-translator the ((value-type form) start next result)
997 "Specifies that the values returned by FORM conform to the VALUE-TYPE.
999 CLHS specifies that the consequences are undefined if any result is
1000 not of the declared type, but SBCL treats declarations as assertions
1001 as long as SAFETY is at least 2, in which case incorrect type
1002 information will result in a runtime type-error instead of leading to
1003 eg. heap corruption. This is however expressly non-portable: use
1004 CHECK-TYPE instead of THE to catch type-errors at runtime. THE is best
1005 considered an optimization tool to inform the compiler about types it
1006 is unable to derive from other declared types."
1007 (the-in-policy value-type form (lexenv-policy *lexenv*) start next result))
1009 ;;; This is like the THE special form, except that it believes
1010 ;;; whatever you tell it. It will never generate a type check, but
1011 ;;; will cause a warning if the compiler can prove the assertion is
1012 ;;; wrong.
1014 ;;; For the benefit of code-walkers we also add a macro-expansion. (Using INFO
1015 ;;; directly to get around safeguards for adding a macro-expansion for special
1016 ;;; operator.) Because :FUNCTION :KIND remains :SPECIAL-FORM, the compiler
1017 ;;; never uses the macro -- but manually calling its MACRO-FUNCTION or
1018 ;;; MACROEXPANDing TRULY-THE forms does.
1019 (def-ir1-translator truly-the ((value-type form) start next result)
1020 "Specifies that the values returned by FORM conform to the
1021 VALUE-TYPE, and causes the compiler to trust this information
1022 unconditionally.
1024 Consequences are undefined if any result is not of the declared type
1025 -- typical symptoms including memory corruptions. Use with great
1026 care."
1027 (the-in-policy value-type form **zero-typecheck-policy** start next result))
1029 ;;; THE with some options for the CAST
1030 (def-ir1-translator the* (((value-type &key context silent-conflict
1031 truly
1032 modifying) form)
1033 start next result)
1034 (let* ((policy (lexenv-policy *lexenv*))
1035 (value-type (values-specifier-type value-type))
1036 (cast (if modifying
1037 (let* ((value-ctran (make-ctran))
1038 (value-lvar (make-lvar))
1039 (cast (make-modifying-cast
1040 :asserted-type value-type
1041 :type-to-check (maybe-weaken-check value-type
1042 (if truly
1043 **zero-typecheck-policy**
1044 policy))
1045 :value value-lvar
1046 :derived-type (coerce-to-values value-type)
1047 :caller modifying)))
1048 (ir1-convert start value-ctran value-lvar form)
1049 (link-node-to-previous-ctran cast value-ctran)
1050 (setf (lvar-dest value-lvar) cast)
1051 (use-continuation cast next result)
1052 cast)
1053 (the-in-policy value-type form policy start next result))))
1054 (when cast
1056 (setf (cast-context cast) context)
1057 (setf (cast-silent-conflict cast) silent-conflict))))
1059 (def-ir1-translator bound-cast ((array bound index) start next result)
1060 (let ((check-bound-tran (make-ctran))
1061 (index-ctran (make-ctran))
1062 (index-lvar (make-lvar)))
1063 ;; CHECK-BOUND transform ensures that INDEX won't be evaluated twice
1064 (ir1-convert start check-bound-tran nil `(%check-bound ,array ,bound ,index))
1065 (ir1-convert check-bound-tran index-ctran index-lvar index)
1066 (let* ((check-bound-combination (ctran-use check-bound-tran))
1067 (array (first (combination-args check-bound-combination)))
1068 (bound (second (combination-args check-bound-combination)))
1069 (derived (constant-lvar-p bound))
1070 (type (specifier-type (if derived
1071 `(integer 0 (,(lvar-value bound)))
1072 '(and unsigned-byte fixnum))))
1073 (cast (make-bound-cast :value index-lvar
1074 :asserted-type type
1075 :type-to-check type
1076 :derived-type (coerce-to-values type)
1077 :check check-bound-combination
1078 :derived derived
1079 :array array
1080 :bound bound)))
1081 (link-node-to-previous-ctran cast index-ctran)
1082 (setf (lvar-dest index-lvar) cast)
1083 (use-continuation cast next result))))
1085 #-sb-xc-host
1086 (setf (info :function :macro-function 'truly-the)
1087 (lambda (whole env)
1088 (declare (ignore env))
1089 `(the ,@(cdr whole)))
1090 (info :function :macro-function 'the*)
1091 (lambda (whole env)
1092 (declare (ignore env))
1093 `(the ,(caadr whole) ,@(cddr whole))))
1095 ;;;; SETQ
1097 (defun explode-setq (form err-fun)
1098 (collect ((sets))
1099 (do ((op (car form))
1100 (thing (cdr form) (cddr thing)))
1101 ((endp thing) (sets))
1102 (if (endp (cdr thing))
1103 (funcall err-fun "odd number of args to ~A: ~S" op form)
1104 (sets `(,op ,(first thing) ,(second thing)))))))
1106 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
1107 ;;; look at the global information. If the name is for a constant,
1108 ;;; then error out.
1109 (def-ir1-translator setq ((&whole source &rest things) start next result)
1110 (if (proper-list-of-length-p things 2)
1111 (let* ((name (first things))
1112 (value-form (second things))
1113 (leaf (or (lexenv-find name vars) (find-free-var name))))
1114 (etypecase leaf
1115 (leaf
1116 (when (constant-p leaf)
1117 (compiler-error "~S is a constant and thus can't be set." name))
1118 (when (lambda-var-p leaf)
1119 (let ((home-lambda (ctran-home-lambda-or-null start)))
1120 (when home-lambda
1121 (sset-adjoin leaf (lambda-calls-or-closes home-lambda))))
1122 (when (lambda-var-ignorep leaf)
1123 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
1124 ;; requires that this be a STYLE-WARNING, not a full warning.
1125 (compiler-style-warn
1126 "~S is being set even though it was declared to be ignored."
1127 name)))
1128 (if (and (global-var-p leaf) (eq :unknown (global-var-kind leaf)))
1129 ;; For undefined variables go through SET, so that we can catch
1130 ;; constant modifications.
1131 (ir1-convert start next result `(set ',name ,value-form))
1132 (setq-var start next result leaf value-form)))
1133 (cons
1134 (aver (eq (car leaf) 'macro))
1135 ;; Allow *MACROEXPAND-HOOK* to see NAME get expanded,
1136 ;; not just see a use of SETF on the new place.
1137 (ir1-convert start next result `(setf ,name ,(second things))))
1138 (heap-alien-info
1139 (ir1-convert start next result
1140 `(%set-heap-alien ',leaf ,(second things))))))
1141 (ir1-convert-progn-body start next result
1142 (explode-setq source 'compiler-error))))
1144 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
1145 ;;; This should only need to be called in SETQ.
1146 (defun setq-var (start next result var value)
1147 (declare (type ctran start next) (type (or lvar null) result)
1148 (type basic-var var))
1149 (let ((dest-ctran (make-ctran))
1150 (dest-lvar (make-lvar))
1151 (type (or (lexenv-find var type-restrictions)
1152 (leaf-type var))))
1153 (ir1-convert start dest-ctran dest-lvar `(the ,(type-specifier type)
1154 ,value))
1155 (let ((res (make-set :var var :value dest-lvar)))
1156 (setf (lvar-dest dest-lvar) res)
1157 (setf (leaf-ever-used var) t)
1158 (push res (basic-var-sets var))
1159 (link-node-to-previous-ctran res dest-ctran)
1160 (use-continuation res next result))))
1162 ;;;; CATCH, THROW and UNWIND-PROTECT
1164 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
1165 ;;; since as far as IR1 is concerned, it has no interesting
1166 ;;; properties other than receiving multiple-values.
1167 (def-ir1-translator throw ((tag result) start next result-lvar)
1168 "THROW tag form
1170 Do a non-local exit, return the values of FORM from the CATCH whose tag is EQ
1171 to TAG."
1172 (ir1-convert start next result-lvar
1173 `(multiple-value-call #'%throw ,tag ,result)))
1175 ;;; This is a special special form used to instantiate a cleanup as
1176 ;;; the current cleanup within the body. KIND is the kind of cleanup
1177 ;;; to make, and MESS-UP is a form that does the mess-up action. We
1178 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
1179 ;;; and introduce the cleanup into the lexical environment. We
1180 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
1181 ;;; cleanup, since this inner cleanup is the interesting one.
1182 (def-ir1-translator %within-cleanup
1183 ((kind mess-up &body body) start next result)
1184 (let ((dummy (make-ctran))
1185 (dummy2 (make-ctran)))
1186 (ir1-convert start dummy nil mess-up)
1187 (let* ((mess-node (ctran-use dummy))
1188 (cleanup (make-cleanup :kind kind
1189 :mess-up mess-node))
1190 (old-cup (lexenv-cleanup *lexenv*))
1191 (*lexenv* (make-lexenv :cleanup cleanup)))
1192 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
1193 (ir1-convert dummy dummy2 nil '(%cleanup-point))
1194 (ir1-convert-progn-body dummy2 next result body))))
1196 ;;; This is a special special form that makes an "escape function"
1197 ;;; which returns unknown values from named block. We convert the
1198 ;;; function, set its kind to :ESCAPE, and then reference it. The
1199 ;;; :ESCAPE kind indicates that this function's purpose is to
1200 ;;; represent a non-local control transfer, and that it might not
1201 ;;; actually have to be compiled.
1203 ;;; Note that environment analysis replaces references to escape
1204 ;;; functions with references to the corresponding NLX-INFO structure.
1205 (def-ir1-translator %escape-fun ((tag) start next result)
1206 (let ((fun (let ((*allow-instrumenting* nil))
1207 (ir1-convert-lambda
1208 `(lambda ()
1209 (return-from ,tag (%unknown-values)))
1210 :debug-name (debug-name 'escape-fun tag))))
1211 (ctran (make-ctran)))
1212 (setf (functional-kind fun) :escape)
1213 (ir1-convert start ctran nil `(%%allocate-closures ,fun))
1214 (reference-leaf ctran next result fun)))
1216 ;;; Yet another special special form. This one looks up a local
1217 ;;; function and smashes it to a :CLEANUP function, as well as
1218 ;;; referencing it.
1219 (def-ir1-translator %cleanup-fun ((name) start next result)
1220 ;; FIXME: Should this not be :TEST #'EQUAL? What happens to
1221 ;; (SETF FOO) here?
1222 (let ((fun (lexenv-find name funs)))
1223 (aver (lambda-p fun))
1224 (setf (functional-kind fun) :cleanup)
1225 (reference-leaf start next result fun)))
1227 (def-ir1-translator catch ((tag &body body) start next result)
1228 "CATCH tag form*
1230 Evaluate TAG and instantiate it as a catcher while the body forms are
1231 evaluated in an implicit PROGN. If a THROW is done to TAG within the dynamic
1232 scope of the body, then control will be transferred to the end of the body and
1233 the thrown values will be returned."
1234 ;; We represent the possibility of the control transfer by making an
1235 ;; "escape function" that does a lexical exit, and instantiate the
1236 ;; cleanup using %WITHIN-CLEANUP.
1237 (let* ((tag-ctran (make-ctran))
1238 (tag-lvar (make-lvar)))
1239 (ir1-convert start tag-ctran tag-lvar tag)
1240 (ir1-convert
1241 tag-ctran next result
1242 (with-unique-names (exit-block)
1243 `(block ,exit-block
1244 (%within-cleanup
1245 :catch (%catch (%escape-fun ,exit-block) ,tag-lvar)
1246 ,@body))))))
1248 ;;; Since NSP is restored on unwind we only need to protect against
1249 ;;; local transfers of control, basically the same as special
1250 ;;; bindings.
1251 ;;; Needs to be wrapped in a LET (let ((nsp (current-nsp))) (restoring-nsp nsp body))
1252 ;;; The LET is needed because the cleanup can be emitted multiple
1253 ;;; times, but there's no reference to NSP before EMIT-CLEANUPS.
1254 ;;; Passing NSP to the dummy %CLEANUP-FUN keeps it alive.
1255 #!-c-stack-is-control-stack
1256 (def-ir1-translator restoring-nsp
1257 ((nsp &body body) start next result)
1258 (let ((cleanup (make-cleanup :kind :restore-nsp))
1259 (nsp-ctran (make-ctran))
1260 (cleanup-ctran (make-ctran)))
1261 (ir1-convert start nsp-ctran nil nsp)
1262 (setf (cleanup-mess-up cleanup) (ctran-use nsp-ctran))
1263 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
1264 ;; KLUDGE: reference NSP twice so that the LET doesn't get
1265 ;; deleted before EMIT-CLEANUPS
1266 (ir1-convert nsp-ctran cleanup-ctran nil `(%cleanup-point ,nsp ,nsp))
1267 (ir1-convert-progn-body cleanup-ctran next result
1268 body))))
1270 (def-ir1-translator unwind-protect
1271 ((protected &body cleanup) start next result)
1272 "UNWIND-PROTECT protected cleanup*
1274 Evaluate the form PROTECTED, returning its values. The CLEANUP forms are
1275 evaluated whenever the dynamic scope of the PROTECTED form is exited (either
1276 due to normal completion or a non-local exit such as THROW)."
1277 ;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
1278 ;; cleanup forms into a local function so that they can be referenced
1279 ;; both in the case where we are unwound and in any local exits. We
1280 ;; use %CLEANUP-FUN on this to indicate that reference by
1281 ;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
1282 ;; an XEP.
1283 (ir1-convert
1284 start next result
1285 (with-unique-names (cleanup-fun drop-thru-tag exit-tag next start count)
1286 `(flet ((,cleanup-fun ()
1287 ,@cleanup
1288 nil))
1289 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
1290 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
1291 ;; and something can be done to make %ESCAPE-FUN have
1292 ;; dynamic extent too.
1293 (declare (dynamic-extent #',cleanup-fun))
1294 (block ,drop-thru-tag
1295 (multiple-value-bind (,next ,start ,count)
1296 (block ,exit-tag
1297 (%within-cleanup
1298 :unwind-protect
1299 (%unwind-protect (%escape-fun ,exit-tag)
1300 (%cleanup-fun ,cleanup-fun))
1301 (return-from ,drop-thru-tag ,protected)))
1302 (declare (optimize (insert-debug-catch 0)))
1303 (,cleanup-fun)
1304 (%continue-unwind ,next ,start ,count)))))))
1306 ;;;; multiple-value stuff
1308 (def-ir1-translator multiple-value-call ((fun &rest args) start next result)
1309 "MULTIPLE-VALUE-CALL function values-form*
1311 Call FUNCTION, passing all the values of each VALUES-FORM as arguments,
1312 values from the first VALUES-FORM making up the first argument, etc."
1313 (let* ((ctran (make-ctran))
1314 (fun-lvar (make-lvar))
1315 (node (if args
1316 ;; If there are arguments, MULTIPLE-VALUE-CALL
1317 ;; turns into an MV-COMBINATION.
1318 (make-mv-combination fun-lvar)
1319 ;; If there are no arguments, then we convert to a
1320 ;; normal combination, ensuring that a MV-COMBINATION
1321 ;; always has at least one argument. This can be
1322 ;; regarded as an optimization, but it is more
1323 ;; important for simplifying compilation of
1324 ;; MV-COMBINATIONS.
1325 (make-combination fun-lvar))))
1326 (ir1-convert start ctran fun-lvar (ensure-source-fun-form fun))
1327 (setf (lvar-dest fun-lvar) node)
1328 (collect ((arg-lvars))
1329 (let ((this-start ctran))
1330 (dolist (arg args)
1331 (let ((this-ctran (make-ctran))
1332 (this-lvar (make-lvar node)))
1333 (ir1-convert this-start this-ctran this-lvar arg)
1334 (setq this-start this-ctran)
1335 (arg-lvars this-lvar)))
1336 (link-node-to-previous-ctran node this-start)
1337 (use-continuation node next result)
1338 (setf (basic-combination-args node) (arg-lvars))))))
1340 (def-ir1-translator multiple-value-prog1
1341 ((values-form &rest forms) start next result)
1342 "MULTIPLE-VALUE-PROG1 values-form form*
1344 Evaluate VALUES-FORM and then the FORMS, but return all the values of
1345 VALUES-FORM."
1346 (let* ((value-ctran (make-ctran))
1347 (forms-ctran (make-ctran))
1348 (value-lvar (make-lvar))
1349 ;; This is to avoid writing in the RESULT LVAR before the
1350 ;; body is executed, because the body may overwrite it.
1351 ;; See MAY-DELETE-VESTIGIAL-EXIT.
1352 (cast (make-vestigial-exit-cast
1353 :value value-lvar)))
1354 (ctran-starts-block value-ctran)
1355 (ir1-convert start value-ctran value-lvar values-form)
1356 (ir1-convert-progn-body value-ctran forms-ctran nil forms)
1357 (link-node-to-previous-ctran cast forms-ctran)
1358 (setf (lvar-dest value-lvar) cast)
1359 (use-continuation cast next result)))
1362 ;;;; interface to defining macros
1364 ;;; Old CMUCL comment:
1366 ;;; Return a new source path with any stuff intervening between the
1367 ;;; current path and the first form beginning with NAME stripped
1368 ;;; off. This is used to hide the guts of DEFmumble macros to
1369 ;;; prevent annoying error messages.
1371 ;;; Now that we have implementations of DEFmumble macros in terms of
1372 ;;; EVAL-WHEN, this function is no longer used. However, it might be
1373 ;;; worth figuring out why it was used, and maybe doing analogous
1374 ;;; munging to the functions created in the expanders for the macros.
1375 (defun revert-source-path (name)
1376 (do ((path *current-path* (cdr path)))
1377 ((null path) *current-path*)
1378 (let ((first (first path)))
1379 (when (or (eq first name)
1380 (eq first 'original-source-start))
1381 (return path)))))