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
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.
15 ;;;; special forms for control
17 (def-ir1-translator progn ((&rest forms) start next result)
20 Evaluates each Form in order, returning the values of the last form. With no
22 (ir1-convert-progn-body start next result forms))
24 (def-ir1-translator if ((test then &optional else) start next result)
26 "If Predicate Then [Else]
27 If Predicate evaluates to non-null, evaluate Then and returns 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 (node (make-if :test pred-lvar
36 :consequent then-block
37 :alternative else-block)))
38 ;; IR1-CONVERT-MAYBE-PREDICATE requires DEST to be CIF, so the
39 ;; order of the following two forms is important
40 (setf (lvar-dest pred-lvar) node)
41 (ir1-convert start pred-ctran pred-lvar test)
42 (link-node-to-previous-ctran node pred-ctran)
44 (let ((start-block (ctran-block pred-ctran)))
45 (setf (block-last start-block) node)
46 (ctran-starts-block next)
48 (link-blocks start-block then-block)
49 (link-blocks start-block else-block))
51 (ir1-convert then-ctran next result then)
52 (ir1-convert else-ctran next result else)))
54 ;;;; BLOCK and TAGBODY
56 ;;;; We make an ENTRY node to mark the start and a :ENTRY cleanup to
57 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an EXIT
60 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
61 ;;; body in the modified environment. We make NEXT start a block now,
62 ;;; since if it was done later, the block would be in the wrong
64 (def-ir1-translator block ((name &rest forms) start next result)
67 Evaluate the Forms as a PROGN. Within the lexical scope of the body,
68 (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
69 result of Value-Form."
70 (unless (symbolp name)
71 (compiler-error "The block name ~S is not a symbol." name))
73 (ctran-starts-block next)
74 (let* ((dummy (make-ctran))
76 (cleanup (make-cleanup :kind :block
78 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
79 (setf (entry-cleanup entry) cleanup)
80 (link-node-to-previous-ctran entry start)
81 (use-ctran entry dummy)
83 (let* ((env-entry (list entry next result))
84 (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
86 (ir1-convert-progn-body dummy next result forms))))
88 (def-ir1-translator return-from ((name &optional value) start next result)
90 "Return-From Block-Name Value-Form
91 Evaluate the Value-Form, returning its values from the lexically enclosing
92 BLOCK Block-Name. This is constrained to be used only within the dynamic
95 ;; We make NEXT start a block just so that it will have a block
96 ;; assigned. People assume that when they pass a ctran into
97 ;; IR1-CONVERT as NEXT, it will have a block when it is done.
98 ;; KLUDGE: Note that this block is basically fictitious. In the code
99 ;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
100 ;; it's the block which answers the question "which block is
101 ;; the (SETQ X 3) in?" when the right answer is that (SETQ X 3) is
102 ;; dead code and so doesn't really have a block at all. The existence
103 ;; of this block, and that way that it doesn't explicitly say
104 ;; "I'm actually nowhere at all" makes some logic (e.g.
105 ;; BLOCK-HOME-LAMBDA-OR-NULL) more obscure, and it might be better
106 ;; to get rid of it, perhaps using a special placeholder value
107 ;; to indicate the orphanedness of the code.
108 (declare (ignore result))
109 (ctran-starts-block next)
110 (let* ((found (or (lexenv-find name blocks)
111 (compiler-error "return for unknown block: ~S" name)))
112 (value-ctran (make-ctran))
113 (value-lvar (make-lvar))
114 (entry (first found))
115 (exit (make-exit :entry entry
117 (push exit (entry-exits entry))
118 (setf (lvar-dest value-lvar) exit)
119 (ir1-convert start value-ctran value-lvar value)
120 (link-node-to-previous-ctran exit value-ctran)
121 (let ((home-lambda (ctran-home-lambda-or-null start)))
123 (push entry (lambda-calls-or-closes home-lambda))))
124 (use-continuation exit (second found) (third found))))
126 ;;; Return a list of the segments of a TAGBODY. Each segment looks
127 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
128 ;;; tagbody into segments of non-tag statements, and explicitly
129 ;;; represent the drop-through with a GO. The first segment has a
130 ;;; dummy NIL tag, since it represents code before the first tag. The
131 ;;; last segment (which may also be the first segment) ends in NIL
132 ;;; rather than a GO.
133 (defun parse-tagbody (body)
134 (declare (list body))
135 (collect ((segments))
136 (let ((current (cons nil body)))
138 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
140 (segments `(,@current nil))
142 (let ((tag (elt current tag-pos)))
143 (when (assoc tag (segments))
145 "The tag ~S appears more than once in the tagbody."
147 (unless (or (symbolp tag) (integerp tag))
148 (compiler-error "~S is not a legal tagbody statement." tag))
149 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
150 (setq current (nthcdr tag-pos current)))))
153 ;;; Set up the cleanup, emitting the entry node. Then make a block for
154 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
155 ;;; Finally, convert each segment with the precomputed Start and Cont
157 (def-ir1-translator tagbody ((&rest statements) start next result)
159 "Tagbody {Tag | Statement}*
160 Define tags for used with GO. The Statements are evaluated in order
161 (skipping Tags) and NIL is returned. If a statement contains a GO to a
162 defined Tag within the lexical scope of the form, then control is transferred
163 to the next statement following that tag. A Tag must an integer or a
164 symbol. A statement must be a list. Other objects are illegal within the
167 (ctran-starts-block next)
168 (let* ((dummy (make-ctran))
170 (segments (parse-tagbody statements))
171 (cleanup (make-cleanup :kind :tagbody
173 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
174 (setf (entry-cleanup entry) cleanup)
175 (link-node-to-previous-ctran entry start)
176 (use-ctran entry dummy)
182 (dolist (segment (rest segments))
183 (let* ((tag-ctran (make-ctran))
184 (tag (list (car segment) entry tag-ctran)))
187 (ctran-starts-block tag-ctran)
191 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
192 (mapc (lambda (segment start end)
193 (ir1-convert-progn-body start end
194 (when (eq end next) result)
196 segments (starts) (ctrans))))))
198 ;;; Emit an EXIT node without any value.
199 (def-ir1-translator go ((tag) start next result)
202 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
203 is constrained to be used only within the dynamic extent of the TAGBODY."
204 (ctran-starts-block next)
205 (let* ((found (or (lexenv-find tag tags :test #'eql)
206 (compiler-error "attempt to GO to nonexistent tag: ~S"
208 (entry (first found))
209 (exit (make-exit :entry entry)))
210 (push exit (entry-exits entry))
211 (link-node-to-previous-ctran exit start)
212 (let ((home-lambda (ctran-home-lambda-or-null start)))
214 (push entry (lambda-calls-or-closes home-lambda))))
215 (use-ctran exit (second found))))
217 ;;;; translators for compiler-magic special forms
219 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
220 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
221 ;;; so that they're never seen at this level.)
223 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
224 ;;; of non-top-level EVAL-WHENs is very simple:
225 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
226 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
227 ;;; are ignored for non-top-level forms. For non-top-level forms, an
228 ;;; eval-when specifying the :EXECUTE situation is treated as an
229 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
230 ;;; form; otherwise, the forms in the body are ignored.
231 (def-ir1-translator eval-when ((situations &rest forms) start next result)
233 "EVAL-WHEN (Situation*) Form*
234 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
235 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
236 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
237 (declare (ignore ct lt))
238 (ir1-convert-progn-body start next result (and e forms)))
241 ;;; common logic for MACROLET and SYMBOL-MACROLET
243 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
244 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
245 ;;; call FUN (with no arguments).
246 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
247 definitionize-keyword
250 (declare (type function definitionize-fun fun))
251 (declare (type (member :vars :funs) definitionize-keyword))
252 (declare (type list definitions))
253 (unless (= (length definitions)
254 (length (remove-duplicates definitions :key #'first)))
255 (compiler-style-warn "duplicate definitions in ~S" definitions))
256 (let* ((processed-definitions (mapcar definitionize-fun definitions))
257 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
258 ;; I wonder how much of an compiler performance penalty this
259 ;; non-constant keyword is.
260 (funcall fun definitionize-keyword processed-definitions)))
262 ;;; Tweak LEXENV to include the DEFINITIONS from a MACROLET, then
263 ;;; call FUN (with no arguments).
265 ;;; This is split off from the IR1 convert method so that it can be
266 ;;; shared by the special-case top level MACROLET processing code, and
267 ;;; further split so that the special-case MACROLET processing code in
268 ;;; EVAL can likewise make use of it.
269 (defun macrolet-definitionize-fun (context lexenv)
270 (flet ((fail (control &rest args)
272 (:compile (apply #'compiler-error control args))
273 (:eval (error 'simple-program-error
274 :format-control control
275 :format-arguments args)))))
277 (unless (list-of-length-at-least-p definition 2)
278 (fail "The list ~S is too short to be a legal local macro definition."
280 (destructuring-bind (name arglist &body body) definition
281 (unless (symbolp name)
282 (fail "The local macro name ~S is not a symbol." name))
284 (compiler-assert-symbol-home-package-unlocked
285 name "binding ~A as a local macro"))
286 (unless (listp arglist)
287 (fail "The local macro argument list ~S is not a list."
289 (with-unique-names (whole environment)
290 (multiple-value-bind (body local-decls)
291 (parse-defmacro arglist whole body name 'macrolet
292 :environment environment)
296 `(lambda (,whole ,environment)
301 (defun funcall-in-macrolet-lexenv (definitions fun context)
302 (%funcall-in-foomacrolet-lexenv
303 (macrolet-definitionize-fun context (make-restricted-lexenv *lexenv*))
308 (def-ir1-translator macrolet ((definitions &rest body) start next result)
310 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
311 Evaluate the Body-Forms in an environment with the specified local macros
312 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
313 destructuring lambda list, and the Forms evaluate to the expansion.."
314 (funcall-in-macrolet-lexenv
317 (declare (ignore funs))
318 (ir1-translate-locally body start next result))
321 (defun symbol-macrolet-definitionize-fun (context)
322 (flet ((fail (control &rest args)
324 (:compile (apply #'compiler-error control args))
325 (:eval (error 'simple-program-error
326 :format-control control
327 :format-arguments args)))))
329 (unless (proper-list-of-length-p definition 2)
330 (fail "malformed symbol/expansion pair: ~S" definition))
331 (destructuring-bind (name expansion) definition
332 (unless (symbolp name)
333 (fail "The local symbol macro name ~S is not a symbol." name))
334 (when (or (boundp name) (eq (info :variable :kind name) :macro))
335 (compiler-assert-symbol-home-package-unlocked
336 name "binding ~A as a local symbol-macro"))
337 (let ((kind (info :variable :kind name)))
338 (when (member kind '(:special :constant))
339 (fail "Attempt to bind a ~(~A~) variable with SYMBOL-MACROLET: ~S"
341 ;; A magical cons that MACROEXPAND-1 understands.
342 `(,name . (MACRO . ,expansion))))))
344 (defun funcall-in-symbol-macrolet-lexenv (definitions fun context)
345 (%funcall-in-foomacrolet-lexenv
346 (symbol-macrolet-definitionize-fun context)
351 (def-ir1-translator symbol-macrolet
352 ((macrobindings &body body) start next result)
354 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
355 Define the Names as symbol macros with the given Expansions. Within the
356 body, references to a Name will effectively be replaced with the Expansion."
357 (funcall-in-symbol-macrolet-lexenv
360 (ir1-translate-locally body start next result :vars vars))
365 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
366 ;;;; into a funny function.
368 ;;; Carefully evaluate a list of forms, returning a list of the results.
369 (defun eval-info-args (args)
370 (declare (list args))
371 (handler-case (mapcar #'eval args)
373 (compiler-error "Lisp error during evaluation of info args:~%~A"
376 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
377 ;;; the template, the second is a list of the results of any
378 ;;; codegen-info args, and the remaining arguments are the runtime
381 ;;; We do various error checking now so that we don't bomb out with
382 ;;; a fatal error during IR2 conversion.
384 ;;; KLUDGE: It's confusing having multiple names floating around for
385 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
386 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
387 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
388 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
389 ;;; VOP or %VOP.. -- WHN 2001-06-11
390 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
391 (def-ir1-translator %primitive ((name &rest args) start next result)
392 (declare (type symbol name))
393 (let* ((template (or (gethash name *backend-template-names*)
394 (bug "undefined primitive ~A" name)))
395 (required (length (template-arg-types template)))
396 (info (template-info-arg-count template))
397 (min (+ required info))
398 (nargs (length args)))
399 (if (template-more-args-type template)
401 (bug "Primitive ~A was called with ~R argument~:P, ~
402 but wants at least ~R."
406 (unless (= nargs min)
407 (bug "Primitive ~A was called with ~R argument~:P, ~
408 but wants exactly ~R."
413 (when (eq (template-result-types template) :conditional)
414 (bug "%PRIMITIVE was used with a conditional template."))
416 (when (template-more-results-type template)
417 (bug "%PRIMITIVE was used with an unknown values template."))
419 (ir1-convert start next result
420 `(%%primitive ',template
422 (subseq args required min))
423 ,@(subseq args 0 required)
424 ,@(subseq args min)))))
428 (def-ir1-translator quote ((thing) start next result)
431 Return Value without evaluating it."
432 (reference-constant start next result thing))
434 ;;;; FUNCTION and NAMED-LAMBDA
435 (defun fun-name-leaf (thing)
439 '(lambda named-lambda instance-lambda lambda-with-lexenv))
440 (ir1-convert-lambdalike
442 :debug-name (debug-namify "#'" thing)
443 :allow-debug-catch-tag t))
444 ((legal-fun-name-p thing)
445 (find-lexically-apparent-fun
446 thing "as the argument to FUNCTION"))
448 (compiler-error "~S is not a legal function name." thing)))
449 (find-lexically-apparent-fun
450 thing "as the argument to FUNCTION")))
452 (def-ir1-translator function ((thing) start next result)
455 Return the lexically apparent definition of the function Name. Name may also
456 be a lambda expression."
457 (reference-leaf start next result (fun-name-leaf thing)))
461 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
462 ;;; (not symbols). %FUNCALL is used directly in some places where the
463 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
464 (deftransform funcall ((function &rest args) * *)
465 (let ((arg-names (make-gensym-list (length args))))
466 `(lambda (function ,@arg-names)
467 (%funcall ,(if (csubtypep (lvar-type function)
468 (specifier-type 'function))
470 '(%coerce-callable-to-fun function))
473 (def-ir1-translator %funcall ((function &rest args) start next result)
474 (if (and (consp function) (eq (car function) 'function))
475 (ir1-convert start next result
476 `(,(fun-name-leaf (second function)) ,@args))
477 (let ((ctran (make-ctran))
478 (fun-lvar (make-lvar)))
479 (ir1-convert start ctran fun-lvar `(the function ,function))
480 (ir1-convert-combination-args fun-lvar ctran next result args))))
482 ;;; This source transform exists to reduce the amount of work for the
483 ;;; compiler. If the called function is a FUNCTION form, then convert
484 ;;; directly to %FUNCALL, instead of waiting around for type
486 (define-source-transform funcall (function &rest args)
487 (if (and (consp function) (eq (car function) 'function))
488 `(%funcall ,function ,@args)
491 (deftransform %coerce-callable-to-fun ((thing) (function) *)
492 "optimize away possible call to FDEFINITION at runtime"
497 ;;;; (LET and LET* can't be implemented as macros due to the fact that
498 ;;;; any pervasive declarations also affect the evaluation of the
501 ;;; Given a list of binding specifiers in the style of LET, return:
502 ;;; 1. The list of var structures for the variables bound.
503 ;;; 2. The initial value form for each variable.
505 ;;; The variable names are checked for legality and globally special
506 ;;; variables are marked as such. Context is the name of the form, for
507 ;;; error reporting purposes.
508 (declaim (ftype (function (list symbol) (values list list))
510 (defun extract-let-vars (bindings context)
514 (flet ((get-var (name)
515 (varify-lambda-arg name
516 (if (eq context 'let*)
519 (dolist (spec bindings)
521 (let ((var (get-var spec)))
526 (unless (proper-list-of-length-p spec 1 2)
527 (compiler-error "The ~S binding spec ~S is malformed."
530 (let* ((name (first spec))
531 (var (get-var name)))
534 (vals (second spec)))))))
535 (dolist (name (names))
536 (when (eq (info :variable :kind name) :macro)
537 (compiler-assert-symbol-home-package-unlocked
538 name "lexically binding symbol-macro ~A")))
539 (values (vars) (vals))))
541 (def-ir1-translator let ((bindings &body body) start next result)
543 "LET ({(Var [Value]) | Var}*) Declaration* Form*
544 During evaluation of the Forms, bind the Vars to the result of evaluating the
545 Value forms. The variables are bound in parallel after all of the Values are
548 (ir1-translate-locally body start next result)
549 (multiple-value-bind (forms decls)
550 (parse-body body :doc-string-allowed nil)
551 (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
552 (binding* ((ctran (make-ctran))
553 (fun-lvar (make-lvar))
555 (processing-decls (decls vars nil next result)
556 (let ((fun (ir1-convert-lambda-body
559 :debug-name (debug-namify "LET S"
561 (reference-leaf start ctran fun-lvar fun))
562 (values next result))))
563 (ir1-convert-combination-args fun-lvar ctran next result values))))))
565 (def-ir1-translator let* ((bindings &body body)
568 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
569 Similar to LET, but the variables are bound sequentially, allowing each Value
570 form to reference any of the previous Vars."
571 (multiple-value-bind (forms decls)
572 (parse-body body :doc-string-allowed nil)
573 (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
574 (processing-decls (decls vars nil start next)
575 (ir1-convert-aux-bindings start
582 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
583 ;;; and SYMBOL-MACROLET
585 ;;; Note that all these things need to preserve toplevel-formness,
586 ;;; but we don't need to worry about that within an IR1 translator,
587 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
588 ;;; forms before we hit the IR1 transform level.
589 (defun ir1-translate-locally (body start next result &key vars funs)
590 (declare (type ctran start next) (type (or lvar null) result)
592 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
593 (processing-decls (decls vars funs next result)
594 (ir1-convert-progn-body start next result forms))))
596 (def-ir1-translator locally ((&body body) start next result)
598 "LOCALLY Declaration* Form*
599 Sequentially evaluate the Forms in a lexical environment where the
600 the Declarations have effect. If LOCALLY is a top level form, then
601 the Forms are also processed as top level forms."
602 (ir1-translate-locally body start next result))
606 ;;; Given a list of local function specifications in the style of
607 ;;; FLET, return lists of the function names and of the lambdas which
608 ;;; are their definitions.
610 ;;; The function names are checked for legality. CONTEXT is the name
611 ;;; of the form, for error reporting.
612 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
613 (defun extract-flet-vars (definitions context)
616 (dolist (def definitions)
617 (when (or (atom def) (< (length def) 2))
618 (compiler-error "The ~S definition spec ~S is malformed." context def))
620 (let ((name (first def)))
621 (check-fun-name name)
623 (compiler-assert-symbol-home-package-unlocked
624 name "binding ~A as a local function"))
626 (multiple-value-bind (forms decls) (parse-body (cddr def))
627 (defs `(lambda ,(second def)
629 (block ,(fun-name-block-name name)
631 (values (names) (defs))))
633 (def-ir1-translator flet ((definitions &body body)
636 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
637 Evaluate the Body-Forms with some local function definitions. The bindings
638 do not enclose the definitions; any use of Name in the Forms will refer to
639 the lexically apparent function definition in the enclosing environment."
640 (multiple-value-bind (forms decls)
641 (parse-body body :doc-string-allowed nil)
642 (multiple-value-bind (names defs)
643 (extract-flet-vars definitions 'flet)
644 (let ((fvars (mapcar (lambda (n d)
645 (ir1-convert-lambda d
647 :debug-name (debug-namify
649 :allow-debug-catch-tag t))
651 (processing-decls (decls nil fvars next result)
652 (let ((*lexenv* (make-lexenv :funs (pairlis names fvars))))
653 (ir1-convert-progn-body start
658 (def-ir1-translator labels ((definitions &body body) start next result)
660 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
661 Evaluate the Body-Forms with some local function definitions. The bindings
662 enclose the new definitions, so the defined functions can call themselves or
664 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
665 (multiple-value-bind (names defs)
666 (extract-flet-vars definitions 'labels)
667 (let* (;; dummy LABELS functions, to be used as placeholders
668 ;; during construction of real LABELS functions
669 (placeholder-funs (mapcar (lambda (name)
672 :%debug-name (debug-namify
673 "LABELS placeholder "
676 ;; (like PAIRLIS but guaranteed to preserve ordering:)
677 (placeholder-fenv (mapcar #'cons names placeholder-funs))
678 ;; the real LABELS functions, compiled in a LEXENV which
679 ;; includes the dummy LABELS functions
681 (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
682 (mapcar (lambda (name def)
683 (ir1-convert-lambda def
685 :debug-name (debug-namify
687 :allow-debug-catch-tag t))
690 ;; Modify all the references to the dummy function leaves so
691 ;; that they point to the real function leaves.
692 (loop for real-fun in real-funs and
693 placeholder-cons in placeholder-fenv do
694 (substitute-leaf real-fun (cdr placeholder-cons))
695 (setf (cdr placeholder-cons) real-fun))
698 (processing-decls (decls nil real-funs next result)
699 (let ((*lexenv* (make-lexenv
700 ;; Use a proper FENV here (not the
701 ;; placeholder used earlier) so that if the
702 ;; lexical environment is used for inline
703 ;; expansion we'll get the right functions.
704 :funs (pairlis names real-funs))))
705 (ir1-convert-progn-body start
711 ;;;; the THE special operator, and friends
713 ;;; A logic shared among THE and TRULY-THE.
714 (defun the-in-policy (type value policy start next result)
715 (let ((type (if (ctype-p type) type
716 (compiler-values-specifier-type type))))
717 (cond ((or (eq type *wild-type*)
718 (eq type *universal-type*)
720 (values-subtypep (make-single-value-type (leaf-type value))
722 (and (sb!xc:constantp value)
723 (ctypep (constant-form-value value)
724 (single-value-type type))))
725 (ir1-convert start next result value))
726 (t (let ((value-ctran (make-ctran))
727 (value-lvar (make-lvar)))
728 (ir1-convert start value-ctran value-lvar value)
729 (let ((cast (make-cast value-lvar type policy)))
730 (link-node-to-previous-ctran cast value-ctran)
731 (setf (lvar-dest value-lvar) cast)
732 (use-continuation cast next result)))))))
734 ;;; Assert that FORM evaluates to the specified type (which may be a
735 ;;; VALUES type). TYPE may be a type specifier or (as a hack) a CTYPE.
736 (def-ir1-translator the ((type value) start next result)
737 (the-in-policy type value (lexenv-policy *lexenv*) start next result))
739 ;;; This is like the THE special form, except that it believes
740 ;;; whatever you tell it. It will never generate a type check, but
741 ;;; will cause a warning if the compiler can prove the assertion is
743 (def-ir1-translator truly-the ((type value) start next result)
747 (let ((type (coerce-to-values (compiler-values-specifier-type type)))
748 (old (when result (find-uses result))))
749 (ir1-convert start next result value)
751 (do-uses (use result)
752 (unless (memq use old)
753 (derive-node-type use type)))))
755 (the-in-policy type value '((type-check . 0)) start cont))
759 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
760 ;;; look at the global information. If the name is for a constant,
762 (def-ir1-translator setq ((&whole source &rest things) start next result)
763 (let ((len (length things)))
765 (compiler-error "odd number of args to SETQ: ~S" source))
767 (let* ((name (first things))
768 (leaf (or (lexenv-find name vars)
769 (find-free-var name))))
772 (when (constant-p leaf)
773 (compiler-error "~S is a constant and thus can't be set." name))
774 (when (lambda-var-p leaf)
775 (let ((home-lambda (ctran-home-lambda-or-null start)))
777 (pushnew leaf (lambda-calls-or-closes home-lambda))))
778 (when (lambda-var-ignorep leaf)
779 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
780 ;; requires that this be a STYLE-WARNING, not a full warning.
782 "~S is being set even though it was declared to be ignored."
784 (setq-var start next result leaf (second things)))
786 (aver (eq (car leaf) 'MACRO))
787 ;; FIXME: [Free] type declaration. -- APD, 2002-01-26
788 (ir1-convert start next result
789 `(setf ,(cdr leaf) ,(second things))))
791 (ir1-convert start next result
792 `(%set-heap-alien ',leaf ,(second things))))))
794 (do ((thing things (cddr thing)))
796 (ir1-convert-progn-body start next result (sets)))
797 (sets `(setq ,(first thing) ,(second thing))))))))
799 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
800 ;;; This should only need to be called in SETQ.
801 (defun setq-var (start next result var value)
802 (declare (type ctran start next) (type (or lvar null) result)
803 (type basic-var var))
804 (let ((dest-ctran (make-ctran))
805 (dest-lvar (make-lvar))
806 (type (or (lexenv-find var type-restrictions)
808 (ir1-convert start dest-ctran dest-lvar `(the ,type ,value))
809 (let ((res (make-set :var var :value dest-lvar)))
810 (setf (lvar-dest dest-lvar) res)
811 (setf (leaf-ever-used var) t)
812 (push res (basic-var-sets var))
813 (link-node-to-previous-ctran res dest-ctran)
814 (use-continuation res next result))))
816 ;;;; CATCH, THROW and UNWIND-PROTECT
818 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
819 ;;; since as as far as IR1 is concerned, it has no interesting
820 ;;; properties other than receiving multiple-values.
821 (def-ir1-translator throw ((tag result) start next result-lvar)
824 Do a non-local exit, return the values of Form from the CATCH whose tag
825 evaluates to the same thing as Tag."
826 (ir1-convert start next result-lvar
827 `(multiple-value-call #'%throw ,tag ,result)))
829 ;;; This is a special special form used to instantiate a cleanup as
830 ;;; the current cleanup within the body. KIND is the kind of cleanup
831 ;;; to make, and MESS-UP is a form that does the mess-up action. We
832 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
833 ;;; and introduce the cleanup into the lexical environment. We
834 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
835 ;;; cleanup, since this inner cleanup is the interesting one.
836 (def-ir1-translator %within-cleanup
837 ((kind mess-up &body body) start next result)
838 (let ((dummy (make-ctran))
839 (dummy2 (make-ctran)))
840 (ir1-convert start dummy nil mess-up)
841 (let* ((mess-node (ctran-use dummy))
842 (cleanup (make-cleanup :kind kind
844 (old-cup (lexenv-cleanup *lexenv*))
845 (*lexenv* (make-lexenv :cleanup cleanup)))
846 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
847 (ir1-convert dummy dummy2 nil '(%cleanup-point))
848 (ir1-convert-progn-body dummy2 next result body))))
850 ;;; This is a special special form that makes an "escape function"
851 ;;; which returns unknown values from named block. We convert the
852 ;;; function, set its kind to :ESCAPE, and then reference it. The
853 ;;; :ESCAPE kind indicates that this function's purpose is to
854 ;;; represent a non-local control transfer, and that it might not
855 ;;; actually have to be compiled.
857 ;;; Note that environment analysis replaces references to escape
858 ;;; functions with references to the corresponding NLX-INFO structure.
859 (def-ir1-translator %escape-fun ((tag) start next result)
860 (let ((fun (ir1-convert-lambda
862 (return-from ,tag (%unknown-values)))
863 :debug-name (debug-namify "escape function for " tag))))
864 (setf (functional-kind fun) :escape)
865 (reference-leaf start next result fun)))
867 ;;; Yet another special special form. This one looks up a local
868 ;;; function and smashes it to a :CLEANUP function, as well as
870 (def-ir1-translator %cleanup-fun ((name) start next result)
871 (let ((fun (lexenv-find name funs)))
872 (aver (lambda-p fun))
873 (setf (functional-kind fun) :cleanup)
874 (reference-leaf start next result fun)))
876 (def-ir1-translator catch ((tag &body body) start next result)
879 Evaluate TAG and instantiate it as a catcher while the body forms are
880 evaluated in an implicit PROGN. If a THROW is done to TAG within the dynamic
881 scope of the body, then control will be transferred to the end of the body
882 and the thrown values will be returned."
883 ;; We represent the possibility of the control transfer by making an
884 ;; "escape function" that does a lexical exit, and instantiate the
885 ;; cleanup using %WITHIN-CLEANUP.
888 (with-unique-names (exit-block)
891 :catch (%catch (%escape-fun ,exit-block) ,tag)
894 (def-ir1-translator unwind-protect
895 ((protected &body cleanup) start next result)
897 "Unwind-Protect Protected Cleanup*
898 Evaluate the form PROTECTED, returning its values. The CLEANUP forms are
899 evaluated whenever the dynamic scope of the PROTECTED form is exited (either
900 due to normal completion or a non-local exit such as THROW)."
901 ;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
902 ;; cleanup forms into a local function so that they can be referenced
903 ;; both in the case where we are unwound and in any local exits. We
904 ;; use %CLEANUP-FUN on this to indicate that reference by
905 ;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
909 (with-unique-names (cleanup-fun drop-thru-tag exit-tag next start count)
910 `(flet ((,cleanup-fun () ,@cleanup nil))
911 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
912 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
913 ;; and something can be done to make %ESCAPE-FUN have
914 ;; dynamic extent too.
915 (block ,drop-thru-tag
916 (multiple-value-bind (,next ,start ,count)
920 (%unwind-protect (%escape-fun ,exit-tag)
921 (%cleanup-fun ,cleanup-fun))
922 (return-from ,drop-thru-tag ,protected)))
924 (%continue-unwind ,next ,start ,count)))))))
926 ;;;; multiple-value stuff
928 (def-ir1-translator multiple-value-call ((fun &rest args) start next result)
930 "MULTIPLE-VALUE-CALL Function Values-Form*
931 Call FUNCTION, passing all the values of each VALUES-FORM as arguments,
932 values from the first VALUES-FORM making up the first argument, etc."
933 (let* ((ctran (make-ctran))
934 (fun-lvar (make-lvar))
936 ;; If there are arguments, MULTIPLE-VALUE-CALL
937 ;; turns into an MV-COMBINATION.
938 (make-mv-combination fun-lvar)
939 ;; If there are no arguments, then we convert to a
940 ;; normal combination, ensuring that a MV-COMBINATION
941 ;; always has at least one argument. This can be
942 ;; regarded as an optimization, but it is more
943 ;; important for simplifying compilation of
945 (make-combination fun-lvar))))
946 (ir1-convert start ctran fun-lvar
947 (if (and (consp fun) (eq (car fun) 'function))
949 `(%coerce-callable-to-fun ,fun)))
950 (setf (lvar-dest fun-lvar) node)
951 (collect ((arg-lvars))
952 (let ((this-start ctran))
954 (let ((this-ctran (make-ctran))
955 (this-lvar (make-lvar node)))
956 (ir1-convert this-start this-ctran this-lvar arg)
957 (setq this-start this-ctran)
958 (arg-lvars this-lvar)))
959 (link-node-to-previous-ctran node this-start)
960 (use-continuation node next result)
961 (setf (basic-combination-args node) (arg-lvars))))))
963 (def-ir1-translator multiple-value-prog1
964 ((values-form &rest forms) start next result)
966 "MULTIPLE-VALUE-PROG1 Values-Form Form*
967 Evaluate Values-Form and then the Forms, but return all the values of
969 (let ((dummy (make-ctran)))
970 (ctran-starts-block dummy)
971 (ir1-convert start dummy result values-form)
972 (ir1-convert-progn-body dummy next nil forms)))
974 ;;;; interface to defining macros
976 ;;; Old CMUCL comment:
978 ;;; Return a new source path with any stuff intervening between the
979 ;;; current path and the first form beginning with NAME stripped
980 ;;; off. This is used to hide the guts of DEFmumble macros to
981 ;;; prevent annoying error messages.
983 ;;; Now that we have implementations of DEFmumble macros in terms of
984 ;;; EVAL-WHEN, this function is no longer used. However, it might be
985 ;;; worth figuring out why it was used, and maybe doing analogous
986 ;;; munging to the functions created in the expanders for the macros.
987 (defun revert-source-path (name)
988 (do ((path *current-path* (cdr path)))
989 ((null path) *current-path*)
990 (let ((first (first path)))
991 (when (or (eq first name)
992 (eq first 'original-source-start))