1 ;;;; miscellaneous types and macros used in writing the compiler
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 (declaim (special *wild-type* *universal-type* *compiler-error-context*))
16 ;;; An INLINEP value describes how a function is called. The values
17 ;;; have these meanings:
18 ;;; NIL No declaration seen: do whatever you feel like, but don't
19 ;;; dump an inline expansion.
20 ;;; :NOTINLINE NOTINLINE declaration seen: always do full function call.
21 ;;; :INLINE INLINE declaration seen: save expansion, expanding to it
24 ;;; Retain expansion, but only use it opportunistically.
25 (deftype inlinep () '(member :inline :maybe-inline :notinline nil))
29 ;;; a helper function for the POLICY macro: Return a list of
30 ;;; POLICY-QUALITY-SLOT objects corresponding to the qualities which
32 (eval-when (:compile-toplevel :load-toplevel :execute)
33 (defun policy-quality-slots-used-by (expr)
38 (let ((pqs (named-policy-quality-slot x)))
40 (pushnew pqs result))))))
44 ;;; syntactic sugar for querying optimization policy qualities
46 ;;; Evaluate EXPR in terms of the current optimization policy for
47 ;;; NODE, or if NODE is NIL, in terms of the current policy as defined
48 ;;; by *DEFAULT-POLICY* and *CURRENT-POLICY*. (Using NODE=NIL is only
49 ;;; well-defined during IR1 conversion.)
51 ;;; EXPR is a form which accesses the policy values by referring to
52 ;;; them by name, e.g. SPEED.
53 (defmacro policy (node expr)
54 (let* ((n-policy (gensym))
57 `(,(policy-quality-slot-quality pqs)
58 (,(policy-quality-slot-accessor pqs) ,n-policy)))
59 (policy-quality-slots-used-by expr))))
60 (/show "in POLICY" expr binds)
61 `(let* ((,n-policy (lexenv-policy
68 ;;;; source-hacking defining forms
70 ;;; to be passed to PARSE-DEFMACRO when we want compiler errors
71 ;;; instead of real errors
72 #!-sb-fluid (declaim (inline convert-condition-into-compiler-error))
73 (defun convert-condition-into-compiler-error (datum &rest stuff)
75 (apply #'compiler-error datum stuff)
78 (apply #'make-condition datum stuff)
81 ;;; Parse a DEFMACRO-style lambda-list, setting things up so that a
82 ;;; compiler error happens if the syntax is invalid.
84 ;;; Define a function that converts a special form or other magical
85 ;;; thing into IR1. LAMBDA-LIST is a defmacro style lambda list.
86 ;;; START-VAR and CONT-VAR are bound to the start and result
87 ;;; continuations for the resulting IR1. KIND is the function kind to
88 ;;; associate with NAME.
89 (defmacro def-ir1-translator (name (lambda-list start-var cont-var
90 &key (kind :special-form))
92 (let ((fn-name (symbolicate "IR1-CONVERT-" name))
95 (multiple-value-bind (body decls doc)
96 (parse-defmacro lambda-list n-form body name "special form"
98 :error-fun 'convert-condition-into-compiler-error)
100 (declaim (ftype (function (continuation continuation t) (values))
102 (defun ,fn-name (,start-var ,cont-var ,n-form)
103 (let ((,n-env *lexenv*))
108 `((setf (fdocumentation ',name 'function) ,doc)))
109 ;; FIXME: Evidently "there can only be one!" -- we overwrite any
110 ;; other :IR1-CONVERT value. This deserves a warning, I think.
111 (setf (info :function :ir1-convert ',name) #',fn-name)
112 (setf (info :function :kind ',name) ,kind)
113 ;; It's nice to do this for error checking in the target
114 ;; SBCL, but it's not nice to do this when we're running in
115 ;; the cross-compilation host Lisp, which owns the
116 ;; SYMBOL-FUNCTION of its COMMON-LISP symbols.
118 ,@(when (eq kind :special-form)
119 `((setf (symbol-function ',name)
121 (declare (ignore rest))
122 (error "can't FUNCALL the SYMBOL-FUNCTION of ~
123 special forms")))))))))
125 ;;; (This is similar to DEF-IR1-TRANSLATOR, except that we pass if the
126 ;;; syntax is invalid.)
128 ;;; Define a macro-like source-to-source transformation for the
129 ;;; function NAME. A source transform may "pass" by returning a
130 ;;; non-nil second value. If the transform passes, then the form is
131 ;;; converted as a normal function call. If the supplied arguments are
132 ;;; not compatible with the specified LAMBDA-LIST, then the transform
133 ;;; automatically passes.
135 ;;; Source transforms may only be defined for functions. Source
136 ;;; transformation is not attempted if the function is declared
137 ;;; NOTINLINE. Source transforms should not examine their arguments.
138 ;;; If it matters how the function is used, then DEFTRANSFORM should
139 ;;; be used to define an IR1 transformation.
141 ;;; If the desirability of the transformation depends on the current
142 ;;; OPTIMIZE parameters, then the POLICY macro should be used to
143 ;;; determine when to pass.
144 (defmacro def-source-transform (name lambda-list &body body)
151 (apply #'symbolicate "SOURCE-TRANSFORM" (pieces)))
152 (symbolicate "SOURCE-TRANSFORM-" name)))
155 (multiple-value-bind (body decls)
156 (parse-defmacro lambda-list n-form body name "form"
158 :error-fun `(lambda (&rest stuff)
159 (declare (ignore stuff))
160 (return-from ,fn-name
163 (defun ,fn-name (,n-form)
164 (let ((,n-env *lexenv*))
167 (setf (info :function :source-transform ',name) #',fn-name)))))
169 ;;; Define a function that converts a use of (%PRIMITIVE NAME ..)
170 ;;; into Lisp code. LAMBDA-LIST is a DEFMACRO-style lambda list.
171 (defmacro def-primitive-translator (name lambda-list &body body)
172 (let ((fn-name (symbolicate "PRIMITIVE-TRANSLATE-" name))
175 (multiple-value-bind (body decls)
176 (parse-defmacro lambda-list n-form body name "%primitive"
178 :error-fun 'convert-condition-into-compiler-error)
180 (defun ,fn-name (,n-form)
181 (let ((,n-env *lexenv*))
184 (setf (gethash ',name *primitive-translators*) ',fn-name)))))
186 ;;;; boolean attribute utilities
188 ;;;; We need to maintain various sets of boolean attributes for known
189 ;;;; functions and VOPs. To save space and allow for quick set
190 ;;;; operations, we represent the attributes as bits in a fixnum.
192 (deftype attributes () 'fixnum)
194 (eval-when (:compile-toplevel :load-toplevel :execute)
196 ;;; Given a list of attribute names and an alist that translates them
197 ;;; to masks, return the OR of the masks.
198 (defun compute-attribute-mask (names alist)
199 (collect ((res 0 logior))
201 (let ((mask (cdr (assoc name alist))))
203 (error "unknown attribute name: ~S" name))
209 ;;; Parse the specification and generate some accessor macros.
211 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
212 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
214 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
215 ;;; arrangement, in order to get it to work in cross-compilation. This
216 ;;; duplication should be removed, perhaps by rewriting the macro in a
217 ;;; more cross-compiler-friendly way, or perhaps just by using some
218 ;;; (MACROLET ((FROB ..)) .. FROB .. FROB) form, but I don't want to
219 ;;; do it now, because the system isn't running yet, so it'd be too
220 ;;; hard to check that my changes were correct -- WHN 19990806
221 (def!macro def-boolean-attribute (name &rest attribute-names)
223 "Def-Boolean-Attribute Name Attribute-Name*
224 Define a new class of boolean attributes, with the attributes having the
225 specified Attribute-Names. Name is the name of the class, which is used to
226 generate some macros to manipulate sets of the attributes:
228 NAME-attributep attributes attribute-name*
229 Return true if one of the named attributes is present, false otherwise.
230 When set with SETF, updates the place Attributes setting or clearing the
231 specified attributes.
233 NAME-attributes attribute-name*
234 Return a set of the named attributes."
236 (let ((translations-name (symbolicate "*" name "-ATTRIBUTE-TRANSLATIONS*"))
237 (test-name (symbolicate name "-ATTRIBUTEP")))
239 (do ((mask 1 (ash mask 1))
240 (names attribute-names (cdr names)))
242 (alist (cons (car names) mask)))
246 (eval-when (:compile-toplevel :load-toplevel :execute)
247 (defparameter ,translations-name ',(alist)))
249 (defmacro ,test-name (attributes &rest attribute-names)
250 "Automagically generated boolean attribute test function. See
251 Def-Boolean-Attribute."
252 `(logtest ,(compute-attribute-mask attribute-names
254 (the attributes ,attributes)))
256 (define-setf-expander ,test-name (place &rest attributes
258 "Automagically generated boolean attribute setter. See
259 Def-Boolean-Attribute."
260 #-sb-xc-host (declare (type sb!c::lexenv env))
261 ;; FIXME: It would be better if &ENVIRONMENT arguments
262 ;; were automatically declared to have type LEXENV by the
263 ;; hairy-argument-handling code.
264 (multiple-value-bind (temps values stores set get)
265 (get-setf-expansion place env)
267 (error "multiple store variables for ~S" place))
268 (let ((newval (gensym))
270 (mask (compute-attribute-mask attributes
271 ,translations-name)))
272 (values `(,@temps ,n-place)
275 `(let ((,(first stores)
277 (logior ,n-place ,mask)
278 (logand ,n-place ,(lognot mask)))))
281 `(,',test-name ,n-place ,@attributes)))))
283 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
284 "Automagically generated boolean attribute creation function. See
285 Def-Boolean-Attribute."
286 (compute-attribute-mask attribute-names ,translations-name))))))
287 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
289 ;;; And now for some gratuitous pseudo-abstraction...
290 (defmacro attributes-union (&rest attributes)
292 "Returns the union of all the sets of boolean attributes which are its
295 (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
296 (defmacro attributes-intersection (&rest attributes)
298 "Returns the intersection of all the sets of boolean attributes which are its
301 (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
302 (declaim (ftype (function (attributes attributes) boolean) attributes=))
303 #!-sb-fluid (declaim (inline attributes=))
304 (defun attributes= (attr1 attr2)
306 "Returns true if the attributes present in Attr1 are identical to those in
310 ;;;; lambda-list parsing utilities
312 ;;;; IR1 transforms, optimizers and type inferencers need to be able
313 ;;;; to parse the IR1 representation of a function call using a
314 ;;;; standard function lambda-list.
316 (eval-when (:compile-toplevel :load-toplevel :execute)
318 ;;; Given a DEFTRANSFORM-style lambda-list, generate code that parses
319 ;;; the arguments of a combination with respect to that lambda-list.
320 ;;; BODY is the the list of forms which are to be evaluated within the
321 ;;; bindings. ARGS is the variable that holds list of argument
322 ;;; continuations. ERROR-FORM is a form which is evaluated when the
323 ;;; syntax of the supplied arguments is incorrect or a non-constant
324 ;;; argument keyword is supplied. Defaults and other gunk are ignored.
325 ;;; The second value is a list of all the arguments bound. We make the
326 ;;; variables IGNORABLE so that we don't have to manually declare them
327 ;;; Ignore if their only purpose is to make the syntax work.
328 (declaim (ftype (function (list list symbol t) list) parse-deftransform))
329 (defun parse-deftransform (lambda-list body args error-form)
330 (multiple-value-bind (req opt restp rest keyp keys allowp)
331 (parse-lambda-list lambda-list)
332 (let* ((min-args (length req))
333 (max-args (+ min-args (length opt)))
341 (binds `(,arg (nth ,(pos) ,args)))
345 (let ((var (if (atom arg) arg (first arg))))
347 (binds `(,var (nth ,(pos) ,args)))
352 (binds `(,rest (nthcdr ,(pos) ,args))))
355 (if (or (atom spec) (atom (first spec)))
356 (let* ((var (if (atom spec) spec (first spec)))
357 (key (intern (symbol-name var) "KEYWORD")))
359 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
361 (let* ((head (first spec))
365 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
368 (let ((n-length (gensym))
369 (limited-legal (not (or restp keyp))))
371 `(let ((,n-length (length ,args))
372 ,@(when keyp `((,n-keys (nthcdr ,(pos) ,args)))))
374 ;; FIXME: should be PROPER-LIST-OF-LENGTH-P
376 `(<= ,min-args ,n-length ,max-args)
377 `(<= ,min-args ,n-length))
380 `((check-keywords-constant ,n-keys))
381 `((check-transform-keys ,n-keys ',(keywords))))))
384 (declare (ignorable ,@(vars)))
392 ;;; Parse the lambda-list and generate code to test the policy and
393 ;;; automatically create the result lambda.
394 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
396 &key result policy node defun-only
397 eval-name important (when :native))
398 &body body-decls-doc)
400 "Deftransform Name (Lambda-List [Arg-Types] [Result-Type] {Key Value}*)
401 Declaration* [Doc-String] Form*
402 Define an IR1 transformation for NAME. An IR1 transformation computes a
403 lambda that replaces the function variable reference for the call. A
404 transform may pass (decide not to transform the call) by calling the
405 GIVE-UP-IR1-TRANSFORM function. LAMBDA-LIST both determines how the
406 current call is parsed and specifies the LAMBDA-LIST for the resulting
409 We parse the call and bind each of the lambda-list variables to the
410 continuation which represents the value of the argument. When parsing
411 the call, we ignore the defaults, and always bind the variables for
412 unsupplied arguments to NIL. If a required argument is missing, an
413 unknown keyword is supplied, or an argument keyword is not a constant,
414 then the transform automatically passes. The DECLARATIONS apply to the
415 bindings made by DEFTRANSFORM at transformation time, rather than to
416 the variables of the resulting lambda. Bound-but-not-referenced
417 warnings are suppressed for the lambda-list variables. The DOC-STRING
418 is used when printing efficiency notes about the defined transform.
420 Normally, the body evaluates to a form which becomes the body of an
421 automatically constructed lambda. We make LAMBDA-LIST the lambda-list
422 for the lambda, and automatically insert declarations of the argument
423 and result types. If the second value of the body is non-null, then it
424 is a list of declarations which are to be inserted at the head of the
425 lambda. Automatic lambda generation may be inhibited by explicitly
426 returning a lambda from the body.
428 The ARG-TYPES and RESULT-TYPE are used to create a function type
429 which the call must satisfy before transformation is attempted. The
430 function type specifier is constructed by wrapping (FUNCTION ...)
431 around these values, so the lack of a restriction may be specified by
432 omitting the argument or supplying *. The argument syntax specified in
433 the ARG-TYPES need not be the same as that in the LAMBDA-LIST, but the
434 transform will never happen if the syntaxes can't be satisfied
435 simultaneously. If there is an existing transform for the same
436 function that has the same type, then it is replaced with the new
439 These are the legal keyword options:
440 :Result - A variable which is bound to the result continuation.
441 :Node - A variable which is bound to the combination node for the call.
442 :Policy - A form which is supplied to the POLICY macro to determine whether
443 this transformation is appropriate. If the result is false, then
444 the transform automatically passes.
446 - The name and argument/result types are actually forms to be
447 evaluated. Useful for getting closures that transform similar
450 - Don't actually instantiate a transform, instead just DEFUN
451 Name with the specified transform definition function. This may
452 be later instantiated with %DEFTRANSFORM.
454 - If supplied and non-NIL, note this transform as ``important,''
455 which means efficiency notes will be generated when this
456 transform fails even if brevity=speed (but not if brevity>speed)
457 :When {:Native | :Byte | :Both}
458 - Indicates whether this transform applies to native code,
459 byte-code or both (default :native.)"
461 (when (and eval-name defun-only)
462 (error "can't specify both DEFUN-ONLY and EVAL-NAME"))
463 (multiple-value-bind (body decls doc) (parse-body body-decls-doc)
464 (let ((n-args (gensym))
465 (n-node (or node (gensym)))
468 (decls-body `(,@decls ,@body)))
469 (multiple-value-bind (parsed-form vars)
470 (parse-deftransform lambda-list
472 `((unless (policy ,n-node ,policy)
473 (give-up-ir1-transform))
477 '(give-up-ir1-transform))
480 (let* ((,n-args (basic-combination-args ,n-node))
482 `((,result (node-cont ,n-node)))))
483 (multiple-value-bind (,n-lambda ,n-decls)
485 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
487 `(lambda ,',lambda-list
488 (declare (ignorable ,@',vars))
492 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
494 ,(if eval-name name `',name)
496 ``(function ,,arg-types ,,result-type)
497 `'(function ,arg-types ,result-type))
500 ,(if important t nil)
503 ;;;; DEFKNOWN and DEFOPTIMIZER
505 ;;; This macro should be the way that all implementation independent
506 ;;; information about functions is made known to the compiler.
508 ;;; FIXME: The comment above suggests that perhaps some of my added
509 ;;; FTYPE declarations are in poor taste. Should I change my
510 ;;; declarations, or change the comment, or what?
512 ;;; FIXME: DEFKNOWN is needed only at build-the-system time. Figure
513 ;;; out some way to keep it from appearing in the target system.
514 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
517 "Defknown Name Arg-Types Result-Type [Attributes] {Key Value}*
518 Declare the function Name to be a known function. We construct a type
519 specifier for the function by wrapping (FUNCTION ...) around the Arg-Types
520 and Result-Type. Attributes is an unevaluated list of boolean
521 attributes of the function. These attributes are meaningful here:
523 May call functions that are passed as arguments. In order
524 to determine what other effects are present, we must find
525 the effects of all arguments that may be functions.
528 May incorporate arguments in the result or somehow pass
532 May fail to return during correct execution. Errors
536 The (default) worst case. Includes all the other bad
537 things, plus any other possible bad thing.
540 May be constant-folded. The function has no side effects,
541 but may be affected by side effects on the arguments. E.g.
545 May be eliminated if value is unused. The function has
546 no side effects except possibly CONS. If a function is
547 defined to signal errors, then it is not flushable even
548 if it is movable or foldable.
551 May be moved with impunity. Has no side effects except
552 possibly CONS,and is affected only by its arguments.
555 A true predicate likely to be open-coded. This is a
556 hint to IR1 conversion that it should ensure calls always
557 appear as an IF test. Not usually specified to Defknown,
558 since this is implementation dependent, and is usually
559 automatically set by the Define-VOP :Conditional option.
561 Name may also be a list of names, in which case the same information
562 is given to all the names. The keywords specify the initial values
563 for various optimizers that the function might have."
564 (when (and (intersection attributes '(any call unwind))
565 (intersection attributes '(movable)))
566 (error "function cannot have both good and bad attributes: ~S" attributes))
568 `(%defknown ',(if (and (consp name)
569 (not (eq (car name) 'setf)))
572 '(function ,arg-types ,result-type)
573 (ir1-attributes ,@(if (member 'any attributes)
574 (union '(call unsafe unwind) attributes)
578 ;;; Create a function which parses combination args according to
579 ;;; LAMBDA-LIST, optionally storing it in a FUNCTION-INFO slot.
580 (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
584 "Defoptimizer (Function Kind) (Lambda-List [Node-Var] Var*)
586 Define some Kind of optimizer for the named Function. Function must be a
587 known function. Lambda-List is used to parse the arguments to the
588 combination as in Deftransform. If the argument syntax is invalid or there
589 are non-constant keys, then we simply return NIL.
591 The function is DEFUN'ed as Function-Kind-OPTIMIZER. Possible kinds are
592 DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If a symbol is
593 specified instead of a (Function Kind) list, then we just do a DEFUN with the
594 symbol as its name, and don't do anything with the definition. This is
595 useful for creating optimizers to be passed by name to DEFKNOWN.
597 If supplied, Node-Var is bound to the combination node being optimized. If
598 additional Vars are supplied, then they are used as the rest of the optimizer
599 function's lambda-list. LTN-ANNOTATE methods are passed an additional POLICY
600 argument, and IR2-CONVERT methods are passed an additional IR2-BLOCK
603 (let ((name (if (symbolp what) what
604 (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
606 (let ((n-args (gensym)))
608 (defun ,name (,n-node ,@vars)
609 (let ((,n-args (basic-combination-args ,n-node)))
610 ,(parse-deftransform lambda-list body n-args
611 `(return-from ,name nil))))
613 `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
614 (function-info-or-lose ',(first what)))
617 ;;;; IR groveling macros
619 (defmacro do-blocks ((block-var component &optional ends result) &body body)
621 "Do-Blocks (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
622 Iterate over the blocks in a component, binding Block-Var to each block in
623 turn. The value of Ends determines whether to iterate over dummy head and
625 NIL -- Skip Head and Tail (the default)
626 :Head -- Do head but skip tail
627 :Tail -- Do tail but skip head
628 :Both -- Do both head and tail
630 If supplied, Result-Form is the value to return."
631 (unless (member ends '(nil :head :tail :both))
632 (error "losing ENDS value: ~S" ends))
633 (let ((n-component (gensym))
635 `(let* ((,n-component ,component)
636 (,n-tail ,(if (member ends '(:both :tail))
638 `(component-tail ,n-component))))
639 (do ((,block-var ,(if (member ends '(:both :head))
640 `(component-head ,n-component)
641 `(block-next (component-head ,n-component)))
642 (block-next ,block-var)))
643 ((eq ,block-var ,n-tail) ,result)
645 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
647 "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
648 Like Do-Blocks, only iterate over the blocks in reverse order."
649 (unless (member ends '(nil :head :tail :both))
650 (error "losing ENDS value: ~S" ends))
651 (let ((n-component (gensym))
653 `(let* ((,n-component ,component)
654 (,n-head ,(if (member ends '(:both :head))
656 `(component-head ,n-component))))
657 (do ((,block-var ,(if (member ends '(:both :tail))
658 `(component-tail ,n-component)
659 `(block-prev (component-tail ,n-component)))
660 (block-prev ,block-var)))
661 ((eq ,block-var ,n-head) ,result)
664 ;;; Could change it not to replicate the code someday perhaps...
665 (defmacro do-uses ((node-var continuation &optional result) &body body)
667 "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
668 Iterate over the uses of Continuation, binding Node to each one
670 (once-only ((n-cont continuation))
671 `(ecase (continuation-kind ,n-cont)
675 (let ((,node-var (continuation-use ,n-cont)))
678 ((:block-start :deleted-block-start)
679 (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
683 ;;; In the forward case, we terminate on Last-Cont so that we don't
684 ;;; have to worry about our termination condition being changed when
685 ;;; new code is added during the iteration. In the backward case, we
686 ;;; do NODE-PREV before evaluating the body so that we can keep going
687 ;;; when the current node is deleted.
689 ;;; When RESTART-P is supplied to DO-NODES, we start iterating over
690 ;;; again at the beginning of the block when we run into a
691 ;;; continuation whose block differs from the one we are trying to
692 ;;; iterate over, either beacuse the block was split, or because a
693 ;;; node was deleted out from under us (hence its block is NIL.) If
694 ;;; the block start is deleted, we just punt. With RESTART-P, we are
695 ;;; also more careful about termination, re-indirecting the BLOCK-LAST
697 (defmacro do-nodes ((node-var cont-var block &key restart-p) &body body)
699 "Do-Nodes (Node-Var Cont-Var Block {Key Value}*) {Declaration}* {Form}*
700 Iterate over the nodes in Block, binding Node-Var to the each node and
701 Cont-Var to the node's Cont. The only keyword option is Restart-P, which
702 causes iteration to be restarted when a node is deleted out from under us (if
703 not supplied, this is an error.)"
704 (let ((n-block (gensym))
705 (n-last-cont (gensym)))
706 `(let* ((,n-block ,block)
708 `((,n-last-cont (node-cont (block-last ,n-block))))))
709 (do* ((,node-var (continuation-next (block-start ,n-block))
712 ((eq (continuation-block ,cont-var) ,n-block)
713 (assert (continuation-next ,cont-var))
714 (continuation-next ,cont-var))
716 (let ((start (block-start ,n-block)))
717 (unless (eq (continuation-kind start)
720 (continuation-next start))))
721 `(continuation-next ,cont-var)))
722 (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
726 `(eq ,node-var (block-last ,n-block))
727 `(eq ,cont-var ,n-last-cont))
729 (defmacro do-nodes-backwards ((node-var cont-var block) &body body)
731 "Do-Nodes-Backwards (Node-Var Cont-Var Block) {Declaration}* {Form}*
732 Like Do-Nodes, only iterates in reverse order."
733 (let ((n-block (gensym))
737 `(let* ((,n-block ,block)
738 (,n-start (block-start ,n-block))
739 (,n-last (block-last ,n-block)))
740 (do* ((,cont-var (node-cont ,n-last) ,n-next)
741 (,node-var ,n-last (continuation-use ,cont-var))
742 (,n-next (node-prev ,node-var) (node-prev ,node-var)))
745 (when (eq ,n-next ,n-start)
748 ;;; The lexical environment is presumably already null...
749 (defmacro with-ir1-environment (node &rest forms)
751 "With-IR1-Environment Node Form*
752 Bind the IR1 context variables so that IR1 conversion can be done after the
753 main conversion pass has finished."
754 (let ((n-node (gensym)))
755 `(let* ((,n-node ,node)
756 (*current-component* (block-component (node-block ,n-node)))
757 (*lexenv* (node-lexenv ,n-node))
758 (*current-path* (node-source-path ,n-node)))
761 ;;; Bind the hashtables used for keeping track of global variables,
762 ;;; functions, &c. Also establish condition handlers.
763 (defmacro with-ir1-namespace (&body forms)
764 `(let ((*free-variables* (make-hash-table :test 'eq))
765 (*free-functions* (make-hash-table :test 'equal))
766 (*constants* (make-hash-table :test 'equal))
767 (*source-paths* (make-hash-table :test 'eq)))
768 (handler-bind ((compiler-error #'compiler-error-handler)
769 (style-warning #'compiler-style-warning-handler)
770 (warning #'compiler-warning-handler))
773 (defmacro lexenv-find (name slot &key test)
775 "LEXENV-FIND Name Slot {Key Value}*
776 Look up Name in the lexical environment namespace designated by Slot,
777 returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
778 may be used to determine the name equality predicate."
779 (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot) *lexenv*)
780 :test ,(or test '#'eq))))
782 (values (cdr ,n-res) t)
785 ;;; These functions are called by the expansion of the DEFPRINTER
786 ;;; macro to do the actual printing.
787 (declaim (ftype (function (symbol t stream &optional t) (values))
788 defprinter-prin1 defprinter-princ))
789 (defun defprinter-prin1 (name value stream &optional indent)
790 (declare (ignore indent))
791 (defprinter-prinx #'prin1 name value stream))
792 (defun defprinter-princ (name value stream &optional indent)
793 (declare (ignore indent))
794 (defprinter-prinx #'princ name value stream))
795 (defun defprinter-prinx (prinx name value stream)
796 (declare (type function prinx))
797 (write-char #\space stream)
799 (pprint-newline :linear stream))
800 (format stream ":~A " name)
801 (funcall prinx value stream)
804 ;; Define some kind of reasonable PRINT-OBJECT method for a STRUCTURE-OBJECT.
806 ;; NAME is the name of the structure class, and CONC-NAME is the same as in
809 ;; The SLOT-DESCS describe how each slot should be printed. Each SLOT-DESC can
810 ;; be a slot name, indicating that the slot should simply be printed. A
811 ;; SLOT-DESC may also be a list of a slot name and other stuff. The other stuff
812 ;; is composed of keywords followed by expressions. The expressions are
813 ;; evaluated with the variable which is the slot name bound to the value of the
814 ;; slot. These keywords are defined:
816 ;; :PRIN1 Print the value of the expression instead of the slot value.
817 ;; :PRINC Like :PRIN1, only princ the value
818 ;; :TEST Only print something if the test is true.
820 ;; If no printing thing is specified then the slot value is printed as PRIN1.
822 ;; The structure being printed is bound to STRUCTURE and the stream is bound to
824 (defmacro defprinter ((name &key (conc-name (concatenate 'simple-string
828 (flet ((sref (slot-name)
829 `(,(symbolicate conc-name slot-name) structure)))
831 (dolist (slot-desc slot-descs)
833 (prints `(defprinter-prin1 ',slot-desc ,(sref slot-desc) stream))
834 (let ((sname (first slot-desc))
837 (do ((option (rest slot-desc) (cddr option)))
840 `(let ((,sname ,(sref sname)))
843 `((defprinter-prin1 ',sname ,sname
847 (stuff `(defprinter-prin1 ',sname ,(second option)
850 (stuff `(defprinter-princ ',sname ,(second option)
852 (:test (setq test (second option)))
854 (error "bad DEFPRINTER option: ~S" (first option)))))))))
856 `(def!method print-object ((structure ,name) stream)
857 (print-unreadable-object (structure stream :type t)
858 (pprint-logical-block (stream nil)
859 ;;(pprint-indent :current 2 stream)
862 ;;;; the Event statistics/trace utility
864 ;;; FIXME: This seems to be useful for troubleshooting and
865 ;;; experimentation, not for ordinary use, so it should probably
866 ;;; become conditional on SB-SHOW.
868 (eval-when (:compile-toplevel :load-toplevel :execute)
870 (defstruct event-info
871 ;; The name of this event.
872 (name (required-argument) :type symbol)
873 ;; The string rescribing this event.
874 (description (required-argument) :type string)
875 ;; The name of the variable we stash this in.
876 (var (required-argument) :type symbol)
877 ;; The number of times this event has happened.
878 (count 0 :type fixnum)
879 ;; The level of significance of this event.
880 (level (required-argument) :type unsigned-byte)
881 ;; If true, a function that gets called with the node that the event
883 (action nil :type (or function null)))
885 ;;; A hashtable from event names to event-info structures.
886 (defvar *event-info* (make-hash-table :test 'eq))
888 ;;; Return the event info for Name or die trying.
889 (declaim (ftype (function (t) event-info) event-info-or-lose))
890 (defun event-info-or-lose (name)
891 (let ((res (gethash name *event-info*)))
893 (error "~S is not the name of an event." name))
898 (declaim (ftype (function (symbol) fixnum) event-count))
899 (defun event-count (name)
901 "Return the number of times that Event has happened."
902 (event-info-count (event-info-or-lose name)))
904 (declaim (ftype (function (symbol) (or function null)) event-action))
905 (defun event-action (name)
907 "Return the function that is called when Event happens. If this is null,
908 there is no action. The function is passed the node to which the event
909 happened, or NIL if there is no relevant node. This may be set with SETF."
910 (event-info-action (event-info-or-lose name)))
911 (declaim (ftype (function (symbol (or function null)) (or function null))
913 (defun %set-event-action (name new-value)
914 (setf (event-info-action (event-info-or-lose name))
916 (defsetf event-action %set-event-action)
918 (declaim (ftype (function (symbol) unsigned-byte) event-level))
919 (defun event-level (name)
921 "Return the non-negative integer which represents the level of significance
922 of the event Name. This is used to determine whether to print a message when
923 the event happens. This may be set with SETF."
924 (event-info-level (event-info-or-lose name)))
925 (declaim (ftype (function (symbol unsigned-byte) unsigned-byte) %set-event-level))
926 (defun %set-event-level (name new-value)
927 (setf (event-info-level (event-info-or-lose name))
929 (defsetf event-level %set-event-level)
931 ;;; Make an EVENT-INFO structure and stash it in a variable so we can
932 ;;; get at it quickly.
933 (defmacro defevent (name description &optional (level 0))
935 "Defevent Name Description
936 Define a new kind of event. Name is a symbol which names the event and
937 Description is a string which describes the event. Level (default 0) is the
938 level of significance associated with this event; it is used to determine
939 whether to print a Note when the event happens."
940 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
941 `(eval-when (:compile-toplevel :load-toplevel :execute)
943 (make-event-info :name ',name
944 :description ',description
947 (setf (gethash ',name *event-info*) ,var-name)
950 (declaim (type unsigned-byte *event-note-threshold*))
951 (defvar *event-note-threshold* 1
953 "This variable is a non-negative integer specifying the lowest level of
954 event that will print a note when it occurs.")
956 ;;; Increment the counter and do any action. Mumble about the event if
957 ;;; policy indicates.
958 (defmacro event (name &optional node)
961 Note that the event with the specified Name has happened. Node is evaluated
962 to determine the node to which the event happened."
963 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
965 (declaim (ftype (function (&optional unsigned-byte stream) (values)) event-statistics))
966 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
968 "Print a listing of events and their counts, sorted by the count. Events
969 that happened fewer than Min-Count times will not be printed. Stream is the
972 (maphash #'(lambda (k v)
974 (when (>= (event-info-count v) min-count)
977 (dolist (event (sort (info) #'> :key #'event-info-count))
978 (format stream "~6D: ~A~%" (event-info-count event)
979 (event-info-description event)))
983 (declaim (ftype (function nil (values)) clear-event-statistics))
984 (defun clear-event-statistics ()
985 (maphash #'(lambda (k v)
987 (setf (event-info-count v) 0))
991 ;;;; functions on directly-linked lists (linked through specialized
992 ;;;; NEXT operations)
994 #!-sb-fluid (declaim (inline find-in position-in map-in))
1002 (test-not nil not-p))
1004 "Find Element in a null-terminated List linked by the accessor function
1005 Next. Key, Test and Test-Not are the same as for generic sequence
1007 (when (and test-p not-p)
1008 (error "It's silly to supply both :TEST and :TEST-NOT arguments."))
1010 (do ((current list (funcall next current)))
1011 ((null current) nil)
1012 (unless (funcall test-not (funcall key current) element)
1014 (do ((current list (funcall next current)))
1015 ((null current) nil)
1016 (when (funcall test (funcall key current) element)
1017 (return current)))))
1019 (defun position-in (next
1025 (test-not nil not-p))
1027 "Return the position of Element (or NIL if absent) in a null-terminated List
1028 linked by the accessor function Next. Key, Test and Test-Not are the same as
1029 for generic sequence functions."
1030 (when (and test-p not-p)
1031 (error "It's silly to supply both :TEST and :TEST-NOT arguments."))
1033 (do ((current list (funcall next current))
1035 ((null current) nil)
1036 (unless (funcall test-not (funcall key current) element)
1038 (do ((current list (funcall next current))
1040 ((null current) nil)
1041 (when (funcall test (funcall key current) element)
1044 (defun map-in (next function list)
1046 "Map Function over the elements in a null-terminated List linked by the
1047 accessor function Next, returning a list of the results."
1049 (do ((current list (funcall next current)))
1051 (res (funcall function current)))
1054 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
1055 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
1057 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
1058 ;;; arrangement, in order to get it to work in cross-compilation. This
1059 ;;; duplication should be removed, perhaps by rewriting the macro in a more
1060 ;;; cross-compiler-friendly way, or perhaps just by using some (MACROLET ((FROB
1061 ;;; ..)) .. FROB .. FROB) form, or perhaps by completely eliminating this macro
1062 ;;; and its partner PUSH-IN, but I don't want to do it now, because the system
1063 ;;; isn't running yet, so it'd be too hard to check that my changes were
1064 ;;; correct -- WHN 19990806
1065 (def!macro deletef-in (next place item &environment env)
1066 (multiple-value-bind (temps vals stores store access)
1067 (get-setf-expansion place env)
1069 (error "multiple store variables for ~S" place))
1070 (let ((n-item (gensym))
1072 (n-current (gensym))
1074 `(let* (,@(mapcar #'list temps vals)
1077 (if (eq ,n-place ,n-item)
1078 (let ((,(first stores) (,next ,n-place)))
1080 (do ((,n-prev ,n-place ,n-current)
1081 (,n-current (,next ,n-place)
1082 (,next ,n-current)))
1083 ((eq ,n-current ,n-item)
1084 (setf (,next ,n-prev)
1085 (,next ,n-current)))))
1087 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
1089 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
1090 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
1092 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
1093 ;;; arrangement, in order to get it to work in cross-compilation. This
1094 ;;; duplication should be removed, perhaps by rewriting the macro in a more
1095 ;;; cross-compiler-friendly way, or perhaps just by using some (MACROLET ((FROB
1096 ;;; ..)) .. FROB .. FROB) form, or perhaps by completely eliminating this macro
1097 ;;; and its partner DELETEF-IN, but I don't want to do it now, because the
1098 ;;; system isn't running yet, so it'd be too hard to check that my changes were
1099 ;;; correct -- WHN 19990806
1100 (def!macro push-in (next item place &environment env)
1102 "Push Item onto a list linked by the accessor function Next that is stored in
1104 (multiple-value-bind (temps vals stores store access)
1105 (get-setf-expansion place env)
1107 (error "multiple store variables for ~S" place))
1108 `(let (,@(mapcar #'list temps vals)
1109 (,(first stores) ,item))
1110 (setf (,next ,(first stores)) ,access)
1113 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
1115 (defmacro position-or-lose (&rest args)
1116 `(or (position ,@args)
1117 (error "Shouldn't happen?")))