1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
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 (declaim (special *compiler-error-bailout*))
17 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
18 ;;; taken through the source to reach the form. This provides a way to
19 ;;; keep track of the location of original source forms, even when
20 ;;; macroexpansions and other arbitary permutations of the code
21 ;;; happen. This table is initialized by calling Find-Source-Paths on
22 ;;; the original source.
23 (declaim (hash-table *source-paths*))
24 (defvar *source-paths*)
26 ;;; *CURRENT-COMPONENT* is the Component structure which we link
27 ;;; blocks into as we generate them. This just serves to glue the
28 ;;; emitted blocks together until local call analysis and flow graph
29 ;;; canonicalization figure out what is really going on. We need to
30 ;;; keep track of all the blocks generated so that we can delete them
31 ;;; if they turn out to be unreachable.
33 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
34 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
35 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
36 ;;; which also confusing.)
37 (declaim (type (or component null) *current-component*))
38 (defvar *current-component*)
40 ;;; *CURRENT-PATH* is the source path of the form we are currently
41 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
42 (declaim (list *current-path*))
43 (defvar *current-path* nil)
45 ;;; *CONVERTING-FOR-INTERPRETER* is true when we are creating IR1 to
46 ;;; be interpreted rather than compiled. This inhibits source
47 ;;; tranformations and stuff.
48 (defvar *converting-for-interpreter* nil)
49 ;;; FIXME: Rename to *IR1-FOR-INTERPRETER-NOT-COMPILER-P*.
51 (defvar *derive-function-types* nil
52 "Should the compiler assume that function types will never change,
53 so that it can use type information inferred from current definitions
54 to optimize code which uses those definitions? Setting this true
55 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
56 the efficiency of stable code.")
58 ;;;; namespace management utilities
60 ;;; Return a GLOBAL-VAR structure usable for referencing the global
62 (defun find-free-really-function (name)
63 (unless (info :function :kind name)
64 (setf (info :function :kind name) :function)
65 (setf (info :function :where-from name) :assumed))
67 (let ((where (info :function :where-from name)))
68 (when (and (eq where :assumed)
69 ;; In the ordinary target Lisp, it's silly to report
70 ;; undefinedness when the function is defined in the
71 ;; running Lisp. But at cross-compile time, the current
72 ;; definedness of a function is irrelevant to the
73 ;; definedness at runtime, which is what matters.
74 #-sb-xc-host (not (fboundp name)))
75 (note-undefined-reference name :function))
76 (make-global-var :kind :global-function
78 :type (if (or *derive-function-types*
80 (info :function :type name)
81 (specifier-type 'function))
84 ;;; Return a SLOT-ACCESSOR structure usable for referencing the slot
85 ;;; accessor NAME. CLASS is the structure class.
86 (defun find-structure-slot-accessor (class name)
87 (declare (type sb!xc:class class))
88 (let* ((info (layout-info
89 (or (info :type :compiler-layout (sb!xc:class-name class))
90 (class-layout class))))
91 (accessor (if (listp name) (cadr name) name))
92 (slot (find accessor (dd-slots info) :key #'sb!kernel:dsd-accessor))
94 (slot-type (dsd-type slot)))
96 (error "can't find slot ~S" type))
101 `(function (,slot-type ,type) ,slot-type)
102 `(function (,type) ,slot-type)))
106 ;;; If NAME is already entered in *FREE-FUNCTIONS*, then return the
107 ;;; value. Otherwise, make a new GLOBAL-VAR using information from the
108 ;;; global environment and enter it in *FREE-FUNCTIONS*. If NAME names
109 ;;; a macro or special form, then we error out using the supplied
110 ;;; context which indicates what we were trying to do that demanded a
112 (defun find-free-function (name context)
113 (declare (string context))
114 (declare (values global-var))
115 (or (gethash name *free-functions*)
116 (ecase (info :function :kind name)
117 ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
119 (compiler-error "The macro name ~S was found ~A." name context))
121 (compiler-error "The special form name ~S was found ~A."
125 (check-function-name name)
126 (note-if-setf-function-and-macro name)
127 (let ((expansion (info :function :inline-expansion name))
128 (inlinep (info :function :inlinep name)))
129 (setf (gethash name *free-functions*)
130 (if (or expansion inlinep)
131 (make-defined-function
133 :inline-expansion expansion
135 :where-from (info :function :where-from name)
136 :type (info :function :type name))
137 (let ((info (info :function :accessor-for name)))
140 (find-free-really-function name))
141 (sb!xc:structure-class
142 (find-structure-slot-accessor info name))
144 (if (typep (layout-info (info :type :compiler-layout
147 'defstruct-description)
148 (find-structure-slot-accessor info name)
149 (find-free-really-function name))))))))))))
151 ;;; Return the LEAF structure for the lexically apparent function
152 ;;; definition of NAME.
153 (declaim (ftype (function (t string) leaf) find-lexically-apparent-function))
154 (defun find-lexically-apparent-function (name context)
155 (let ((var (lexenv-find name functions :test #'equal)))
158 (aver (and (consp var) (eq (car var) 'macro)))
159 (compiler-error "found macro name ~S ~A" name context))
162 (find-free-function name context)))))
164 ;;; Return the LEAF node for a global variable reference to NAME. If
165 ;;; NAME is already entered in *FREE-VARIABLES*, then we just return
166 ;;; the corresponding value. Otherwise, we make a new leaf using
167 ;;; information from the global environment and enter it in
168 ;;; *FREE-VARIABLES*. If the variable is unknown, then we emit a
170 (defun find-free-variable (name)
171 (declare (values (or leaf heap-alien-info)))
172 (unless (symbolp name)
173 (compiler-error "Variable name is not a symbol: ~S." name))
174 (or (gethash name *free-variables*)
175 (let ((kind (info :variable :kind name))
176 (type (info :variable :type name))
177 (where-from (info :variable :where-from name)))
178 (when (and (eq where-from :assumed) (eq kind :global))
179 (note-undefined-reference name :variable))
181 (setf (gethash name *free-variables*)
183 (info :variable :alien-info name)
184 (multiple-value-bind (val valp)
185 (info :variable :constant-value name)
186 (if (and (eq kind :constant) valp)
187 (make-constant :value val
190 :where-from where-from)
191 (make-global-var :kind kind
194 :where-from where-from))))))))
196 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
197 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
198 ;;; CONSTANT might be circular. We also check that the constant (and
199 ;;; any subparts) are dumpable at all.
200 (eval-when (:compile-toplevel :load-toplevel :execute)
201 ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
202 ;; below. -- AL 20010227
203 (defconstant list-to-hash-table-threshold 32))
204 (defun maybe-emit-make-load-forms (constant)
205 (let ((things-processed nil)
207 ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
208 (declare (type (or list hash-table) things-processed)
209 (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
211 (labels ((grovel (value)
212 ;; Unless VALUE is an object which which obviously
213 ;; can't contain other objects
215 '(or #-sb-xc-host unboxed-array
220 (etypecase things-processed
222 (when (member value things-processed :test #'eq)
223 (return-from grovel nil))
224 (push value things-processed)
226 (when (> count list-to-hash-table-threshold)
227 (let ((things things-processed))
228 (setf things-processed
229 (make-hash-table :test 'eq))
230 (dolist (thing things)
231 (setf (gethash thing things-processed) t)))))
233 (when (gethash value things-processed)
234 (return-from grovel nil))
235 (setf (gethash value things-processed) t)))
239 (grovel (cdr value)))
241 (dotimes (i (length value))
242 (grovel (svref value i))))
244 (dotimes (i (length value))
245 (grovel (aref value i))))
247 ;; Even though the (ARRAY T) branch does the exact
248 ;; same thing as this branch we do this separately
249 ;; so that the compiler can use faster versions of
250 ;; array-total-size and row-major-aref.
251 (dotimes (i (array-total-size value))
252 (grovel (row-major-aref value i))))
254 (dotimes (i (array-total-size value))
255 (grovel (row-major-aref value i))))
256 (;; In the target SBCL, we can dump any instance,
257 ;; but in the cross-compilation host,
258 ;; %INSTANCE-FOO functions don't work on general
259 ;; instances, only on STRUCTURE!OBJECTs.
260 #+sb-xc-host structure!object
261 #-sb-xc-host instance
262 (when (emit-make-load-form value)
263 (dotimes (i (%instance-length value))
264 (grovel (%instance-ref value i)))))
267 "Objects of type ~S can't be dumped into fasl files."
268 (type-of value)))))))
272 ;;;; some flow-graph hacking utilities
274 ;;; This function sets up the back link between the node and the
275 ;;; continuation which continues at it.
276 #!-sb-fluid (declaim (inline prev-link))
277 (defun prev-link (node cont)
278 (declare (type node node) (type continuation cont))
279 (aver (not (continuation-next cont)))
280 (setf (continuation-next cont) node)
281 (setf (node-prev node) cont))
283 ;;; This function is used to set the continuation for a node, and thus
284 ;;; determine what receives the value and what is evaluated next. If
285 ;;; the continuation has no block, then we make it be in the block
286 ;;; that the node is in. If the continuation heads its block, we end
287 ;;; our block and link it to that block. If the continuation is not
288 ;;; currently used, then we set the derived-type for the continuation
289 ;;; to that of the node, so that a little type propagation gets done.
291 ;;; We also deal with a bit of THE's semantics here: we weaken the
292 ;;; assertion on CONT to be no stronger than the assertion on CONT in
293 ;;; our scope. See the IR1-CONVERT method for THE.
294 #!-sb-fluid (declaim (inline use-continuation))
295 (defun use-continuation (node cont)
296 (declare (type node node) (type continuation cont))
297 (let ((node-block (continuation-block (node-prev node))))
298 (case (continuation-kind cont)
300 (setf (continuation-block cont) node-block)
301 (setf (continuation-kind cont) :inside-block)
302 (setf (continuation-use cont) node)
303 (setf (node-cont node) cont))
305 (%use-continuation node cont)))))
306 (defun %use-continuation (node cont)
307 (declare (type node node) (type continuation cont) (inline member))
308 (let ((block (continuation-block cont))
309 (node-block (continuation-block (node-prev node))))
310 (aver (eq (continuation-kind cont) :block-start))
311 (when (block-last node-block)
312 (error "~S has already ended." node-block))
313 (setf (block-last node-block) node)
314 (when (block-succ node-block)
315 (error "~S already has successors." node-block))
316 (setf (block-succ node-block) (list block))
317 (when (memq node-block (block-pred block))
318 (error "~S is already a predecessor of ~S." node-block block))
319 (push node-block (block-pred block))
320 (add-continuation-use node cont)
321 (unless (eq (continuation-asserted-type cont) *wild-type*)
322 (let ((new (values-type-union (continuation-asserted-type cont)
323 (or (lexenv-find cont type-restrictions)
325 (when (type/= new (continuation-asserted-type cont))
326 (setf (continuation-asserted-type cont) new)
327 (reoptimize-continuation cont))))))
329 ;;;; exported functions
331 ;;; This function takes a form and the top-level form number for that
332 ;;; form, and returns a lambda representing the translation of that
333 ;;; form in the current global environment. The lambda is top-level
334 ;;; lambda that can be called to cause evaluation of the forms. This
335 ;;; lambda is in the initial component. If FOR-VALUE is T, then the
336 ;;; value of the form is returned from the function, otherwise NIL is
339 ;;; This function may have arbitrary effects on the global environment
340 ;;; due to processing of PROCLAIMs and EVAL-WHENs. All syntax error
341 ;;; checking is done, with erroneous forms being replaced by a proxy
342 ;;; which signals an error if it is evaluated. Warnings about possibly
343 ;;; inconsistent or illegal changes to the global environment will
346 ;;; We make the initial component and convert the form in a PROGN (and
347 ;;; an optional NIL tacked on the end.) We then return the lambda. We
348 ;;; bind all of our state variables here, rather than relying on the
349 ;;; global value (if any) so that IR1 conversion will be reentrant.
350 ;;; This is necessary for EVAL-WHEN processing, etc.
352 ;;; The hashtables used to hold global namespace info must be
353 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
354 ;;; that local macro definitions can be introduced by enclosing code.
355 (defun ir1-top-level (form path for-value)
356 (declare (list path))
357 (let* ((*current-path* path)
358 (component (make-empty-component))
359 (*current-component* component))
360 (setf (component-name component) "initial component")
361 (setf (component-kind component) :initial)
362 (let* ((forms (if for-value `(,form) `(,form nil)))
363 (res (ir1-convert-lambda-body forms ())))
364 (setf (leaf-name res) "top-level form")
365 (setf (functional-entry-function res) res)
366 (setf (functional-arg-documentation res) ())
367 (setf (functional-kind res) :top-level)
370 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
371 ;;; form number to associate with a source path. This should be bound
372 ;;; to 0 around the processing of each truly top-level form.
373 (declaim (type index *current-form-number*))
374 (defvar *current-form-number*)
376 ;;; This function is called on freshly read forms to record the
377 ;;; initial location of each form (and subform.) Form is the form to
378 ;;; find the paths in, and TLF-NUM is the top-level form number of the
379 ;;; truly top-level form.
381 ;;; This gets a bit interesting when the source code is circular. This
382 ;;; can (reasonably?) happen in the case of circular list constants.
383 (defun find-source-paths (form tlf-num)
384 (declare (type index tlf-num))
385 (let ((*current-form-number* 0))
386 (sub-find-source-paths form (list tlf-num)))
388 (defun sub-find-source-paths (form path)
389 (unless (gethash form *source-paths*)
390 (setf (gethash form *source-paths*)
391 (list* 'original-source-start *current-form-number* path))
392 (incf *current-form-number*)
396 (declare (fixnum pos))
399 (when (atom subform) (return))
400 (let ((fm (car subform)))
402 (sub-find-source-paths fm (cons pos path)))
404 (setq subform (cdr subform))
405 (when (eq subform trail) (return)))))
409 (setq trail (cdr trail)))))))
411 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
413 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
414 ;; out of the body and converts a proxy form instead.
415 (ir1-error-bailout ((start
419 (proxy ``(error "execution of a form compiled with errors:~% ~S"
422 (let ((skip (gensym "SKIP")))
424 (catch 'ir1-error-abort
425 (let ((*compiler-error-bailout*
427 (throw 'ir1-error-abort nil))))
429 (return-from ,skip nil)))
430 (ir1-convert ,start ,cont ,proxy)))))
432 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
433 ;; continuation START. CONT is the continuation which receives the
434 ;; value of the FORM to be translated. The translators call this
435 ;; function recursively to translate their subnodes.
437 ;; As a special hack to make life easier in the compiler, a LEAF
438 ;; IR1-converts into a reference to that LEAF structure. This allows
439 ;; the creation using backquote of forms that contain leaf
440 ;; references, without having to introduce dummy names into the
442 (declaim (ftype (function (continuation continuation t) (values)) ir1-convert))
443 (defun ir1-convert (start cont form)
444 (ir1-error-bailout (start cont form)
445 (let ((*current-path* (or (gethash form *source-paths*)
446 (cons form *current-path*))))
448 (cond ((and (symbolp form) (not (keywordp form)))
449 (ir1-convert-variable start cont form))
451 (reference-leaf start cont form))
453 (reference-constant start cont form)))
454 (let ((fun (car form)))
457 (let ((lexical-def (lexenv-find fun functions)))
458 (typecase lexical-def
459 (null (ir1-convert-global-functoid start cont form))
461 (ir1-convert-local-combination start
466 (ir1-convert-srctran start cont lexical-def form))
468 (aver (and (consp lexical-def)
469 (eq (car lexical-def) 'macro)))
470 (ir1-convert start cont
471 (careful-expand-macro (cdr lexical-def)
473 ((or (atom fun) (not (eq (car fun) 'lambda)))
474 (compiler-error "illegal function call"))
476 (ir1-convert-combination start
479 (ir1-convert-lambda fun))))))))
482 ;; Generate a reference to a manifest constant, creating a new leaf
483 ;; if necessary. If we are producing a fasl file, make sure that
484 ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
486 (defun reference-constant (start cont value)
487 (declare (type continuation start cont)
488 (inline find-constant))
491 '(error "attempt to reference undumpable constant"))
492 (when (producing-fasl-file)
493 (maybe-emit-make-load-forms value))
494 (let* ((leaf (find-constant value))
495 (res (make-ref (leaf-type leaf) leaf)))
496 (push res (leaf-refs leaf))
497 (prev-link res start)
498 (use-continuation res cont)))
501 ;;; Add Fun to the COMPONENT-REANALYZE-FUNCTIONS. Fun is returned.
502 (defun maybe-reanalyze-function (fun)
503 (declare (type functional fun))
504 (when (typep fun '(or optional-dispatch clambda))
505 (pushnew fun (component-reanalyze-functions *current-component*)))
508 ;;; Generate a Ref node for LEAF, frobbing the LEAF structure as
509 ;;; needed. If LEAF represents a defined function which has already
510 ;;; been converted, and is not :NOTINLINE, then reference the
511 ;;; functional instead.
512 (defun reference-leaf (start cont leaf)
513 (declare (type continuation start cont) (type leaf leaf))
514 (let* ((leaf (or (and (defined-function-p leaf)
515 (not (eq (defined-function-inlinep leaf)
517 (let ((fun (defined-function-functional leaf)))
518 (when (and fun (not (functional-kind fun)))
519 (maybe-reanalyze-function fun))))
521 (res (make-ref (or (lexenv-find leaf type-restrictions)
524 (push res (leaf-refs leaf))
525 (setf (leaf-ever-used leaf) t)
526 (prev-link res start)
527 (use-continuation res cont)))
529 ;;; Convert a reference to a symbolic constant or variable. If the
530 ;;; symbol is entered in the LEXENV-VARIABLES we use that definition,
531 ;;; otherwise we find the current global definition. This is also
532 ;;; where we pick off symbol macro and Alien variable references.
533 (defun ir1-convert-variable (start cont name)
534 (declare (type continuation start cont) (symbol name))
535 (let ((var (or (lexenv-find name variables) (find-free-variable name))))
538 (when (and (lambda-var-p var) (lambda-var-ignorep var))
539 ;; (ANSI's specification for the IGNORE declaration requires
540 ;; that this be a STYLE-WARNING, not a full WARNING.)
541 (compiler-style-warning "reading an ignored variable: ~S" name))
542 (reference-leaf start cont var))
544 (aver (eq (car var) 'MACRO))
545 (ir1-convert start cont (cdr var)))
547 (ir1-convert start cont `(%heap-alien ',var)))))
550 ;;; Convert anything that looks like a special form, global function
552 (defun ir1-convert-global-functoid (start cont form)
553 (declare (type continuation start cont) (list form))
554 (let* ((fun (first form))
555 (translator (info :function :ir1-convert fun))
556 (cmacro (info :function :compiler-macro-function fun)))
557 (cond (translator (funcall translator start cont form))
558 ((and cmacro (not *converting-for-interpreter*)
559 (not (eq (info :function :inlinep fun) :notinline)))
560 (let ((res (careful-expand-macro cmacro form)))
562 (ir1-convert-global-functoid-no-cmacro start cont form fun)
563 (ir1-convert start cont res))))
565 (ir1-convert-global-functoid-no-cmacro start cont form fun)))))
567 ;;; Handle the case of where the call was not a compiler macro, or was a
568 ;;; compiler macro and passed.
569 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
570 (declare (type continuation start cont) (list form))
571 ;; FIXME: Couldn't all the INFO calls here be converted into
572 ;; standard CL functions, like MACRO-FUNCTION or something?
573 ;; And what happens with lexically-defined (MACROLET) macros
575 (ecase (info :function :kind fun)
579 (careful-expand-macro (info :function :macro-function fun)
582 (ir1-convert-srctran start cont (find-free-function fun "Eh?") form))))
584 (defun muffle-warning-or-die ()
586 (error "internal error -- no MUFFLE-WARNING restart"))
588 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
589 ;;; errors which occur during the macroexpansion.
590 (defun careful-expand-macro (fun form)
591 (handler-bind (;; When cross-compiling, we can get style warnings
592 ;; about e.g. undefined functions. An unhandled
593 ;; CL:STYLE-WARNING (as opposed to a
594 ;; SB!C::COMPILER-NOTE) would cause FAILURE-P to be
595 ;; set on the return from #'SB!XC:COMPILE-FILE, which
596 ;; would falsely indicate an error sufficiently
597 ;; serious that we should stop the build process. To
598 ;; avoid this, we translate CL:STYLE-WARNING
599 ;; conditions from the host Common Lisp into
600 ;; cross-compiler SB!C::COMPILER-NOTE calls. (It
601 ;; might be cleaner to just make Python use
602 ;; CL:STYLE-WARNING internally, so that the
603 ;; significance of any host Common Lisp
604 ;; CL:STYLE-WARNINGs is understood automatically. But
605 ;; for now I'm not motivated to do this. -- WHN
607 (style-warning (lambda (c)
608 (compiler-note "(during macroexpansion)~%~A"
610 (muffle-warning-or-die)))
611 ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
612 ;; Debian Linux, anyway) raises a CL:WARNING
613 ;; condition (not a CL:STYLE-WARNING) for undefined
614 ;; symbols when converting interpreted functions,
615 ;; causing COMPILE-FILE to think the file has a real
616 ;; problem, causing COMPILE-FILE to return FAILURE-P
617 ;; set (not just WARNINGS-P set). Since undefined
618 ;; symbol warnings are often harmless forward
619 ;; references, and since it'd be inordinately painful
620 ;; to try to eliminate all such forward references,
621 ;; these warnings are basically unavoidable. Thus, we
622 ;; need to coerce the system to work through them,
623 ;; and this code does so, by crudely suppressing all
624 ;; warnings in cross-compilation macroexpansion. --
629 "(during macroexpansion)~%~
631 (KLUDGE: That was a non-STYLE WARNING.~%~
632 Ordinarily that would cause compilation to~%~
633 fail. However, since we're running under~%~
634 CMU CL, and since CMU CL emits non-STYLE~%~
635 warnings for safe, hard-to-fix things (e.g.~%~
636 references to not-yet-defined functions)~%~
637 we're going to have to ignore it and proceed~%~
638 anyway. Hopefully we're not ignoring anything~%~
641 (muffle-warning-or-die)))
643 (compiler-error "(during macroexpansion)~%~A" c))))
644 (funcall sb!xc:*macroexpand-hook*
649 ;;;; conversion utilities
651 ;;; Convert a bunch of forms, discarding all the values except the
652 ;;; last. If there aren't any forms, then translate a NIL.
653 (declaim (ftype (function (continuation continuation list) (values))
654 ir1-convert-progn-body))
655 (defun ir1-convert-progn-body (start cont body)
657 (reference-constant start cont nil)
658 (let ((this-start start)
661 (let ((form (car forms)))
662 (when (endp (cdr forms))
663 (ir1-convert this-start cont form)
665 (let ((this-cont (make-continuation)))
666 (ir1-convert this-start this-cont form)
667 (setq this-start this-cont forms (cdr forms)))))))
670 ;;;; converting combinations
672 ;;; Convert a function call where the function (Fun) is a Leaf. We
673 ;;; return the Combination node so that we can poke at it if we want to.
674 (declaim (ftype (function (continuation continuation list leaf) combination)
675 ir1-convert-combination))
676 (defun ir1-convert-combination (start cont form fun)
677 (let ((fun-cont (make-continuation)))
678 (reference-leaf start fun-cont fun)
679 (ir1-convert-combination-args fun-cont cont (cdr form))))
681 ;;; Convert the arguments to a call and make the Combination node. Fun-Cont
682 ;;; is the continuation which yields the function to call. Form is the source
683 ;;; for the call. Args is the list of arguments for the call, which defaults
684 ;;; to the cdr of source. We return the Combination node.
685 (defun ir1-convert-combination-args (fun-cont cont args)
686 (declare (type continuation fun-cont cont) (list args))
687 (let ((node (make-combination fun-cont)))
688 (setf (continuation-dest fun-cont) node)
689 (assert-continuation-type fun-cont
690 (specifier-type '(or function symbol)))
691 (collect ((arg-conts))
692 (let ((this-start fun-cont))
694 (let ((this-cont (make-continuation node)))
695 (ir1-convert this-start this-cont arg)
696 (setq this-start this-cont)
697 (arg-conts this-cont)))
698 (prev-link node this-start)
699 (use-continuation node cont)
700 (setf (combination-args node) (arg-conts))))
703 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
704 ;;; source transforms and try out any inline expansion. If there is no
705 ;;; expansion, but is :INLINE, then give an efficiency note (unless a known
706 ;;; function which will quite possibly be open-coded.) Next, we go to
707 ;;; ok-combination conversion.
708 (defun ir1-convert-srctran (start cont var form)
709 (declare (type continuation start cont) (type global-var var))
710 (let ((inlinep (when (defined-function-p var)
711 (defined-function-inlinep var))))
713 ((eq inlinep :notinline)
714 (ir1-convert-combination start cont form var))
715 (*converting-for-interpreter*
716 (ir1-convert-combination-checking-type start cont form var))
718 (let ((transform (info :function :source-transform (leaf-name var))))
721 (multiple-value-bind (result pass) (funcall transform form)
723 (ir1-convert-maybe-predicate start cont form var)
724 (ir1-convert start cont result))))
726 (ir1-convert-maybe-predicate start cont form var))))))))
728 ;;; If the function has the Predicate attribute, and the CONT's DEST isn't
729 ;;; an IF, then we convert (IF <form> T NIL), ensuring that a predicate always
730 ;;; appears in a conditional context.
732 ;;; If the function isn't a predicate, then we call
733 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
734 (defun ir1-convert-maybe-predicate (start cont form var)
735 (declare (type continuation start cont) (list form) (type global-var var))
736 (let ((info (info :function :info (leaf-name var))))
738 (ir1-attributep (function-info-attributes info) predicate)
739 (not (if-p (continuation-dest cont))))
740 (ir1-convert start cont `(if ,form t nil))
741 (ir1-convert-combination-checking-type start cont form var))))
743 ;;; Actually really convert a global function call that we are allowed
746 ;;; If we know the function type of the function, then we check the
747 ;;; call for syntactic legality with respect to the declared function
748 ;;; type. If it is impossible to determine whether the call is correct
749 ;;; due to non-constant keywords, then we give up, marking the call as
750 ;;; :FULL to inhibit further error messages. We return true when the
753 ;;; If the call is legal, we also propagate type assertions from the
754 ;;; function type to the arg and result continuations. We do this now
755 ;;; so that IR1 optimize doesn't have to redundantly do the check
756 ;;; later so that it can do the type propagation.
757 (defun ir1-convert-combination-checking-type (start cont form var)
758 (declare (type continuation start cont) (list form) (type leaf var))
759 (let* ((node (ir1-convert-combination start cont form var))
760 (fun-cont (basic-combination-fun node))
761 (type (leaf-type var)))
762 (when (validate-call-type node type t)
763 (setf (continuation-%derived-type fun-cont) type)
764 (setf (continuation-reoptimize fun-cont) nil)
765 (setf (continuation-%type-check fun-cont) nil)))
769 ;;; Convert a call to a local function. If the function has already
770 ;;; been let converted, then throw FUN to LOCAL-CALL-LOSSAGE. This
771 ;;; should only happen when we are converting inline expansions for
772 ;;; local functions during optimization.
773 (defun ir1-convert-local-combination (start cont form fun)
774 (if (functional-kind fun)
775 (throw 'local-call-lossage fun)
776 (ir1-convert-combination start cont form
777 (maybe-reanalyze-function fun))))
781 ;;; Given a list of Lambda-Var structures and a variable name, return
782 ;;; the structure for that name, or NIL if it isn't found. We return
783 ;;; the *last* variable with that name, since LET* bindings may be
784 ;;; duplicated, and declarations always apply to the last.
785 (declaim (ftype (function (list symbol) (or lambda-var list))
787 (defun find-in-bindings (vars name)
791 (when (eq (leaf-name var) name)
793 (let ((info (lambda-var-arg-info var)))
795 (let ((supplied-p (arg-info-supplied-p info)))
796 (when (and supplied-p
797 (eq (leaf-name supplied-p) name))
798 (setq found supplied-p))))))
799 ((and (consp var) (eq (car var) name))
800 (setf found (cdr var)))))
803 ;;; Called by Process-Decls to deal with a variable type declaration.
804 ;;; If a lambda-var being bound, we intersect the type with the vars
805 ;;; type, otherwise we add a type-restriction on the var. If a symbol
806 ;;; macro, we just wrap a THE around the expansion.
807 (defun process-type-decl (decl res vars)
808 (declare (list decl vars) (type lexenv res))
809 (let ((type (specifier-type (first decl))))
810 (collect ((restr nil cons)
812 (dolist (var-name (rest decl))
813 (let* ((bound-var (find-in-bindings vars var-name))
815 (lexenv-find var-name variables)
816 (find-free-variable var-name))))
819 (let* ((old-type (or (lexenv-find var type-restrictions)
821 (int (if (or (function-type-p type)
822 (function-type-p old-type))
824 (type-approx-intersection2 old-type type))))
825 (cond ((eq int *empty-type*)
826 (unless (policy *lexenv* (= inhibit-warnings 3))
828 "The type declarations ~S and ~S for ~S conflict."
829 (type-specifier old-type) (type-specifier type)
831 (bound-var (setf (leaf-type bound-var) int))
833 (restr (cons var int))))))
835 ;; FIXME: non-ANSI weirdness
836 (aver (eq (car var) 'MACRO))
837 (new-vars `(,var-name . (MACRO . (the ,(first decl)
841 "~S is an alien variable, so its type can't be declared."
844 (if (or (restr) (new-vars))
845 (make-lexenv :default res
846 :type-restrictions (restr)
847 :variables (new-vars))
850 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
851 ;;; declarations for function variables. In addition to allowing
852 ;;; declarations for functions being bound, we must also deal with
853 ;;; declarations that constrain the type of lexically apparent
855 (defun process-ftype-decl (spec res names fvars)
856 (declare (list spec names fvars) (type lexenv res))
857 (let ((type (specifier-type spec)))
858 (collect ((res nil cons))
860 (let ((found (find name fvars :key #'leaf-name :test #'equal)))
863 (setf (leaf-type found) type)
864 (assert-definition-type found type
865 :warning-function #'compiler-note
866 :where "FTYPE declaration"))
868 (res (cons (find-lexically-apparent-function
869 name "in a function type declaration")
872 (make-lexenv :default res :type-restrictions (res))
875 ;;; Process a special declaration, returning a new LEXENV. A non-bound
876 ;;; special declaration is instantiated by throwing a special variable
877 ;;; into the variables.
878 (defun process-special-decl (spec res vars)
879 (declare (list spec vars) (type lexenv res))
880 (collect ((new-venv nil cons))
881 (dolist (name (cdr spec))
882 (let ((var (find-in-bindings vars name)))
885 (aver (eq (car var) 'MACRO))
887 "~S is a symbol-macro and thus can't be declared special."
890 (when (lambda-var-ignorep var)
891 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
892 ;; requires that this be a STYLE-WARNING, not a full WARNING.
893 (compiler-style-warning
894 "The ignored variable ~S is being declared special."
896 (setf (lambda-var-specvar var)
897 (specvar-for-binding name)))
899 (unless (assoc name (new-venv) :test #'eq)
900 (new-venv (cons name (specvar-for-binding name))))))))
902 (make-lexenv :default res :variables (new-venv))
905 ;;; Return a DEFINED-FUNCTION which copies a global-var but for its inlinep.
906 (defun make-new-inlinep (var inlinep)
907 (declare (type global-var var) (type inlinep inlinep))
908 (let ((res (make-defined-function
909 :name (leaf-name var)
910 :where-from (leaf-where-from var)
911 :type (leaf-type var)
913 (when (defined-function-p var)
914 (setf (defined-function-inline-expansion res)
915 (defined-function-inline-expansion var))
916 (setf (defined-function-functional res)
917 (defined-function-functional var)))
920 ;;; Parse an inline/notinline declaration. If it's a local function we're
921 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
922 (defun process-inline-decl (spec res fvars)
923 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
925 (dolist (name (rest spec))
926 (let ((fvar (find name fvars :key #'leaf-name :test #'equal)))
928 (setf (functional-inlinep fvar) sense)
930 (find-lexically-apparent-function
931 name "in an inline or notinline declaration")))
934 (when (policy *lexenv* (>= speed inhibit-warnings))
935 (compiler-note "ignoring ~A declaration not at ~
936 definition of local function:~% ~S"
939 (push (cons name (make-new-inlinep found sense))
943 (make-lexenv :default res :functions new-fenv)
946 ;;; Like FIND-IN-BINDINGS, but looks for #'foo in the fvars.
947 (defun find-in-bindings-or-fbindings (name vars fvars)
948 (declare (list vars fvars))
950 (destructuring-bind (wot fn-name) name
951 (unless (eq wot 'function)
952 (compiler-error "The function or variable name ~S is unrecognizable."
954 (find fn-name fvars :key #'leaf-name :test #'equal))
955 (find-in-bindings vars name)))
957 ;;; Process an ignore/ignorable declaration, checking for various losing
959 (defun process-ignore-decl (spec vars fvars)
960 (declare (list spec vars fvars))
961 (dolist (name (rest spec))
962 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
965 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
966 ;; requires that this be a STYLE-WARNING, not a full WARNING.
967 (compiler-style-warning "declaring unknown variable ~S to be ignored"
969 ;; FIXME: This special case looks like non-ANSI weirdness.
970 ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
971 ;; Just ignore the IGNORE decl.
974 (setf (leaf-ever-used var) t))
975 ((lambda-var-specvar var)
976 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
977 ;; requires that this be a STYLE-WARNING, not a full WARNING.
978 (compiler-style-warning "declaring special variable ~S to be ignored"
980 ((eq (first spec) 'ignorable)
981 (setf (leaf-ever-used var) t))
983 (setf (lambda-var-ignorep var) t)))))
986 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
987 ;;; go away, I think.
988 (defvar *suppress-values-declaration* nil
990 "If true, processing of the VALUES declaration is inhibited.")
992 ;;; Process a single declaration spec, augmenting the specified LEXENV
993 ;;; RES and returning it as a result. VARS and FVARS are as described in
995 (defun process-1-decl (raw-spec res vars fvars cont)
996 (declare (type list raw-spec vars fvars))
997 (declare (type lexenv res))
998 (declare (type continuation cont))
999 (let ((spec (canonized-decl-spec raw-spec)))
1001 (special (process-special-decl spec res vars))
1004 (compiler-error "No type specified in FTYPE declaration: ~S" spec))
1005 (process-ftype-decl (second spec) res (cddr spec) fvars))
1006 ((inline notinline maybe-inline)
1007 (process-inline-decl spec res fvars))
1009 (process-ignore-decl spec vars fvars)
1014 :policy (process-optimize-decl spec (lexenv-policy res))))
1016 (process-type-decl (cdr spec) res vars))
1018 (if *suppress-values-declaration*
1020 (let ((types (cdr spec)))
1021 (do-the-stuff (if (eql (length types) 1)
1024 cont res 'values))))
1026 (when (policy *lexenv* (> speed inhibit-warnings))
1028 "compiler limitation:~
1029 ~% There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1032 (unless (info :declaration :recognized (first spec))
1033 (compiler-warning "unrecognized declaration ~S" raw-spec))
1036 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1037 ;;; and FUNCTIONAL structures which are being bound. In addition to
1038 ;;; filling in slots in the leaf structures, we return a new LEXENV
1039 ;;; which reflects pervasive special and function type declarations,
1040 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1041 ;;; continuation affected by VALUES declarations.
1043 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1045 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1046 (declare (list decls vars fvars) (type continuation cont))
1047 (dolist (decl decls)
1048 (dolist (spec (rest decl))
1049 (unless (consp spec)
1050 (compiler-error "malformed declaration specifier ~S in ~S"
1053 (setq env (process-1-decl spec env vars fvars cont))))
1056 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1057 ;;; declaration. If there is a global variable of that name, then
1058 ;;; check that it isn't a constant and return it. Otherwise, create an
1059 ;;; anonymous GLOBAL-VAR.
1060 (defun specvar-for-binding (name)
1061 (cond ((not (eq (info :variable :where-from name) :assumed))
1062 (let ((found (find-free-variable name)))
1063 (when (heap-alien-info-p found)
1065 "~S is an alien variable and so can't be declared special."
1067 (when (or (not (global-var-p found))
1068 (eq (global-var-kind found) :constant))
1070 "~S is a constant and so can't be declared special."
1074 (make-global-var :kind :special
1076 :where-from :declared))))
1080 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
1081 ;;;; function representation" before you seriously mess with this
1084 ;;; Verify that a thing is a legal name for a variable and return a
1085 ;;; Var structure for it, filling in info if it is globally special.
1086 ;;; If it is losing, we punt with a Compiler-Error. Names-So-Far is an
1087 ;;; alist of names which have previously been bound. If the name is in
1088 ;;; this list, then we error out.
1089 (declaim (ftype (function (t list) lambda-var) varify-lambda-arg))
1090 (defun varify-lambda-arg (name names-so-far)
1091 (declare (inline member))
1092 (unless (symbolp name)
1093 (compiler-error "The lambda-variable ~S is not a symbol." name))
1094 (when (member name names-so-far :test #'eq)
1095 (compiler-error "The variable ~S occurs more than once in the lambda-list."
1097 (let ((kind (info :variable :kind name)))
1098 (when (or (keywordp name) (eq kind :constant))
1099 (compiler-error "The name of the lambda-variable ~S is a constant."
1101 (cond ((eq kind :special)
1102 (let ((specvar (find-free-variable name)))
1103 (make-lambda-var :name name
1104 :type (leaf-type specvar)
1105 :where-from (leaf-where-from specvar)
1108 (note-lexical-binding name)
1109 (make-lambda-var :name name)))))
1111 ;;; Make the default keyword for a &KEY arg, checking that the keyword
1112 ;;; isn't already used by one of the VARS. We also check that the
1113 ;;; keyword isn't the magical :ALLOW-OTHER-KEYS.
1114 (declaim (ftype (function (symbol list t) keyword) make-keyword-for-arg))
1115 (defun make-keyword-for-arg (symbol vars keywordify)
1116 (let ((key (if (and keywordify (not (keywordp symbol)))
1117 (keywordicate symbol)
1119 (when (eq key :allow-other-keys)
1120 (compiler-error "No &KEY arg can be called :ALLOW-OTHER-KEYS."))
1122 (let ((info (lambda-var-arg-info var)))
1124 (eq (arg-info-kind info) :keyword)
1125 (eq (arg-info-key info) key))
1127 "The keyword ~S appears more than once in the lambda-list."
1131 ;;; Parse a lambda-list into a list of VAR structures, stripping off
1132 ;;; any aux bindings. Each arg name is checked for legality, and
1133 ;;; duplicate names are checked for. If an arg is globally special,
1134 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
1135 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
1136 ;;; which contains the extra information. If we hit something losing,
1137 ;;; we bug out with COMPILER-ERROR. These values are returned:
1138 ;;; 1. a list of the var structures for each top-level argument;
1139 ;;; 2. a flag indicating whether &KEY was specified;
1140 ;;; 3. a flag indicating whether other &KEY args are allowed;
1141 ;;; 4. a list of the &AUX variables; and
1142 ;;; 5. a list of the &AUX values.
1143 (declaim (ftype (function (list) (values list boolean boolean list list))
1145 (defun find-lambda-vars (list)
1146 (multiple-value-bind (required optional restp rest keyp keys allowp aux
1147 morep more-context more-count)
1148 (parse-lambda-list list)
1153 (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
1154 ;; for optionals and keywords args.
1155 (parse-default (spec info)
1156 (when (consp (cdr spec))
1157 (setf (arg-info-default info) (second spec))
1158 (when (consp (cddr spec))
1159 (let* ((supplied-p (third spec))
1160 (supplied-var (varify-lambda-arg supplied-p
1162 (setf (arg-info-supplied-p info) supplied-var)
1163 (names-so-far supplied-p)
1164 (when (> (length (the list spec)) 3)
1166 "The list ~S is too long to be an arg specifier."
1169 (dolist (name required)
1170 (let ((var (varify-lambda-arg name (names-so-far))))
1172 (names-so-far name)))
1174 (dolist (spec optional)
1176 (let ((var (varify-lambda-arg spec (names-so-far))))
1177 (setf (lambda-var-arg-info var) (make-arg-info :kind :optional))
1179 (names-so-far spec))
1180 (let* ((name (first spec))
1181 (var (varify-lambda-arg name (names-so-far)))
1182 (info (make-arg-info :kind :optional)))
1183 (setf (lambda-var-arg-info var) info)
1186 (parse-default spec info))))
1189 (let ((var (varify-lambda-arg rest (names-so-far))))
1190 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
1192 (names-so-far rest)))
1195 (let ((var (varify-lambda-arg more-context (names-so-far))))
1196 (setf (lambda-var-arg-info var)
1197 (make-arg-info :kind :more-context))
1199 (names-so-far more-context))
1200 (let ((var (varify-lambda-arg more-count (names-so-far))))
1201 (setf (lambda-var-arg-info var)
1202 (make-arg-info :kind :more-count))
1204 (names-so-far more-count)))
1209 (let ((var (varify-lambda-arg spec (names-so-far))))
1210 (setf (lambda-var-arg-info var)
1211 (make-arg-info :kind :keyword
1212 :key (make-keyword-for-arg spec
1216 (names-so-far spec)))
1217 ((atom (first spec))
1218 (let* ((name (first spec))
1219 (var (varify-lambda-arg name (names-so-far)))
1220 (info (make-arg-info
1222 :key (make-keyword-for-arg name (vars) t))))
1223 (setf (lambda-var-arg-info var) info)
1226 (parse-default spec info)))
1228 (let ((head (first spec)))
1229 (unless (proper-list-of-length-p head 2)
1230 (error "malformed &KEY argument specifier: ~S" spec))
1231 (let* ((name (second head))
1232 (var (varify-lambda-arg name (names-so-far)))
1233 (info (make-arg-info
1235 :key (make-keyword-for-arg (first head)
1238 (setf (lambda-var-arg-info var) info)
1241 (parse-default spec info))))))
1245 (let ((var (varify-lambda-arg spec nil)))
1248 (names-so-far spec)))
1250 (unless (proper-list-of-length-p spec 1 2)
1251 (compiler-error "malformed &AUX binding specifier: ~S"
1253 (let* ((name (first spec))
1254 (var (varify-lambda-arg name nil)))
1256 (aux-vals (second spec))
1257 (names-so-far name)))))
1259 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
1261 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
1262 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
1263 ;;; converting the body. If there are no bindings, just convert the
1264 ;;; body, otherwise do one binding and recurse on the rest.
1265 (defun ir1-convert-aux-bindings (start cont body aux-vars aux-vals)
1266 (declare (type continuation start cont) (list body aux-vars aux-vals))
1268 (ir1-convert-progn-body start cont body)
1269 (let ((fun-cont (make-continuation))
1270 (fun (ir1-convert-lambda-body body
1271 (list (first aux-vars))
1272 :aux-vars (rest aux-vars)
1273 :aux-vals (rest aux-vals))))
1274 (reference-leaf start fun-cont fun)
1275 (ir1-convert-combination-args fun-cont cont
1276 (list (first aux-vals)))))
1279 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
1280 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
1281 ;;; around the body. If there are no special bindings, we just convert
1282 ;;; the body, otherwise we do one special binding and recurse on the
1285 ;;; We make a cleanup and introduce it into the lexical environment.
1286 ;;; If there are multiple special bindings, the cleanup for the blocks
1287 ;;; will end up being the innermost one. We force CONT to start a
1288 ;;; block outside of this cleanup, causing cleanup code to be emitted
1289 ;;; when the scope is exited.
1290 (defun ir1-convert-special-bindings (start cont body aux-vars aux-vals svars)
1291 (declare (type continuation start cont)
1292 (list body aux-vars aux-vals svars))
1295 (ir1-convert-aux-bindings start cont body aux-vars aux-vals))
1297 (continuation-starts-block cont)
1298 (let ((cleanup (make-cleanup :kind :special-bind))
1300 (next-cont (make-continuation))
1301 (nnext-cont (make-continuation)))
1302 (ir1-convert start next-cont
1303 `(%special-bind ',(lambda-var-specvar var) ,var))
1304 (setf (cleanup-mess-up cleanup) (continuation-use next-cont))
1305 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
1306 (ir1-convert next-cont nnext-cont '(%cleanup-point))
1307 (ir1-convert-special-bindings nnext-cont cont body aux-vars aux-vals
1311 ;;; Create a lambda node out of some code, returning the result. The
1312 ;;; bindings are specified by the list of VAR structures VARS. We deal
1313 ;;; with adding the names to the LEXENV-VARIABLES for the conversion.
1314 ;;; The result is added to the NEW-FUNCTIONS in the
1315 ;;; *CURRENT-COMPONENT* and linked to the component head and tail.
1317 ;;; We detect special bindings here, replacing the original VAR in the
1318 ;;; lambda list with a temporary variable. We then pass a list of the
1319 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
1320 ;;; the special binding code.
1322 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
1323 ;;; dealing with &nonsense.
1325 ;;; AUX-VARS is a list of VAR structures for variables that are to be
1326 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
1327 ;;; to get the initial value for the corresponding AUX-VAR.
1328 (defun ir1-convert-lambda-body (body vars &key aux-vars aux-vals result)
1329 (declare (list body vars aux-vars aux-vals)
1330 (type (or continuation null) result))
1331 (let* ((bind (make-bind))
1332 (lambda (make-lambda :vars vars :bind bind))
1333 (result (or result (make-continuation))))
1334 (setf (lambda-home lambda) lambda)
1336 (new-venv nil cons))
1339 (setf (lambda-var-home var) lambda)
1340 (let ((specvar (lambda-var-specvar var)))
1343 (new-venv (cons (leaf-name specvar) specvar)))
1345 (note-lexical-binding (leaf-name var))
1346 (new-venv (cons (leaf-name var) var))))))
1348 (let ((*lexenv* (make-lexenv :variables (new-venv)
1351 (setf (bind-lambda bind) lambda)
1352 (setf (node-lexenv bind) *lexenv*)
1354 (let ((cont1 (make-continuation))
1355 (cont2 (make-continuation)))
1356 (continuation-starts-block cont1)
1357 (prev-link bind cont1)
1358 (use-continuation bind cont2)
1359 (ir1-convert-special-bindings cont2 result body aux-vars aux-vals
1362 (let ((block (continuation-block result)))
1364 (let ((return (make-return :result result :lambda lambda))
1365 (tail-set (make-tail-set :functions (list lambda)))
1366 (dummy (make-continuation)))
1367 (setf (lambda-tail-set lambda) tail-set)
1368 (setf (lambda-return lambda) return)
1369 (setf (continuation-dest result) return)
1370 (setf (block-last block) return)
1371 (prev-link return result)
1372 (use-continuation return dummy))
1373 (link-blocks block (component-tail *current-component*))))))
1375 (link-blocks (component-head *current-component*) (node-block bind))
1376 (push lambda (component-new-functions *current-component*))
1379 ;;; Create the actual entry-point function for an optional entry
1380 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
1381 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
1382 ;;; to the VARS by name. The VALS are passed in in reverse order.
1384 ;;; If any of the copies of the vars are referenced more than once,
1385 ;;; then we mark the corresponding var as EVER-USED to inhibit
1386 ;;; "defined but not read" warnings for arguments that are only used
1387 ;;; by default forms.
1388 (defun convert-optional-entry (fun vars vals defaults)
1389 (declare (type clambda fun) (list vars vals defaults))
1390 (let* ((fvars (reverse vars))
1391 (arg-vars (mapcar (lambda (var)
1392 (unless (lambda-var-specvar var)
1393 (note-lexical-binding (leaf-name var)))
1395 :name (leaf-name var)
1396 :type (leaf-type var)
1397 :where-from (leaf-where-from var)
1398 :specvar (lambda-var-specvar var)))
1401 (ir1-convert-lambda-body `((%funcall ,fun
1405 (mapc (lambda (var arg-var)
1406 (when (cdr (leaf-refs arg-var))
1407 (setf (leaf-ever-used var) t)))
1411 ;;; This function deals with supplied-p vars in optional arguments. If
1412 ;;; the there is no supplied-p arg, then we just call
1413 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
1414 ;;; optional entry that calls the result. If there is a supplied-p
1415 ;;; var, then we add it into the default vars and throw a T into the
1416 ;;; entry values. The resulting entry point function is returned.
1417 (defun generate-optional-default-entry (res default-vars default-vals
1418 entry-vars entry-vals
1419 vars supplied-p-p body
1420 aux-vars aux-vals cont)
1421 (declare (type optional-dispatch res)
1422 (list default-vars default-vals entry-vars entry-vals vars body
1424 (type (or continuation null) cont))
1425 (let* ((arg (first vars))
1426 (arg-name (leaf-name arg))
1427 (info (lambda-var-arg-info arg))
1428 (supplied-p (arg-info-supplied-p info))
1430 (ir1-convert-hairy-args
1432 (list* supplied-p arg default-vars)
1433 (list* (leaf-name supplied-p) arg-name default-vals)
1434 (cons arg entry-vars)
1435 (list* t arg-name entry-vals)
1436 (rest vars) t body aux-vars aux-vals cont)
1437 (ir1-convert-hairy-args
1439 (cons arg default-vars)
1440 (cons arg-name default-vals)
1441 (cons arg entry-vars)
1442 (cons arg-name entry-vals)
1443 (rest vars) supplied-p-p body aux-vars aux-vals cont))))
1445 (convert-optional-entry ep default-vars default-vals
1447 (list (arg-info-default info) nil)
1448 (list (arg-info-default info))))))
1450 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
1451 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
1452 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
1454 ;;; The most interesting thing that we do is parse keywords. We create
1455 ;;; a bunch of temporary variables to hold the result of the parse,
1456 ;;; and then loop over the supplied arguments, setting the appropriate
1457 ;;; temps for the supplied keyword. Note that it is significant that
1458 ;;; we iterate over the keywords in reverse order --- this implements
1459 ;;; the CL requirement that (when a keyword appears more than once)
1460 ;;; the first value is used.
1462 ;;; If there is no supplied-p var, then we initialize the temp to the
1463 ;;; default and just pass the temp into the main entry. Since
1464 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
1465 ;;; know that the default is constant, and thus safe to evaluate out
1468 ;;; If there is a supplied-p var, then we create temps for both the
1469 ;;; value and the supplied-p, and pass them into the main entry,
1470 ;;; letting it worry about defaulting.
1472 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
1473 ;;; until we have scanned all the keywords.
1474 (defun convert-more-entry (res entry-vars entry-vals rest morep keys)
1475 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
1476 (collect ((arg-vars)
1477 (arg-vals (reverse entry-vals))
1481 (dolist (var (reverse entry-vars))
1482 (arg-vars (make-lambda-var :name (leaf-name var)
1483 :type (leaf-type var)
1484 :where-from (leaf-where-from var))))
1486 (let* ((n-context (gensym "N-CONTEXT-"))
1487 (context-temp (make-lambda-var :name n-context))
1488 (n-count (gensym "N-COUNT-"))
1489 (count-temp (make-lambda-var :name n-count
1490 :type (specifier-type 'index))))
1492 (arg-vars context-temp count-temp)
1495 (arg-vals `(%listify-rest-args ,n-context ,n-count)))
1497 (arg-vals n-context)
1500 (when (optional-dispatch-keyp res)
1501 (let ((n-index (gensym "N-INDEX-"))
1502 (n-key (gensym "N-KEY-"))
1503 (n-value-temp (gensym "N-VALUE-TEMP-"))
1504 (n-allowp (gensym "N-ALLOWP-"))
1505 (n-losep (gensym "N-LOSEP-"))
1506 (allowp (or (optional-dispatch-allowp res)
1507 (policy *lexenv* (zerop safety)))))
1509 (temps `(,n-index (1- ,n-count)) n-key n-value-temp)
1510 (body `(declare (fixnum ,n-index) (ignorable ,n-key ,n-value-temp)))
1514 (let* ((info (lambda-var-arg-info key))
1515 (default (arg-info-default info))
1516 (keyword (arg-info-key info))
1517 (supplied-p (arg-info-supplied-p info))
1518 (n-value (gensym "N-VALUE-")))
1519 (temps `(,n-value ,default))
1521 (let ((n-supplied (gensym "N-SUPPLIED-")))
1523 (arg-vals n-value n-supplied)
1524 (tests `((eq ,n-key ',keyword)
1525 (setq ,n-supplied t)
1526 (setq ,n-value ,n-value-temp)))))
1529 (tests `((eq ,n-key ',keyword)
1530 (setq ,n-value ,n-value-temp)))))))
1533 (temps n-allowp n-losep)
1534 (tests `((eq ,n-key :allow-other-keys)
1535 (setq ,n-allowp ,n-value-temp)))
1537 (setq ,n-losep ,n-key))))
1540 `(when (oddp ,n-count)
1541 (%odd-key-arguments-error)))
1545 (declare (optimize (safety 0)))
1547 (when (minusp ,n-index) (return))
1548 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
1550 (setq ,n-key (%more-arg ,n-context ,n-index))
1555 (body `(when (and ,n-losep (not ,n-allowp))
1556 (%unknown-key-argument-error ,n-losep)))))))
1558 (let ((ep (ir1-convert-lambda-body
1561 (%funcall ,(optional-dispatch-main-entry res)
1562 . ,(arg-vals)))) ; FIXME: What is the '.'? ,@?
1564 (setf (optional-dispatch-more-entry res) ep))))
1568 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
1569 ;;; or &KEY arg. The arguments are similar to that function, but we
1570 ;;; split off any &REST arg and pass it in separately. REST is the
1571 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
1572 ;;; the &KEY argument vars.
1574 ;;; When there are &KEY arguments, we introduce temporary gensym
1575 ;;; variables to hold the values while keyword defaulting is in
1576 ;;; progress to get the required sequential binding semantics.
1578 ;;; This gets interesting mainly when there are &KEY arguments with
1579 ;;; supplied-p vars or non-constant defaults. In either case, pass in
1580 ;;; a supplied-p var. If the default is non-constant, we introduce an
1581 ;;; IF in the main entry that tests the supplied-p var and decides
1582 ;;; whether to evaluate the default or not. In this case, the real
1583 ;;; incoming value is NIL, so we must union NULL with the declared
1584 ;;; type when computing the type for the main entry's argument.
1585 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
1586 rest more-context more-count keys supplied-p-p
1587 body aux-vars aux-vals cont)
1588 (declare (type optional-dispatch res)
1589 (list default-vars default-vals entry-vars entry-vals keys body
1591 (type (or continuation null) cont))
1592 (collect ((main-vars (reverse default-vars))
1593 (main-vals default-vals cons)
1600 (main-vars more-context)
1602 (main-vars more-count)
1606 (let* ((info (lambda-var-arg-info key))
1607 (default (arg-info-default info))
1608 (hairy-default (not (sb!xc:constantp default)))
1609 (supplied-p (arg-info-supplied-p info))
1610 (n-val (make-symbol (format nil
1611 "~A-DEFAULTING-TEMP"
1613 (key-type (leaf-type key))
1614 (val-temp (make-lambda-var
1616 :type (if hairy-default
1617 (type-union key-type (specifier-type 'null))
1619 (main-vars val-temp)
1621 (cond ((or hairy-default supplied-p)
1622 (let* ((n-supplied (gensym "N-SUPPLIED-"))
1623 (supplied-temp (make-lambda-var :name n-supplied)))
1625 (setf (arg-info-supplied-p info) supplied-temp))
1627 (setf (arg-info-default info) nil))
1628 (main-vars supplied-temp)
1629 (cond (hairy-default
1631 (bind-vals `(if ,n-supplied ,n-val ,default)))
1633 (main-vals default nil)
1636 (bind-vars supplied-p)
1637 (bind-vals n-supplied))))
1639 (main-vals (arg-info-default info))
1640 (bind-vals n-val)))))
1642 (let* ((main-entry (ir1-convert-lambda-body
1644 :aux-vars (append (bind-vars) aux-vars)
1645 :aux-vals (append (bind-vals) aux-vals)
1647 (last-entry (convert-optional-entry main-entry default-vars
1649 (setf (optional-dispatch-main-entry res) main-entry)
1650 (convert-more-entry res entry-vars entry-vals rest more-context keys)
1652 (push (if supplied-p-p
1653 (convert-optional-entry last-entry entry-vars entry-vals ())
1655 (optional-dispatch-entry-points res))
1658 ;;; This function generates the entry point functions for the
1659 ;;; optional-dispatch Res. We accomplish this by recursion on the list of
1660 ;;; arguments, analyzing the arglist on the way down and generating entry
1661 ;;; points on the way up.
1663 ;;; Default-Vars is a reversed list of all the argument vars processed
1664 ;;; so far, including supplied-p vars. Default-Vals is a list of the
1665 ;;; names of the Default-Vars.
1667 ;;; Entry-Vars is a reversed list of processed argument vars,
1668 ;;; excluding supplied-p vars. Entry-Vals is a list things that can be
1669 ;;; evaluated to get the values for all the vars from the Entry-Vars.
1670 ;;; It has the var name for each required or optional arg, and has T
1671 ;;; for each supplied-p arg.
1673 ;;; Vars is a list of the Lambda-Var structures for arguments that
1674 ;;; haven't been processed yet. Supplied-p-p is true if a supplied-p
1675 ;;; argument has already been processed; only in this case are the
1676 ;;; Default-XXX and Entry-XXX different.
1678 ;;; The result at each point is a lambda which should be called by the
1679 ;;; above level to default the remaining arguments and evaluate the
1680 ;;; body. We cause the body to be evaluated by converting it and
1681 ;;; returning it as the result when the recursion bottoms out.
1683 ;;; Each level in the recursion also adds its entry point function to
1684 ;;; the result Optional-Dispatch. For most arguments, the defaulting
1685 ;;; function and the entry point function will be the same, but when
1686 ;;; supplied-p args are present they may be different.
1688 ;;; When we run into a &REST or &KEY arg, we punt out to
1689 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
1690 (defun ir1-convert-hairy-args (res default-vars default-vals
1691 entry-vars entry-vals
1692 vars supplied-p-p body aux-vars
1694 (declare (type optional-dispatch res)
1695 (list default-vars default-vals entry-vars entry-vals vars body
1697 (type (or continuation null) cont))
1699 (if (optional-dispatch-keyp res)
1700 ;; Handle &KEY with no keys...
1701 (ir1-convert-more res default-vars default-vals
1702 entry-vars entry-vals
1703 nil nil nil vars supplied-p-p body aux-vars
1705 (let ((fun (ir1-convert-lambda-body body (reverse default-vars)
1709 (setf (optional-dispatch-main-entry res) fun)
1710 (push (if supplied-p-p
1711 (convert-optional-entry fun entry-vars entry-vals ())
1713 (optional-dispatch-entry-points res))
1715 ((not (lambda-var-arg-info (first vars)))
1716 (let* ((arg (first vars))
1717 (nvars (cons arg default-vars))
1718 (nvals (cons (leaf-name arg) default-vals)))
1719 (ir1-convert-hairy-args res nvars nvals nvars nvals
1720 (rest vars) nil body aux-vars aux-vals
1723 (let* ((arg (first vars))
1724 (info (lambda-var-arg-info arg))
1725 (kind (arg-info-kind info)))
1728 (let ((ep (generate-optional-default-entry
1729 res default-vars default-vals
1730 entry-vars entry-vals vars supplied-p-p body
1731 aux-vars aux-vals cont)))
1732 (push (if supplied-p-p
1733 (convert-optional-entry ep entry-vars entry-vals ())
1735 (optional-dispatch-entry-points res))
1738 (ir1-convert-more res default-vars default-vals
1739 entry-vars entry-vals
1740 arg nil nil (rest vars) supplied-p-p body
1741 aux-vars aux-vals cont))
1743 (ir1-convert-more res default-vars default-vals
1744 entry-vars entry-vals
1745 nil arg (second vars) (cddr vars) supplied-p-p
1746 body aux-vars aux-vals cont))
1748 (ir1-convert-more res default-vars default-vals
1749 entry-vars entry-vals
1750 nil nil nil vars supplied-p-p body aux-vars
1751 aux-vals cont)))))))
1753 ;;; This function deals with the case where we have to make an
1754 ;;; Optional-Dispatch to represent a lambda. We cons up the result and
1755 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
1756 ;;; figure out the min-args and max-args.
1757 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals cont)
1758 (declare (list body vars aux-vars aux-vals) (type continuation cont))
1759 (let ((res (make-optional-dispatch :arglist vars
1762 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
1763 (push res (component-new-functions *current-component*))
1764 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
1766 (setf (optional-dispatch-min-args res) min)
1767 (setf (optional-dispatch-max-args res)
1768 (+ (1- (length (optional-dispatch-entry-points res))) min))
1772 (setf (functional-kind ep) :optional)
1773 (setf (leaf-ever-used ep) t)
1774 (setf (lambda-optional-dispatch ep) res))))
1775 (dolist (ep (optional-dispatch-entry-points res)) (frob ep))
1776 (frob (optional-dispatch-more-entry res))
1777 (frob (optional-dispatch-main-entry res)))
1781 ;;; Convert a Lambda into a Lambda or Optional-Dispatch leaf.
1782 (defun ir1-convert-lambda (form &optional name)
1783 (unless (consp form)
1784 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
1787 (unless (eq (car form) 'lambda)
1788 (compiler-error "~S was expected but ~S was found:~% ~S"
1792 (unless (and (consp (cdr form)) (listp (cadr form)))
1794 "The lambda expression has a missing or non-list lambda-list:~% ~S"
1797 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
1798 (find-lambda-vars (cadr form))
1799 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr form))
1800 (let* ((cont (make-continuation))
1801 (*lexenv* (process-decls decls
1802 (append aux-vars vars)
1804 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
1805 (ir1-convert-hairy-lambda forms vars keyp
1807 aux-vars aux-vals cont)
1808 (ir1-convert-lambda-body forms vars
1812 (setf (functional-inline-expansion res) form)
1813 (setf (functional-arg-documentation res) (cadr form))
1814 (setf (leaf-name res) name)
1817 ;;; FIXME: This file is rather long, and contains two distinct sections,
1818 ;;; transform machinery above this point and transforms themselves below this
1819 ;;; point. Why not split it in two? (ir1translate.lisp and
1820 ;;; ir1translators.lisp?) Then consider byte-compiling the translators, too.
1822 ;;;; control special forms
1824 (def-ir1-translator progn ((&rest forms) start cont)
1827 Evaluates each Form in order, returning the values of the last form. With no
1828 forms, returns NIL."
1829 (ir1-convert-progn-body start cont forms))
1831 (def-ir1-translator if ((test then &optional else) start cont)
1833 "If Predicate Then [Else]
1834 If Predicate evaluates to non-null, evaluate Then and returns its values,
1835 otherwise evaluate Else and return its values. Else defaults to NIL."
1836 (let* ((pred (make-continuation))
1837 (then-cont (make-continuation))
1838 (then-block (continuation-starts-block then-cont))
1839 (else-cont (make-continuation))
1840 (else-block (continuation-starts-block else-cont))
1841 (dummy-cont (make-continuation))
1842 (node (make-if :test pred
1843 :consequent then-block
1844 :alternative else-block)))
1845 (setf (continuation-dest pred) node)
1846 (ir1-convert start pred test)
1847 (prev-link node pred)
1848 (use-continuation node dummy-cont)
1850 (let ((start-block (continuation-block pred)))
1851 (setf (block-last start-block) node)
1852 (continuation-starts-block cont)
1854 (link-blocks start-block then-block)
1855 (link-blocks start-block else-block)
1857 (ir1-convert then-cont cont then)
1858 (ir1-convert else-cont cont else))))
1860 ;;;; BLOCK and TAGBODY
1862 ;;;; We make an Entry node to mark the start and a :Entry cleanup to
1863 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an Exit
1866 ;;; Make a :entry cleanup and emit an Entry node, then convert the
1867 ;;; body in the modified environment. We make Cont start a block now,
1868 ;;; since if it was done later, the block would be in the wrong
1870 (def-ir1-translator block ((name &rest forms) start cont)
1873 Evaluate the Forms as a PROGN. Within the lexical scope of the body,
1874 (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
1875 result of Value-Form."
1876 (unless (symbolp name)
1877 (compiler-error "The block name ~S is not a symbol." name))
1878 (continuation-starts-block cont)
1879 (let* ((dummy (make-continuation))
1880 (entry (make-entry))
1881 (cleanup (make-cleanup :kind :block
1883 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
1884 (setf (entry-cleanup entry) cleanup)
1885 (prev-link entry start)
1886 (use-continuation entry dummy)
1888 (let* ((env-entry (list entry cont))
1889 (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
1891 (push env-entry (continuation-lexenv-uses cont))
1892 (ir1-convert-progn-body dummy cont forms))))
1895 ;;; We make Cont start a block just so that it will have a block
1896 ;;; assigned. People assume that when they pass a continuation into
1897 ;;; IR1-Convert as Cont, it will have a block when it is done.
1898 (def-ir1-translator return-from ((name &optional value)
1901 "Return-From Block-Name Value-Form
1902 Evaluate the Value-Form, returning its values from the lexically enclosing
1903 BLOCK Block-Name. This is constrained to be used only within the dynamic
1904 extent of the BLOCK."
1905 (continuation-starts-block cont)
1906 (let* ((found (or (lexenv-find name blocks)
1907 (compiler-error "return for unknown block: ~S" name)))
1908 (value-cont (make-continuation))
1909 (entry (first found))
1910 (exit (make-exit :entry entry
1911 :value value-cont)))
1912 (push exit (entry-exits entry))
1913 (setf (continuation-dest value-cont) exit)
1914 (ir1-convert start value-cont value)
1915 (prev-link exit value-cont)
1916 (use-continuation exit (second found))))
1918 ;;; Return a list of the segments of a TAGBODY. Each segment looks
1919 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
1920 ;;; tagbody into segments of non-tag statements, and explicitly
1921 ;;; represent the drop-through with a GO. The first segment has a
1922 ;;; dummy NIL tag, since it represents code before the first tag. The
1923 ;;; last segment (which may also be the first segment) ends in NIL
1924 ;;; rather than a GO.
1925 (defun parse-tagbody (body)
1926 (declare (list body))
1927 (collect ((segments))
1928 (let ((current (cons nil body)))
1930 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
1932 (segments `(,@current nil))
1934 (let ((tag (elt current tag-pos)))
1935 (when (assoc tag (segments))
1937 "The tag ~S appears more than once in the tagbody."
1939 (unless (or (symbolp tag) (integerp tag))
1940 (compiler-error "~S is not a legal tagbody statement." tag))
1941 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
1942 (setq current (nthcdr tag-pos current)))))
1945 ;;; Set up the cleanup, emitting the entry node. Then make a block for
1946 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
1947 ;;; Finally, convert each segment with the precomputed Start and Cont
1949 (def-ir1-translator tagbody ((&rest statements) start cont)
1951 "Tagbody {Tag | Statement}*
1952 Define tags for used with GO. The Statements are evaluated in order
1953 (skipping Tags) and NIL is returned. If a statement contains a GO to a
1954 defined Tag within the lexical scope of the form, then control is transferred
1955 to the next statement following that tag. A Tag must an integer or a
1956 symbol. A statement must be a list. Other objects are illegal within the
1958 (continuation-starts-block cont)
1959 (let* ((dummy (make-continuation))
1960 (entry (make-entry))
1961 (segments (parse-tagbody statements))
1962 (cleanup (make-cleanup :kind :tagbody
1964 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
1965 (setf (entry-cleanup entry) cleanup)
1966 (prev-link entry start)
1967 (use-continuation entry dummy)
1973 (dolist (segment (rest segments))
1974 (let* ((tag-cont (make-continuation))
1975 (tag (list (car segment) entry tag-cont)))
1978 (continuation-starts-block tag-cont)
1980 (push (cdr tag) (continuation-lexenv-uses tag-cont))))
1983 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
1984 (mapc (lambda (segment start cont)
1985 (ir1-convert-progn-body start cont (rest segment)))
1986 segments (starts) (conts))))))
1988 ;;; Emit an EXIT node without any value.
1989 (def-ir1-translator go ((tag) start cont)
1992 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
1993 is constrained to be used only within the dynamic extent of the TAGBODY."
1994 (continuation-starts-block cont)
1995 (let* ((found (or (lexenv-find tag tags :test #'eql)
1996 (compiler-error "Go to nonexistent tag: ~S." tag)))
1997 (entry (first found))
1998 (exit (make-exit :entry entry)))
1999 (push exit (entry-exits entry))
2000 (prev-link exit start)
2001 (use-continuation exit (second found))))
2003 ;;;; translators for compiler-magic special forms
2005 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in
2006 ;;; top-level forms are picked off and handled by PROCESS-TOP-LEVEL-FORM,
2007 ;;; so they're never seen at this level.)
2009 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
2010 ;;; of non-top-level EVAL-WHENs is very simple:
2011 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
2012 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
2013 ;;; are ignored for non-top-level forms. For non-top-level forms, an
2014 ;;; eval-when specifying the :EXECUTE situation is treated as an
2015 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
2016 ;;; form; otherwise, the forms in the body are ignored.
2017 (def-ir1-translator eval-when ((situations &rest forms) start cont)
2019 "EVAL-WHEN (Situation*) Form*
2020 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
2021 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
2022 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
2023 (declare (ignore ct lt))
2025 (ir1-convert-progn-body start cont forms)))
2028 ;;; Tweak *LEXENV* to include the DEFINITIONS from a MACROLET, then
2029 ;;; call FUN (with no arguments).
2031 ;;; This is split off from the IR1 convert method so that it can be
2032 ;;; shared by the special-case top-level MACROLET processing code.
2033 (defun funcall-in-macrolet-lexenv (definitions fun)
2034 (declare (type list definitions) (type function fun))
2035 (let* ((whole (gensym "WHOLE"))
2036 (environment (gensym "ENVIRONMENT"))
2037 (processed-definitions
2038 (mapcar (lambda (definition)
2039 (unless (list-of-length-at-least-p definition 2)
2041 "The list ~S is too short to be a legal ~
2042 local macro definition."
2044 (destructuring-bind (name arglist &body body) definition
2045 (unless (symbolp name)
2047 "The local macro name ~S is not a symbol." name))
2048 (multiple-value-bind (body local-decls)
2049 (parse-defmacro arglist whole body name 'macrolet
2050 :environment environment)
2053 `(lambda (,whole ,environment)
2055 (block ,name ,body)))))))
2057 (*lexenv* (make-lexenv :functions processed-definitions)))
2058 (unless (= (length definitions)
2059 (length (remove-duplicates definitions :key #'first)))
2060 (compiler-style-warning
2061 "duplicate macro names in MACROLET ~S" definitions))
2065 (def-ir1-translator macrolet ((definitions &rest body) start cont)
2067 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
2068 Evaluate the Body-Forms in an environment with the specified local macros
2069 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
2070 destructuring lambda list, and the Forms evaluate to the expansion. The
2071 Forms are evaluated in the null environment."
2072 (funcall-in-macrolet-lexenv definitions
2074 (ir1-translate-locally body start cont))))
2076 ;;; Tweak *LEXENV* to include the MACROBINDINGS from a SYMBOL-MACROLET,
2077 ;;; then call FUN (with no arguments).
2079 ;;; This is split off from the IR1 convert method so that it can be
2080 ;;; shared by the special-case top-level SYMBOL-MACROLET processing code.
2081 (defun funcall-in-symbol-macrolet-lexenv (macrobindings fun)
2082 (declare (type list macrobindings) (type function fun))
2083 (let ((processed-macrobindings
2084 (mapcar (lambda (macrobinding)
2085 (unless (proper-list-of-length-p macrobinding 2)
2086 (compiler-error "malformed symbol/expansion pair: ~S"
2088 (destructuring-bind (name expansion) macrobinding
2089 (unless (symbolp name)
2091 "The local symbol macro name ~S is not a symbol."
2093 `(,name . (MACRO . ,expansion))))
2095 (unless (= (length macrobindings)
2096 (length (remove-duplicates macrobindings :key #'first)))
2097 (compiler-style-warning
2098 "duplicate symbol macro names in SYMBOL-MACROLET ~S" macrobindings))
2099 (let ((*lexenv* (make-lexenv :variables processed-macrobindings)))
2103 (def-ir1-translator symbol-macrolet ((macrobindings &body body) start cont)
2105 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
2106 Define the Names as symbol macros with the given Expansions. Within the
2107 body, references to a Name will effectively be replaced with the Expansion."
2108 (funcall-in-symbol-macrolet-lexenv
2111 (ir1-translate-locally body start cont))))
2113 ;;; not really a special form, but..
2114 (def-ir1-translator declare ((&rest stuff) start cont)
2115 (declare (ignore stuff))
2116 ;; We ignore START and CONT too, but we can't use DECLARE IGNORE to
2117 ;; tell the compiler about it here, because the DEF-IR1-TRANSLATOR
2118 ;; macro would put the DECLARE in the wrong place, so..
2120 (compiler-error "misplaced declaration"))
2124 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
2125 ;;;; into a funny function.
2127 ;;; Carefully evaluate a list of forms, returning a list of the results.
2128 (defun eval-info-args (args)
2129 (declare (list args))
2130 (handler-case (mapcar #'eval args)
2132 (compiler-error "Lisp error during evaluation of info args:~%~A"
2135 ;;; If there is a primitive translator, then we expand the call.
2136 ;;; Otherwise, we convert to the %%PRIMITIVE funny function. The first
2137 ;;; argument is the template, the second is a list of the results of
2138 ;;; any codegen-info args, and the remaining arguments are the runtime
2141 ;;; We do a bunch of error checking now so that we don't bomb out with
2142 ;;; a fatal error during IR2 conversion.
2144 ;;; KLUDGE: It's confusing having multiple names floating around for
2145 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
2146 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
2147 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
2148 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
2149 ;;; VOP or %VOP.. -- WHN 2001-06-11
2150 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
2151 (def-ir1-translator %primitive ((&whole form name &rest args) start cont)
2153 (unless (symbolp name)
2154 (compiler-error "The primitive name ~S is not a symbol." name))
2156 (let* ((template (or (gethash name *backend-template-names*)
2158 "The primitive name ~A is not defined."
2160 (required (length (template-arg-types template)))
2161 (info (template-info-arg-count template))
2162 (min (+ required info))
2163 (nargs (length args)))
2164 (if (template-more-args-type template)
2166 (compiler-error "Primitive ~A was called with ~R argument~:P, ~
2167 but wants at least ~R."
2171 (unless (= nargs min)
2172 (compiler-error "Primitive ~A was called with ~R argument~:P, ~
2173 but wants exactly ~R."
2178 (when (eq (template-result-types template) :conditional)
2180 "%PRIMITIVE was used with a conditional template."))
2182 (when (template-more-results-type template)
2184 "%PRIMITIVE was used with an unknown values template."))
2188 `(%%primitive ',template
2190 (subseq args required min))
2191 ,@(subseq args 0 required)
2192 ,@(subseq args min)))))
2194 ;;;; QUOTE and FUNCTION
2196 (def-ir1-translator quote ((thing) start cont)
2199 Return Value without evaluating it."
2200 (reference-constant start cont thing))
2202 (def-ir1-translator function ((thing) start cont)
2205 Return the lexically apparent definition of the function Name. Name may also
2210 (reference-leaf start cont (ir1-convert-lambda thing)))
2212 (let ((var (find-lexically-apparent-function
2213 thing "as the argument to FUNCTION")))
2214 (reference-leaf start cont var)))
2216 (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing)))))
2217 (setf (getf (functional-plist res) :fin-function) t)
2218 (reference-leaf start cont res)))
2220 (compiler-error "~S is not a legal function name." thing)))
2221 (let ((var (find-lexically-apparent-function
2222 thing "as the argument to FUNCTION")))
2223 (reference-leaf start cont var))))
2227 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
2228 ;;; (not symbols). %FUNCALL is used directly in some places where the
2229 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
2230 (deftransform funcall ((function &rest args) * * :when :both)
2231 (let ((arg-names (make-gensym-list (length args))))
2232 `(lambda (function ,@arg-names)
2233 (%funcall ,(if (csubtypep (continuation-type function)
2234 (specifier-type 'function))
2236 '(%coerce-callable-to-function function))
2239 (def-ir1-translator %funcall ((function &rest args) start cont)
2240 (let ((fun-cont (make-continuation)))
2241 (ir1-convert start fun-cont function)
2242 (assert-continuation-type fun-cont (specifier-type 'function))
2243 (ir1-convert-combination-args fun-cont cont args)))
2245 ;;; This source transform exists to reduce the amount of work for the
2246 ;;; compiler. If the called function is a FUNCTION form, then convert
2247 ;;; directly to %FUNCALL, instead of waiting around for type
2249 (def-source-transform funcall (function &rest args)
2250 (if (and (consp function) (eq (car function) 'function))
2251 `(%funcall ,function ,@args)
2254 (deftransform %coerce-callable-to-function ((thing) (function) *
2257 "optimize away possible call to FDEFINITION at runtime"
2260 ;;; This is a frob that DEFSTRUCT expands into to establish the compiler
2261 ;;; semantics. The other code in the expansion and %%COMPILER-DEFSTRUCT do
2262 ;;; most of the work, we just clear all of the functions out of
2263 ;;; *FREE-FUNCTIONS* to keep things in synch. %%COMPILER-DEFSTRUCT is also
2264 ;;; called at load-time.
2265 (def-ir1-translator %compiler-defstruct ((info) start cont :kind :function)
2266 (let* ((info (eval info)))
2267 (%%compiler-defstruct info)
2268 (dolist (slot (dd-slots info))
2269 (let ((fun (dsd-accessor slot)))
2270 (remhash fun *free-functions*)
2271 (unless (dsd-read-only slot)
2272 (remhash `(setf ,fun) *free-functions*))))
2273 (remhash (dd-predicate info) *free-functions*)
2274 (remhash (dd-copier info) *free-functions*)
2275 (ir1-convert start cont `(%%compiler-defstruct ',info))))
2277 ;;; Return the contents of a quoted form.
2281 (eq 'quote (first x)))
2283 (error "not a quoted form")))
2285 ;;; Don't actually compile anything, instead call the function now.
2286 (def-ir1-translator %compiler-only-defstruct
2287 ((info inherits) start cont :kind :function)
2288 (function-%compiler-only-defstruct (unquote info) (unquote inherits))
2289 (reference-constant start cont nil))
2293 ;;;; (LET and LET* can't be implemented as macros due to the fact that
2294 ;;;; any pervasive declarations also affect the evaluation of the
2297 ;;; Given a list of binding specifiers in the style of Let, return:
2298 ;;; 1. The list of var structures for the variables bound.
2299 ;;; 2. The initial value form for each variable.
2301 ;;; The variable names are checked for legality and globally special
2302 ;;; variables are marked as such. Context is the name of the form, for
2303 ;;; error reporting purposes.
2304 (declaim (ftype (function (list symbol) (values list list list))
2305 extract-let-variables))
2306 (defun extract-let-variables (bindings context)
2310 (flet ((get-var (name)
2311 (varify-lambda-arg name
2312 (if (eq context 'let*)
2315 (dolist (spec bindings)
2317 (let ((var (get-var spec)))
2319 (names (cons spec var))
2322 (unless (proper-list-of-length-p spec 1 2)
2323 (compiler-error "The ~S binding spec ~S is malformed."
2326 (let* ((name (first spec))
2327 (var (get-var name)))
2330 (vals (second spec)))))))
2332 (values (vars) (vals) (names))))
2334 (def-ir1-translator let ((bindings &body body)
2337 "LET ({(Var [Value]) | Var}*) Declaration* Form*
2338 During evaluation of the Forms, bind the Vars to the result of evaluating the
2339 Value forms. The variables are bound in parallel after all of the Values are
2341 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2342 (multiple-value-bind (vars values) (extract-let-variables bindings 'let)
2343 (let* ((*lexenv* (process-decls decls vars nil cont))
2344 (fun-cont (make-continuation))
2345 (fun (ir1-convert-lambda-body forms vars)))
2346 (reference-leaf start fun-cont fun)
2347 (ir1-convert-combination-args fun-cont cont values)))))
2349 (def-ir1-translator let* ((bindings &body body)
2352 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
2353 Similar to LET, but the variables are bound sequentially, allowing each Value
2354 form to reference any of the previous Vars."
2355 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2356 (multiple-value-bind (vars values) (extract-let-variables bindings 'let*)
2357 (let ((*lexenv* (process-decls decls vars nil cont)))
2358 (ir1-convert-aux-bindings start cont forms vars values)))))
2360 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
2361 ;;; and SYMBOL-MACROLET
2363 ;;; Note that all these things need to preserve top-level-formness,
2364 ;;; but we don't need to worry about that within an IR1 translator,
2365 ;;; since top-level-formness is picked off by PROCESS-TOP-LEVEL-FOO
2366 ;;; forms before we hit the IR1 transform level.
2367 (defun ir1-translate-locally (body start cont)
2368 (declare (type list body) (type continuation start cont))
2369 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2370 (let ((*lexenv* (process-decls decls nil nil cont)))
2371 (ir1-convert-aux-bindings start cont forms nil nil))))
2373 (def-ir1-translator locally ((&body body) start cont)
2375 "LOCALLY Declaration* Form*
2376 Sequentially evaluate the Forms in a lexical environment where the
2377 the Declarations have effect. If LOCALLY is a top-level form, then
2378 the Forms are also processed as top-level forms."
2379 (ir1-translate-locally body start cont))
2381 ;;;; FLET and LABELS
2383 ;;; Given a list of local function specifications in the style of
2384 ;;; FLET, return lists of the function names and of the lambdas which
2385 ;;; are their definitions.
2387 ;;; The function names are checked for legality. CONTEXT is the name
2388 ;;; of the form, for error reporting.
2389 (declaim (ftype (function (list symbol) (values list list))
2390 extract-flet-variables))
2391 (defun extract-flet-variables (definitions context)
2394 (dolist (def definitions)
2395 (when (or (atom def) (< (length def) 2))
2396 (compiler-error "The ~S definition spec ~S is malformed." context def))
2398 (let ((name (check-function-name (first def))))
2400 (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr def))
2401 (defs `(lambda ,(second def)
2403 (block ,(function-name-block-name name)
2405 (values (names) (defs))))
2407 (def-ir1-translator flet ((definitions &body body)
2410 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
2411 Evaluate the Body-Forms with some local function definitions. The bindings
2412 do not enclose the definitions; any use of Name in the Forms will refer to
2413 the lexically apparent function definition in the enclosing environment."
2414 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2415 (multiple-value-bind (names defs)
2416 (extract-flet-variables definitions 'flet)
2417 (let* ((fvars (mapcar (lambda (n d)
2418 (ir1-convert-lambda d n))
2420 (*lexenv* (make-lexenv
2421 :default (process-decls decls nil fvars cont)
2422 :functions (pairlis names fvars))))
2423 (ir1-convert-progn-body start cont forms)))))
2425 ;;; For LABELS, we have to create dummy function vars and add them to
2426 ;;; the function namespace while converting the functions. We then
2427 ;;; modify all the references to these leaves so that they point to
2428 ;;; the real functional leaves. We also backpatch the FENV so that if
2429 ;;; the lexical environment is used for inline expansion we will get
2430 ;;; the right functions.
2431 (def-ir1-translator labels ((definitions &body body) start cont)
2433 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
2434 Evaluate the Body-Forms with some local function definitions. The bindings
2435 enclose the new definitions, so the defined functions can call themselves or
2437 (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
2438 (multiple-value-bind (names defs)
2439 (extract-flet-variables definitions 'labels)
2440 (let* ((new-fenv (loop for name in names
2441 collect (cons name (make-functional :name name))))
2443 (let ((*lexenv* (make-lexenv :functions new-fenv)))
2444 (mapcar (lambda (n d)
2445 (ir1-convert-lambda d n))
2448 (loop for real in real-funs and env in new-fenv do
2449 (let ((dum (cdr env)))
2450 (substitute-leaf real dum)
2451 (setf (cdr env) real)))
2453 (let ((*lexenv* (make-lexenv
2454 :default (process-decls decls nil real-funs cont)
2455 :functions (pairlis names real-funs))))
2456 (ir1-convert-progn-body start cont forms))))))
2460 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
2461 ;;; continuation that the assertion applies to, TYPE is the type
2462 ;;; specifier and Lexenv is the current lexical environment. NAME is
2463 ;;; the name of the declaration we are doing, for use in error
2466 ;;; This is somewhat involved, since a type assertion may only be made
2467 ;;; on a continuation, not on a node. We can't just set the
2468 ;;; continuation asserted type and let it go at that, since there may
2469 ;;; be parallel THE's for the same continuation, i.e.:
2474 ;;; In this case, our representation can do no better than the union
2475 ;;; of these assertions. And if there is a branch with no assertion,
2476 ;;; we have nothing at all. We really need to recognize scoping, since
2477 ;;; we need to be able to discern between parallel assertions (which
2478 ;;; we union) and nested ones (which we intersect).
2480 ;;; We represent the scoping by throwing our innermost (intersected)
2481 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
2482 ;;; intersect our assertions together. If CONT has no uses yet, we
2483 ;;; have not yet bottomed out on the first COND branch; in this case
2484 ;;; we optimistically assume that this type will be the one we end up
2485 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
2486 ;;; than the type that we have the first time we bottom out. Later
2487 ;;; THE's (or the absence thereof) can only weaken this result.
2489 ;;; We make this work by getting USE-CONTINUATION to do the unioning
2490 ;;; across COND branches. We can't do it here, since we don't know how
2491 ;;; many branches there are going to be.
2492 (defun do-the-stuff (type cont lexenv name)
2493 (declare (type continuation cont) (type lexenv lexenv))
2494 (let* ((ctype (values-specifier-type type))
2495 (old-type (or (lexenv-find cont type-restrictions)
2497 (intersects (values-types-equal-or-intersect old-type ctype))
2498 (int (values-type-intersection old-type ctype))
2499 (new (if intersects int old-type)))
2500 (when (null (find-uses cont))
2501 (setf (continuation-asserted-type cont) new))
2502 (when (and (not intersects)
2503 (not (policy *lexenv*
2504 (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
2506 "The type ~S in ~S declaration conflicts with an enclosing assertion:~% ~S"
2507 (type-specifier ctype)
2509 (type-specifier old-type)))
2510 (make-lexenv :type-restrictions `((,cont . ,new))
2513 ;;; Assert that FORM evaluates to the specified type (which may be a
2516 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
2517 ;;; this didn't seem to expand into an assertion, at least for ALIEN
2518 ;;; values. Check that SBCL doesn't have this problem.
2519 (def-ir1-translator the ((type value) start cont)
2520 (let ((*lexenv* (do-the-stuff type cont *lexenv* 'the)))
2521 (ir1-convert start cont value)))
2523 ;;; This is like the THE special form, except that it believes
2524 ;;; whatever you tell it. It will never generate a type check, but
2525 ;;; will cause a warning if the compiler can prove the assertion is
2528 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
2529 ;;; its uses's types, setting it won't work. Instead we must intersect
2530 ;;; the type with the uses's DERIVED-TYPE.
2531 (def-ir1-translator truly-the ((type value) start cont)
2533 (declare (inline member))
2534 (let ((type (values-specifier-type type))
2535 (old (find-uses cont)))
2536 (ir1-convert start cont value)
2538 (unless (member use old :test #'eq)
2539 (derive-node-type use type)))))
2543 ;;; If there is a definition in LEXENV-VARIABLES, just set that,
2544 ;;; otherwise look at the global information. If the name is for a
2545 ;;; constant, then error out.
2546 (def-ir1-translator setq ((&whole source &rest things) start cont)
2547 (let ((len (length things)))
2549 (compiler-error "odd number of args to SETQ: ~S" source))
2551 (let* ((name (first things))
2552 (leaf (or (lexenv-find name variables)
2553 (find-free-variable name))))
2556 (when (or (constant-p leaf)
2557 (and (global-var-p leaf)
2558 (eq (global-var-kind leaf) :constant)))
2559 (compiler-error "~S is a constant and thus can't be set." name))
2560 (when (and (lambda-var-p leaf)
2561 (lambda-var-ignorep leaf))
2562 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
2563 ;; requires that this be a STYLE-WARNING, not a full warning.
2564 (compiler-style-warning
2565 "~S is being set even though it was declared to be ignored."
2567 (set-variable start cont leaf (second things)))
2569 (aver (eq (car leaf) 'MACRO))
2570 (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
2572 (ir1-convert start cont
2573 `(%set-heap-alien ',leaf ,(second things))))))
2575 (do ((thing things (cddr thing)))
2577 (ir1-convert-progn-body start cont (sets)))
2578 (sets `(setq ,(first thing) ,(second thing))))))))
2580 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
2581 ;;; This should only need to be called in SETQ.
2582 (defun set-variable (start cont var value)
2583 (declare (type continuation start cont) (type basic-var var))
2584 (let ((dest (make-continuation)))
2585 (setf (continuation-asserted-type dest) (leaf-type var))
2586 (ir1-convert start dest value)
2587 (let ((res (make-set :var var :value dest)))
2588 (setf (continuation-dest dest) res)
2589 (setf (leaf-ever-used var) t)
2590 (push res (basic-var-sets var))
2591 (prev-link res dest)
2592 (use-continuation res cont))))
2594 ;;;; CATCH, THROW and UNWIND-PROTECT
2596 ;;; We turn THROW into a multiple-value-call of a magical function,
2597 ;;; since as as far as IR1 is concerned, it has no interesting
2598 ;;; properties other than receiving multiple-values.
2599 (def-ir1-translator throw ((tag result) start cont)
2602 Do a non-local exit, return the values of Form from the CATCH whose tag
2603 evaluates to the same thing as Tag."
2604 (ir1-convert start cont
2605 `(multiple-value-call #'%throw ,tag ,result)))
2607 ;;; This is a special special form used to instantiate a cleanup as
2608 ;;; the current cleanup within the body. KIND is a the kind of cleanup
2609 ;;; to make, and MESS-UP is a form that does the mess-up action. We
2610 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
2611 ;;; and introduce the cleanup into the lexical environment. We
2612 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
2613 ;;; cleanup, since this inner cleanup is the interesting one.
2614 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
2615 (let ((dummy (make-continuation))
2616 (dummy2 (make-continuation)))
2617 (ir1-convert start dummy mess-up)
2618 (let* ((mess-node (continuation-use dummy))
2619 (cleanup (make-cleanup :kind kind
2620 :mess-up mess-node))
2621 (old-cup (lexenv-cleanup *lexenv*))
2622 (*lexenv* (make-lexenv :cleanup cleanup)))
2623 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
2624 (ir1-convert dummy dummy2 '(%cleanup-point))
2625 (ir1-convert-progn-body dummy2 cont body))))
2627 ;;; This is a special special form that makes an "escape function"
2628 ;;; which returns unknown values from named block. We convert the
2629 ;;; function, set its kind to :ESCAPE, and then reference it. The
2630 ;;; :Escape kind indicates that this function's purpose is to
2631 ;;; represent a non-local control transfer, and that it might not
2632 ;;; actually have to be compiled.
2634 ;;; Note that environment analysis replaces references to escape
2635 ;;; functions with references to the corresponding NLX-INFO structure.
2636 (def-ir1-translator %escape-function ((tag) start cont)
2637 (let ((fun (ir1-convert-lambda
2639 (return-from ,tag (%unknown-values))))))
2640 (setf (functional-kind fun) :escape)
2641 (reference-leaf start cont fun)))
2643 ;;; Yet another special special form. This one looks up a local
2644 ;;; function and smashes it to a :CLEANUP function, as well as
2646 (def-ir1-translator %cleanup-function ((name) start cont)
2647 (let ((fun (lexenv-find name functions)))
2648 (aver (lambda-p fun))
2649 (setf (functional-kind fun) :cleanup)
2650 (reference-leaf start cont fun)))
2652 ;;; We represent the possibility of the control transfer by making an
2653 ;;; "escape function" that does a lexical exit, and instantiate the
2654 ;;; cleanup using %WITHIN-CLEANUP.
2655 (def-ir1-translator catch ((tag &body body) start cont)
2658 Evaluates Tag and instantiates it as a catcher while the body forms are
2659 evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
2660 scope of the body, then control will be transferred to the end of the body
2661 and the thrown values will be returned."
2664 (let ((exit-block (gensym "EXIT-BLOCK-")))
2668 (%catch (%escape-function ,exit-block) ,tag)
2671 ;;; UNWIND-PROTECT is similar to CATCH, but more hairy. We make the
2672 ;;; cleanup forms into a local function so that they can be referenced
2673 ;;; both in the case where we are unwound and in any local exits. We
2674 ;;; use %CLEANUP-FUNCTION on this to indicate that reference by
2675 ;;; %UNWIND-PROTECT ISN'T "real", and thus doesn't cause creation of
2677 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
2679 "Unwind-Protect Protected Cleanup*
2680 Evaluate the form Protected, returning its values. The cleanup forms are
2681 evaluated whenever the dynamic scope of the Protected form is exited (either
2682 due to normal completion or a non-local exit such as THROW)."
2685 (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
2686 (drop-thru-tag (gensym "DROP-THRU-TAG-"))
2687 (exit-tag (gensym "EXIT-TAG-"))
2688 (next (gensym "NEXT"))
2689 (start (gensym "START"))
2690 (count (gensym "COUNT")))
2691 `(flet ((,cleanup-fun () ,@cleanup nil))
2692 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
2693 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
2694 ;; and something can be done to make %ESCAPE-FUNCTION have
2695 ;; dynamic extent too.
2696 (block ,drop-thru-tag
2697 (multiple-value-bind (,next ,start ,count)
2701 (%unwind-protect (%escape-function ,exit-tag)
2702 (%cleanup-function ,cleanup-fun))
2703 (return-from ,drop-thru-tag ,protected)))
2705 (%continue-unwind ,next ,start ,count)))))))
2707 ;;;; multiple-value stuff
2709 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
2712 ;;; If there are no arguments, then we convert to a normal
2713 ;;; combination, ensuring that a MV-COMBINATION always has at least
2714 ;;; one argument. This can be regarded as an optimization, but it is
2715 ;;; more important for simplifying compilation of MV-COMBINATIONS.
2716 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
2718 "MULTIPLE-VALUE-CALL Function Values-Form*
2719 Call Function, passing all the values of each Values-Form as arguments,
2720 values from the first Values-Form making up the first argument, etc."
2721 (let* ((fun-cont (make-continuation))
2723 (make-mv-combination fun-cont)
2724 (make-combination fun-cont))))
2725 (ir1-convert start fun-cont
2726 (if (and (consp fun) (eq (car fun) 'function))
2728 `(%coerce-callable-to-function ,fun)))
2729 (setf (continuation-dest fun-cont) node)
2730 (assert-continuation-type fun-cont
2731 (specifier-type '(or function symbol)))
2732 (collect ((arg-conts))
2733 (let ((this-start fun-cont))
2735 (let ((this-cont (make-continuation node)))
2736 (ir1-convert this-start this-cont arg)
2737 (setq this-start this-cont)
2738 (arg-conts this-cont)))
2739 (prev-link node this-start)
2740 (use-continuation node cont)
2741 (setf (basic-combination-args node) (arg-conts))))))
2743 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
2744 ;;; the result code use result continuation (CONT), but transfer
2745 ;;; control to the evaluation of the body. In other words, the result
2746 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
2749 ;;; In order to get the control flow right, we convert the result with
2750 ;;; a dummy result continuation, then convert all the uses of the
2751 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
2752 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
2753 ;;; that they are consistent. Note that this doesn't amount to
2754 ;;; changing the exit target, since the control destination of an exit
2755 ;;; is determined by the block successor; we are just indicating the
2756 ;;; continuation that the result is delivered to.
2758 ;;; We then convert the body, using another dummy continuation in its
2759 ;;; own block as the result. After we are done converting the body, we
2760 ;;; move all predecessors of the dummy end block to CONT's block.
2762 ;;; Note that we both exploit and maintain the invariant that the CONT
2763 ;;; to an IR1 convert method either has no block or starts the block
2764 ;;; that control should transfer to after completion for the form.
2765 ;;; Nested MV-PROG1's work because during conversion of the result
2766 ;;; form, we use dummy continuation whose block is the true control
2768 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
2770 "MULTIPLE-VALUE-PROG1 Values-Form Form*
2771 Evaluate Values-Form and then the Forms, but return all the values of
2773 (continuation-starts-block cont)
2774 (let* ((dummy-result (make-continuation))
2775 (dummy-start (make-continuation))
2776 (cont-block (continuation-block cont)))
2777 (continuation-starts-block dummy-start)
2778 (ir1-convert start dummy-start result)
2780 (substitute-continuation-uses cont dummy-start)
2782 (continuation-starts-block dummy-result)
2783 (ir1-convert-progn-body dummy-start dummy-result forms)
2784 (let ((end-block (continuation-block dummy-result)))
2785 (dolist (pred (block-pred end-block))
2786 (unlink-blocks pred end-block)
2787 (link-blocks pred cont-block))
2788 (aver (not (continuation-dest dummy-result)))
2789 (delete-continuation dummy-result)
2790 (remove-from-dfo end-block))))
2792 ;;;; interface to defining macros
2795 ;;;; classic CMU CL comment:
2796 ;;;; DEFMACRO and DEFUN expand into calls to %DEFxxx functions
2797 ;;;; so that we get a chance to see what is going on. We define
2798 ;;;; IR1 translators for these functions which look at the
2799 ;;;; definition and then generate a call to the %%DEFxxx function.
2800 ;;;; Alas, this implementation doesn't do the right thing for
2801 ;;;; non-toplevel uses of these forms, so this should probably
2802 ;;;; be changed to use EVAL-WHEN instead.
2804 ;;; Return a new source path with any stuff intervening between the
2805 ;;; current path and the first form beginning with NAME stripped off.
2806 ;;; This is used to hide the guts of DEFmumble macros to prevent
2807 ;;; annoying error messages.
2808 (defun revert-source-path (name)
2809 (do ((path *current-path* (cdr path)))
2810 ((null path) *current-path*)
2811 (let ((first (first path)))
2812 (when (or (eq first name)
2813 (eq first 'original-source-start))
2816 ;;; Warn about incompatible or illegal definitions and add the macro
2817 ;;; to the compiler environment.
2819 ;;; Someday we could check for macro arguments being incompatibly
2820 ;;; redefined. Doing this right will involve finding the old macro
2821 ;;; lambda-list and comparing it with the new one.
2822 (def-ir1-translator %defmacro ((qname qdef lambda-list doc) start cont
2824 (let (;; QNAME is typically a quoted name. I think the idea is to let
2825 ;; %DEFMACRO work as an ordinary function when interpreting. Whatever
2826 ;; the reason it's there, we don't want it any more. -- WHN 19990603
2828 ;; QDEF should be a sharp-quoted definition. We don't want to make a
2829 ;; function of it just yet, so we just drop the sharp-quote.
2831 (aver (eq 'function (first qdef)))
2832 (aver (proper-list-of-length-p qdef 2))
2835 (/show "doing IR1 translator for %DEFMACRO" name)
2837 (unless (symbolp name)
2838 (compiler-error "The macro name ~S is not a symbol." name))
2840 (ecase (info :function :kind name)
2843 (remhash name *free-functions*)
2844 (undefine-function-name name)
2846 "~S is being redefined as a macro when it was ~
2847 previously ~(~A~) to be a function."
2849 (info :function :where-from name)))
2852 (compiler-error "The special form ~S can't be redefined as a macro."
2855 (setf (info :function :kind name) :macro
2856 (info :function :where-from name) :defined
2857 (info :function :macro-function name) (coerce def 'function))
2859 (let* ((*current-path* (revert-source-path 'defmacro))
2860 (fun (ir1-convert-lambda def name)))
2861 (setf (leaf-name fun)
2862 (concatenate 'string "DEFMACRO " (symbol-name name)))
2863 (setf (functional-arg-documentation fun) (eval lambda-list))
2865 (ir1-convert start cont `(%%defmacro ',name ,fun ,doc)))
2867 (when sb!xc:*compile-print*
2868 ;; FIXME: It would be nice to convert this, and the other places
2869 ;; which create compiler diagnostic output prefixed by
2870 ;; semicolons, to use some common utility which automatically
2871 ;; prefixes all its output with semicolons. (The addition of
2872 ;; semicolon prefixes was introduced ca. sbcl-0.6.8.10 as the
2873 ;; "MNA compiler message patch", and implemented by modifying a
2874 ;; bunch of output statements on a case-by-case basis, which
2875 ;; seems unnecessarily error-prone and unclear, scattering
2876 ;; implicit information about output style throughout the
2877 ;; system.) Starting by rewriting COMPILER-MUMBLE to add
2878 ;; semicolon prefixes would be a good start, and perhaps also:
2879 ;; * Add semicolon prefixes for "FOO assembled" messages emitted
2880 ;; when e.g. src/assembly/x86/assem-rtns.lisp is processed.
2881 ;; * At least some debugger output messages deserve semicolon
2883 ;; ** restarts table
2884 ;; ** "Within the debugger, you can type HELP for help."
2885 (compiler-mumble "~&; converted ~S~%" name))))
2887 (def-ir1-translator %define-compiler-macro ((name def lambda-list doc)
2890 (let ((name (eval name))
2891 (def (second def))) ; We don't want to make a function just yet...
2893 (when (eq (info :function :kind name) :special-form)
2894 (compiler-error "attempt to define a compiler-macro for special form ~S"
2897 (setf (info :function :compiler-macro-function name)
2898 (coerce def 'function))
2900 (let* ((*current-path* (revert-source-path 'define-compiler-macro))
2901 (fun (ir1-convert-lambda def name)))
2902 (setf (leaf-name fun)
2903 (let ((*print-case* :upcase))
2904 (format nil "DEFINE-COMPILER-MACRO ~S" name)))
2905 (setf (functional-arg-documentation fun) (eval lambda-list))
2907 (ir1-convert start cont `(%%define-compiler-macro ',name ,fun ,doc)))
2909 (when sb!xc:*compile-print*
2910 (compiler-mumble "~&; converted ~S~%" name))))
2912 ;;;; defining global functions
2914 ;;; Convert FUN as a lambda in the null environment, but use the
2915 ;;; current compilation policy. Note that FUN may be a
2916 ;;; LAMBDA-WITH-ENVIRONMENT, so we may have to augment the environment
2917 ;;; to reflect the state at the definition site.
2918 (defun ir1-convert-inline-lambda (fun &optional name)
2919 (destructuring-bind (decls macros symbol-macros &rest body)
2920 (if (eq (car fun) 'lambda-with-environment)
2922 `(() () () . ,(cdr fun)))
2923 (let ((*lexenv* (make-lexenv
2924 :default (process-decls decls nil nil
2927 :variables (copy-list symbol-macros)
2931 (macro . ,(coerce (cdr x) 'function))))
2933 :policy (lexenv-policy *lexenv*))))
2934 (ir1-convert-lambda `(lambda ,@body) name))))
2936 ;;; Return a lambda that has been "closed" with respect to ENV,
2937 ;;; returning a LAMBDA-WITH-ENVIRONMENT if there are interesting
2938 ;;; macros or declarations. If there is something too complex (like a
2939 ;;; lexical variable) in the environment, then we return NIL.
2940 (defun inline-syntactic-closure-lambda (lambda &optional (env *lexenv*))
2941 (let ((variables (lexenv-variables env))
2942 (functions (lexenv-functions env))
2946 (cond ((or (lexenv-blocks env) (lexenv-tags env)) nil)
2947 ((and (null variables) (null functions))
2949 ((dolist (x variables nil)
2950 (let ((name (car x))
2952 (when (eq x (assoc name variables :test #'eq))
2955 (aver (eq (car what) 'macro))
2958 (aver (eq (global-var-kind what) :special))
2959 (push `(special ,name) decls))
2962 ((dolist (x functions nil)
2963 (let ((name (car x))
2965 (when (eq x (assoc name functions :test #'equal))
2969 (function-lambda-expression (cdr what)))
2972 (when (defined-function-p what)
2973 (push `(,(car (rassoc (defined-function-inlinep what)
2974 *inlinep-translations*))
2980 `(lambda-with-environment ,decls
2983 . ,(rest lambda))))))
2985 ;;; Get a DEFINED-FUNCTION object for a function we are about to
2986 ;;; define. If the function has been forward referenced, then
2987 ;;; substitute for the previous references.
2988 (defun get-defined-function (name)
2989 (let* ((name (proclaim-as-function-name name))
2990 (found (find-free-function name "Eh?")))
2991 (note-name-defined name :function)
2992 (cond ((not (defined-function-p found))
2993 (aver (not (info :function :inlinep name)))
2994 (let* ((where-from (leaf-where-from found))
2995 (res (make-defined-function
2997 :where-from (if (eq where-from :declared)
2999 :type (leaf-type found))))
3000 (substitute-leaf res found)
3001 (setf (gethash name *free-functions*) res)))
3002 ;; If *FREE-FUNCTIONS* has a previously converted definition for this
3003 ;; name, then blow it away and try again.
3004 ((defined-function-functional found)
3005 (remhash name *free-functions*)
3006 (get-defined-function name))
3009 ;;; Check a new global function definition for consistency with
3010 ;;; previous declaration or definition, and assert argument/result
3011 ;;; types if appropriate. This assertion is suppressed by the
3012 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
3013 ;;; check their argument types as a consequence of type dispatching.
3014 ;;; This avoids redundant checks such as NUMBERP on the args to +,
3016 (defun assert-new-definition (var fun)
3017 (let ((type (leaf-type var))
3018 (for-real (eq (leaf-where-from var) :declared))
3019 (info (info :function :info (leaf-name var))))
3020 (assert-definition-type
3022 ;; KLUDGE: Common Lisp is such a dynamic language that in general
3023 ;; all we can do here in general is issue a STYLE-WARNING. It
3024 ;; would be nice to issue a full WARNING in the special case of
3025 ;; of type mismatches within a compilation unit (as in section
3026 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
3027 ;; keep track of whether the mismatched data came from the same
3028 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
3029 :error-function #'compiler-style-warning
3030 :warning-function (cond (info #'compiler-style-warning)
3031 (for-real #'compiler-note)
3036 (ir1-attributep (function-info-attributes info)
3039 "previous declaration"
3040 "previous definition"))))
3042 ;;; Convert a lambda doing all the basic stuff we would do if we were
3043 ;;; converting a DEFUN. This is used both by the %DEFUN translator and
3044 ;;; for global inline expansion.
3046 ;;; Unless a :INLINE function, we temporarily clobber the inline
3047 ;;; expansion. This prevents recursive inline expansion of
3048 ;;; opportunistic pseudo-inlines.
3049 (defun ir1-convert-lambda-for-defun (lambda var expansion converter)
3050 (declare (cons lambda) (function converter) (type defined-function var))
3051 (let ((var-expansion (defined-function-inline-expansion var)))
3052 (unless (eq (defined-function-inlinep var) :inline)
3053 (setf (defined-function-inline-expansion var) nil))
3054 (let* ((name (leaf-name var))
3055 (fun (funcall converter lambda name))
3056 (function-info (info :function :info name)))
3057 (setf (functional-inlinep fun) (defined-function-inlinep var))
3058 (assert-new-definition var fun)
3059 (setf (defined-function-inline-expansion var) var-expansion)
3060 ;; If definitely not an interpreter stub, then substitute for any
3062 (unless (or (eq (defined-function-inlinep var) :notinline)
3063 (not *block-compile*)
3065 (or (function-info-transforms function-info)
3066 (function-info-templates function-info)
3067 (function-info-ir2-convert function-info))))
3068 (substitute-leaf fun var)
3069 ;; If in a simple environment, then we can allow backward
3070 ;; references to this function from following top-level forms.
3071 (when expansion (setf (defined-function-functional var) fun)))
3074 ;;; Convert the definition and install it in the global environment
3075 ;;; with a LABELS-like effect. If the lexical environment is not null,
3076 ;;; then we only install the definition during the processing of this
3077 ;;; DEFUN, ensuring that the function cannot be called outside of the
3078 ;;; correct environment. If the function is globally NOTINLINE, then
3079 ;;; that inhibits even local substitution. Also, emit top-level code
3080 ;;; to install the definition.
3082 ;;; This is one of the major places where the semantics of block
3083 ;;; compilation is handled. Substitution for global names is totally
3084 ;;; inhibited if *BLOCK-COMPILE* is NIL. And if *BLOCK-COMPILE* is
3085 ;;; true and entry points are specified, then we don't install global
3086 ;;; definitions for non-entry functions (effectively turning them into
3087 ;;; local lexical functions.)
3088 (def-ir1-translator %defun ((name def doc source) start cont
3090 (declare (ignore source))
3091 (let* ((name (eval name))
3092 (lambda (second def))
3093 (*current-path* (revert-source-path 'defun))
3094 (expansion (unless (eq (info :function :inlinep name) :notinline)
3095 (inline-syntactic-closure-lambda lambda))))
3096 ;; If not in a simple environment or NOTINLINE, then discard any
3097 ;; forward references to this function.
3098 (unless expansion (remhash name *free-functions*))
3100 (let* ((var (get-defined-function name))
3101 (save-expansion (and (member (defined-function-inlinep var)
3102 '(:inline :maybe-inline))
3104 (setf (defined-function-inline-expansion var) expansion)
3105 (setf (info :function :inline-expansion name) save-expansion)
3106 ;; If there is a type from a previous definition, blast it,
3107 ;; since it is obsolete.
3108 (when (eq (leaf-where-from var) :defined)
3109 (setf (leaf-type var) (specifier-type 'function)))
3111 (let ((fun (ir1-convert-lambda-for-defun lambda
3114 #'ir1-convert-lambda)))
3117 (if (and *block-compile* *entry-points*
3118 (not (member name *entry-points* :test #'equal)))
3120 `(%%defun ',name ,fun ,doc
3121 ,@(when save-expansion `(',save-expansion)))))
3123 (when sb!xc:*compile-print*
3124 (compiler-mumble "~&; converted ~S~%" name))))))