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 (eval-when (:compile-toplevel :load-toplevel :execute)
31 ;;; a helper function for the POLICY macro: Look up a named optimization
32 ;;; quality in POLICY.
33 (declaim (ftype (function (policy symbol) policy-quality)))
34 (defun policy-quality (policy quality-name)
36 (cdr (assoc quality-name policy))))
38 ;;; A helper function for the POLICY macro: Return a list of symbols
39 ;;; naming the qualities which appear in EXPR.
40 (defun policy-qualities-used-by (expr)
45 (when (policy-quality-p x)
46 (pushnew x result)))))
52 ;;; syntactic sugar for querying optimization policy qualities
54 ;;; Evaluate EXPR in terms of the current optimization policy for
55 ;;; NODE, or if NODE is NIL, in terms of the current policy as defined
56 ;;; by *DEFAULT-POLICY* and *CURRENT-POLICY*. (Using NODE=NIL is only
57 ;;; well-defined during IR1 conversion.)
59 ;;; EXPR is a form which accesses the policy values by referring to
60 ;;; them by name, e.g. (> SPEED SPACE).
61 (defmacro policy (node expr)
62 (let* ((n-policy (gensym))
63 (binds (mapcar (lambda (name)
64 `(,name (policy-quality ,n-policy ',name)))
65 (policy-qualities-used-by expr))))
66 (/show "in POLICY" expr binds)
67 `(let* ((,n-policy (lexenv-policy ,(if node
73 ;;;; source-hacking defining forms
75 ;;; to be passed to PARSE-DEFMACRO when we want compiler errors
76 ;;; instead of real errors
77 #!-sb-fluid (declaim (inline convert-condition-into-compiler-error))
78 (defun convert-condition-into-compiler-error (datum &rest stuff)
80 (apply #'compiler-error datum stuff)
83 (apply #'make-condition datum stuff)
86 ;;; Parse a DEFMACRO-style lambda-list, setting things up so that a
87 ;;; compiler error happens if the syntax is invalid.
89 ;;; Define a function that converts a special form or other magical
90 ;;; thing into IR1. LAMBDA-LIST is a defmacro style lambda list.
91 ;;; START-VAR and CONT-VAR are bound to the start and result
92 ;;; continuations for the resulting IR1. KIND is the function kind to
93 ;;; associate with NAME.
94 (defmacro def-ir1-translator (name (lambda-list start-var cont-var
95 &key (kind :special-form))
97 (let ((fn-name (symbolicate "IR1-CONVERT-" name))
100 (multiple-value-bind (body decls doc)
101 (parse-defmacro lambda-list n-form body name "special form"
103 :error-fun 'convert-condition-into-compiler-error)
105 (declaim (ftype (function (continuation continuation t) (values))
107 (defun ,fn-name (,start-var ,cont-var ,n-form)
108 (let ((,n-env *lexenv*))
113 `((setf (fdocumentation ',name 'function) ,doc)))
114 ;; FIXME: Evidently "there can only be one!" -- we overwrite any
115 ;; other :IR1-CONVERT value. This deserves a warning, I think.
116 (setf (info :function :ir1-convert ',name) #',fn-name)
117 (setf (info :function :kind ',name) ,kind)
118 ;; It's nice to do this for error checking in the target
119 ;; SBCL, but it's not nice to do this when we're running in
120 ;; the cross-compilation host Lisp, which owns the
121 ;; SYMBOL-FUNCTION of its COMMON-LISP symbols.
123 ,@(when (eq kind :special-form)
124 `((setf (symbol-function ',name)
126 (declare (ignore rest))
127 (error "can't FUNCALL the SYMBOL-FUNCTION of ~
128 special forms")))))))))
130 ;;; (This is similar to DEF-IR1-TRANSLATOR, except that we pass if the
131 ;;; syntax is invalid.)
133 ;;; Define a macro-like source-to-source transformation for the
134 ;;; function NAME. A source transform may "pass" by returning a
135 ;;; non-nil second value. If the transform passes, then the form is
136 ;;; converted as a normal function call. If the supplied arguments are
137 ;;; not compatible with the specified LAMBDA-LIST, then the transform
138 ;;; automatically passes.
140 ;;; Source transforms may only be defined for functions. Source
141 ;;; transformation is not attempted if the function is declared
142 ;;; NOTINLINE. Source transforms should not examine their arguments.
143 ;;; If it matters how the function is used, then DEFTRANSFORM should
144 ;;; be used to define an IR1 transformation.
146 ;;; If the desirability of the transformation depends on the current
147 ;;; OPTIMIZE parameters, then the POLICY macro should be used to
148 ;;; determine when to pass.
149 (defmacro def-source-transform (name lambda-list &body body)
156 (apply #'symbolicate "SOURCE-TRANSFORM" (pieces)))
157 (symbolicate "SOURCE-TRANSFORM-" name)))
160 (multiple-value-bind (body decls)
161 (parse-defmacro lambda-list n-form body name "form"
163 :error-fun `(lambda (&rest stuff)
164 (declare (ignore stuff))
165 (return-from ,fn-name
168 (defun ,fn-name (,n-form)
169 (let ((,n-env *lexenv*))
172 (setf (info :function :source-transform ',name) #',fn-name)))))
174 ;;; Define a function that converts a use of (%PRIMITIVE NAME ..)
175 ;;; into Lisp code. LAMBDA-LIST is a DEFMACRO-style lambda list.
176 (defmacro def-primitive-translator (name lambda-list &body body)
177 (let ((fn-name (symbolicate "PRIMITIVE-TRANSLATE-" name))
180 (multiple-value-bind (body decls)
181 (parse-defmacro lambda-list n-form body name "%primitive"
183 :error-fun 'convert-condition-into-compiler-error)
185 (defun ,fn-name (,n-form)
186 (let ((,n-env *lexenv*))
189 (setf (gethash ',name *primitive-translators*) ',fn-name)))))
191 ;;;; boolean attribute utilities
193 ;;;; We need to maintain various sets of boolean attributes for known
194 ;;;; functions and VOPs. To save space and allow for quick set
195 ;;;; operations, we represent the attributes as bits in a fixnum.
197 (deftype attributes () 'fixnum)
199 (eval-when (:compile-toplevel :load-toplevel :execute)
201 ;;; Given a list of attribute names and an alist that translates them
202 ;;; to masks, return the OR of the masks.
203 (defun compute-attribute-mask (names alist)
204 (collect ((res 0 logior))
206 (let ((mask (cdr (assoc name alist))))
208 (error "unknown attribute name: ~S" name))
214 ;;; Parse the specification and generate some accessor macros.
216 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
217 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
219 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
220 ;;; arrangement, in order to get it to work in cross-compilation. This
221 ;;; duplication should be removed, perhaps by rewriting the macro in a
222 ;;; more cross-compiler-friendly way, or perhaps just by using some
223 ;;; (MACROLET ((FROB ..)) .. FROB .. FROB) form, but I don't want to
224 ;;; do it now, because the system isn't running yet, so it'd be too
225 ;;; hard to check that my changes were correct -- WHN 19990806
226 (def!macro def-boolean-attribute (name &rest attribute-names)
228 "Def-Boolean-Attribute Name Attribute-Name*
229 Define a new class of boolean attributes, with the attributes having the
230 specified Attribute-Names. Name is the name of the class, which is used to
231 generate some macros to manipulate sets of the attributes:
233 NAME-attributep attributes attribute-name*
234 Return true if one of the named attributes is present, false otherwise.
235 When set with SETF, updates the place Attributes setting or clearing the
236 specified attributes.
238 NAME-attributes attribute-name*
239 Return a set of the named attributes."
241 (let ((translations-name (symbolicate "*" name "-ATTRIBUTE-TRANSLATIONS*"))
242 (test-name (symbolicate name "-ATTRIBUTEP")))
244 (do ((mask 1 (ash mask 1))
245 (names attribute-names (cdr names)))
247 (alist (cons (car names) mask)))
251 (eval-when (:compile-toplevel :load-toplevel :execute)
252 (defparameter ,translations-name ',(alist)))
254 (defmacro ,test-name (attributes &rest attribute-names)
255 "Automagically generated boolean attribute test function. See
256 Def-Boolean-Attribute."
257 `(logtest ,(compute-attribute-mask attribute-names
259 (the attributes ,attributes)))
261 (define-setf-expander ,test-name (place &rest attributes
263 "Automagically generated boolean attribute setter. See
264 Def-Boolean-Attribute."
265 #-sb-xc-host (declare (type sb!c::lexenv env))
266 ;; FIXME: It would be better if &ENVIRONMENT arguments
267 ;; were automatically declared to have type LEXENV by the
268 ;; hairy-argument-handling code.
269 (multiple-value-bind (temps values stores set get)
270 (get-setf-expansion place env)
272 (error "multiple store variables for ~S" place))
273 (let ((newval (gensym))
275 (mask (compute-attribute-mask attributes
276 ,translations-name)))
277 (values `(,@temps ,n-place)
280 `(let ((,(first stores)
282 (logior ,n-place ,mask)
283 (logand ,n-place ,(lognot mask)))))
286 `(,',test-name ,n-place ,@attributes)))))
288 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
289 "Automagically generated boolean attribute creation function. See
290 Def-Boolean-Attribute."
291 (compute-attribute-mask attribute-names ,translations-name))))))
292 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
294 ;;; And now for some gratuitous pseudo-abstraction...
295 (defmacro attributes-union (&rest attributes)
297 "Returns the union of all the sets of boolean attributes which are its
300 (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
301 (defmacro attributes-intersection (&rest attributes)
303 "Returns the intersection of all the sets of boolean attributes which are its
306 (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
307 (declaim (ftype (function (attributes attributes) boolean) attributes=))
308 #!-sb-fluid (declaim (inline attributes=))
309 (defun attributes= (attr1 attr2)
311 "Returns true if the attributes present in Attr1 are identical to those in
315 ;;;; lambda-list parsing utilities
317 ;;;; IR1 transforms, optimizers and type inferencers need to be able
318 ;;;; to parse the IR1 representation of a function call using a
319 ;;;; standard function lambda-list.
321 (eval-when (:compile-toplevel :load-toplevel :execute)
323 ;;; Given a DEFTRANSFORM-style lambda-list, generate code that parses
324 ;;; the arguments of a combination with respect to that lambda-list.
325 ;;; BODY is the the list of forms which are to be evaluated within the
326 ;;; bindings. ARGS is the variable that holds list of argument
327 ;;; continuations. ERROR-FORM is a form which is evaluated when the
328 ;;; syntax of the supplied arguments is incorrect or a non-constant
329 ;;; argument keyword is supplied. Defaults and other gunk are ignored.
330 ;;; The second value is a list of all the arguments bound. We make the
331 ;;; variables IGNORABLE so that we don't have to manually declare them
332 ;;; Ignore if their only purpose is to make the syntax work.
333 (declaim (ftype (function (list list symbol t) list) parse-deftransform))
334 (defun parse-deftransform (lambda-list body args error-form)
335 (multiple-value-bind (req opt restp rest keyp keys allowp)
336 (parse-lambda-list lambda-list)
337 (let* ((min-args (length req))
338 (max-args (+ min-args (length opt)))
346 (binds `(,arg (nth ,(pos) ,args)))
350 (let ((var (if (atom arg) arg (first arg))))
352 (binds `(,var (nth ,(pos) ,args)))
357 (binds `(,rest (nthcdr ,(pos) ,args))))
360 (if (or (atom spec) (atom (first spec)))
361 (let* ((var (if (atom spec) spec (first spec)))
362 (key (intern (symbol-name var) "KEYWORD")))
364 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
366 (let* ((head (first spec))
370 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
373 (let ((n-length (gensym))
374 (limited-legal (not (or restp keyp))))
376 `(let ((,n-length (length ,args))
377 ,@(when keyp `((,n-keys (nthcdr ,(pos) ,args)))))
379 ;; FIXME: should be PROPER-LIST-OF-LENGTH-P
381 `(<= ,min-args ,n-length ,max-args)
382 `(<= ,min-args ,n-length))
385 `((check-keywords-constant ,n-keys))
386 `((check-transform-keys ,n-keys ',(keywords))))))
389 (declare (ignorable ,@(vars)))
397 ;;; Define an IR1 transformation for NAME. An IR1 transformation
398 ;;; computes a lambda that replaces the function variable reference
399 ;;; for the call. A transform may pass (decide not to transform the
400 ;;; call) by calling the GIVE-UP-IR1-TRANSFORM function. LAMBDA-LIST
401 ;;; both determines how the current call is parsed and specifies the
402 ;;; LAMBDA-LIST for the resulting lambda.
404 ;;; We parse the call and bind each of the lambda-list variables to
405 ;;; the continuation which represents the value of the argument. When
406 ;;; parsing the call, we ignore the defaults, and always bind the
407 ;;; variables for unsupplied arguments to NIL. If a required argument
408 ;;; is missing, an unknown keyword is supplied, or an argument keyword
409 ;;; is not a constant, then the transform automatically passes. The
410 ;;; DECLARATIONS apply to the bindings made by DEFTRANSFORM at
411 ;;; transformation time, rather than to the variables of the resulting
412 ;;; lambda. Bound-but-not-referenced warnings are suppressed for the
413 ;;; lambda-list variables. The DOC-STRING is used when printing
414 ;;; efficiency notes about the defined transform.
416 ;;; Normally, the body evaluates to a form which becomes the body of
417 ;;; an automatically constructed lambda. We make LAMBDA-LIST the
418 ;;; lambda-list for the lambda, and automatically insert declarations
419 ;;; of the argument and result types. If the second value of the body
420 ;;; is non-null, then it is a list of declarations which are to be
421 ;;; inserted at the head of the lambda. Automatic lambda generation
422 ;;; may be inhibited by explicitly returning a lambda from the body.
424 ;;; The ARG-TYPES and RESULT-TYPE are used to create a function type
425 ;;; which the call must satisfy before transformation is attempted.
426 ;;; The function type specifier is constructed by wrapping (FUNCTION
427 ;;; ...) around these values, so the lack of a restriction may be
428 ;;; specified by omitting the argument or supplying *. The argument
429 ;;; syntax specified in the ARG-TYPES need not be the same as that in
430 ;;; the LAMBDA-LIST, but the transform will never happen if the
431 ;;; syntaxes can't be satisfied simultaneously. If there is an
432 ;;; existing transform for the same function that has the same type,
433 ;;; then it is replaced with the new definition.
435 ;;; These are the legal keyword options:
436 ;;; :RESULT - A variable which is bound to the result continuation.
437 ;;; :NODE - A variable which is bound to the combination node for the call.
438 ;;; :POLICY - A form which is supplied to the POLICY macro to determine
439 ;;; whether this transformation is appropriate. If the result
440 ;;; is false, then the transform automatically gives up.
442 ;;; - The name and argument/result types are actually forms to be
443 ;;; evaluated. Useful for getting closures that transform similar
446 ;;; - Don't actually instantiate a transform, instead just DEFUN
447 ;;; Name with the specified transform definition function. This
448 ;;; may be later instantiated with %DEFTRANSFORM.
450 ;;; - If supplied and non-NIL, note this transform as ``important,''
451 ;;; which means efficiency notes will be generated when this
452 ;;; transform fails even if INHIBIT-WARNINGS=SPEED (but not if
453 ;;; INHIBIT-WARNINGS>SPEED).
454 ;;; :WHEN {:NATIVE | :BYTE | :BOTH}
455 ;;; - Indicates whether this transform applies to native code,
456 ;;; byte-code or both (default :native.)
457 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
459 &key result policy node defun-only
460 eval-name important (when :native))
461 &body body-decls-doc)
462 (when (and eval-name defun-only)
463 (error "can't specify both DEFUN-ONLY and EVAL-NAME"))
464 (multiple-value-bind (body decls doc) (parse-body body-decls-doc)
465 (let ((n-args (gensym))
466 (n-node (or node (gensym)))
469 (decls-body `(,@decls ,@body)))
470 (multiple-value-bind (parsed-form vars)
471 (parse-deftransform lambda-list
473 `((unless (policy ,n-node ,policy)
474 (give-up-ir1-transform))
478 '(give-up-ir1-transform))
481 (let* ((,n-args (basic-combination-args ,n-node))
483 `((,result (node-cont ,n-node)))))
484 (multiple-value-bind (,n-lambda ,n-decls)
486 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
488 `(lambda ,',lambda-list
489 (declare (ignorable ,@',vars))
493 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
495 ,(if eval-name name `',name)
497 ``(function ,,arg-types ,,result-type)
498 `'(function ,arg-types ,result-type))
501 ,(if important t nil)
504 ;;;; DEFKNOWN and DEFOPTIMIZER
506 ;;; This macro should be the way that all implementation independent
507 ;;; information about functions is made known to the compiler.
509 ;;; FIXME: The comment above suggests that perhaps some of my added
510 ;;; FTYPE declarations are in poor taste. Should I change my
511 ;;; declarations, or change the comment, or what?
513 ;;; FIXME: DEFKNOWN is needed only at build-the-system time. Figure
514 ;;; out some way to keep it from appearing in the target system.
516 ;;; Declare the function NAME to be a known function. We construct a
517 ;;; type specifier for the function by wrapping (FUNCTION ...) around
518 ;;; the ARG-TYPES and RESULT-TYPE. ATTRIBUTES is an unevaluated list
519 ;;; of boolean attributes of the function. These attributes are
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
562 ;;; information is given to all the names. The keywords specify the
563 ;;; initial values for various optimizers that the function might
565 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
567 (when (and (intersection attributes '(any call unwind))
568 (intersection attributes '(movable)))
569 (error "function cannot have both good and bad attributes: ~S" attributes))
571 `(%defknown ',(if (and (consp name)
572 (not (eq (car name) 'setf)))
575 '(function ,arg-types ,result-type)
576 (ir1-attributes ,@(if (member 'any attributes)
577 (union '(call unsafe unwind) attributes)
581 ;;; Create a function which parses combination args according to WHAT
582 ;;; and LAMBDA-LIST, where WHAT is either a function name or a list
583 ;;; (FUNCTION-NAME KIND) and does some KIND of optimization.
585 ;;; The FUNCTION-NAME must name a known function. LAMBDA-LIST is used
586 ;;; to parse the arguments to the combination as in DEFTRANSFORM. If
587 ;;; the argument syntax is invalid or there are non-constant keys,
588 ;;; then we simply return NIL.
590 ;;; The function is DEFUN'ed as FUNCTION-KIND-OPTIMIZER. Possible
591 ;;; kinds are DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If
592 ;;; a symbol is specified instead of a (FUNCTION KIND) list, then we
593 ;;; just do a DEFUN with the symbol as its name, and don't do anything
594 ;;; with the definition. This is useful for creating optimizers to be
595 ;;; passed by name to DEFKNOWN.
597 ;;; If supplied, NODE-VAR is bound to the combination node being
598 ;;; optimized. If additional VARS are supplied, then they are used as
599 ;;; the rest of the optimizer function's lambda-list. LTN-ANNOTATE
600 ;;; methods are passed an additional POLICY argument, and IR2-CONVERT
601 ;;; methods are passed an additional IR2-BLOCK argument.
602 (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
605 (let ((name (if (symbolp what) what
606 (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
608 (let ((n-args (gensym)))
610 (defun ,name (,n-node ,@vars)
611 (let ((,n-args (basic-combination-args ,n-node)))
612 ,(parse-deftransform lambda-list body n-args
613 `(return-from ,name nil))))
615 `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
616 (function-info-or-lose ',(first what)))
619 ;;;; IR groveling macros
621 ;;; Iterate over the blocks in a component, binding BLOCK-VAR to each
622 ;;; block in turn. The value of ENDS determines whether to iterate
623 ;;; over dummy head and tail blocks:
624 ;;; NIL -- Skip Head and Tail (the default)
625 ;;; :HEAD -- Do head but skip tail
626 ;;; :TAIL -- Do tail but skip head
627 ;;; :BOTH -- Do both head and tail
629 ;;; If supplied, RESULT-FORM is the value to return.
630 (defmacro do-blocks ((block-var component &optional ends result) &body body)
632 (unless (member ends '(nil :head :tail :both))
633 (error "losing ENDS value: ~S" ends))
634 (let ((n-component (gensym))
636 `(let* ((,n-component ,component)
637 (,n-tail ,(if (member ends '(:both :tail))
639 `(component-tail ,n-component))))
640 (do ((,block-var ,(if (member ends '(:both :head))
641 `(component-head ,n-component)
642 `(block-next (component-head ,n-component)))
643 (block-next ,block-var)))
644 ((eq ,block-var ,n-tail) ,result)
646 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
648 "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
649 Like Do-Blocks, only iterate over the blocks in reverse order."
650 (unless (member ends '(nil :head :tail :both))
651 (error "losing ENDS value: ~S" ends))
652 (let ((n-component (gensym))
654 `(let* ((,n-component ,component)
655 (,n-head ,(if (member ends '(:both :head))
657 `(component-head ,n-component))))
658 (do ((,block-var ,(if (member ends '(:both :tail))
659 `(component-tail ,n-component)
660 `(block-prev (component-tail ,n-component)))
661 (block-prev ,block-var)))
662 ((eq ,block-var ,n-head) ,result)
665 ;;; Could change it not to replicate the code someday perhaps...
666 (defmacro do-uses ((node-var continuation &optional result) &body body)
668 "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
669 Iterate over the uses of Continuation, binding Node to each one
671 (once-only ((n-cont continuation))
672 `(ecase (continuation-kind ,n-cont)
676 (let ((,node-var (continuation-use ,n-cont)))
679 ((:block-start :deleted-block-start)
680 (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
684 ;;; In the forward case, we terminate on Last-Cont so that we don't
685 ;;; have to worry about our termination condition being changed when
686 ;;; new code is added during the iteration. In the backward case, we
687 ;;; do NODE-PREV before evaluating the body so that we can keep going
688 ;;; when the current node is deleted.
690 ;;; When RESTART-P is supplied to DO-NODES, we start iterating over
691 ;;; again at the beginning of the block when we run into a
692 ;;; continuation whose block differs from the one we are trying to
693 ;;; iterate over, either beacuse the block was split, or because a
694 ;;; node was deleted out from under us (hence its block is NIL.) If
695 ;;; the block start is deleted, we just punt. With RESTART-P, we are
696 ;;; also more careful about termination, re-indirecting the BLOCK-LAST
698 (defmacro do-nodes ((node-var cont-var block &key restart-p) &body body)
700 "Do-Nodes (Node-Var Cont-Var Block {Key Value}*) {Declaration}* {Form}*
701 Iterate over the nodes in Block, binding Node-Var to the each node and
702 Cont-Var to the node's Cont. The only keyword option is Restart-P, which
703 causes iteration to be restarted when a node is deleted out from under us (if
704 not supplied, this is an error.)"
705 (let ((n-block (gensym))
706 (n-last-cont (gensym)))
707 `(let* ((,n-block ,block)
709 `((,n-last-cont (node-cont (block-last ,n-block))))))
710 (do* ((,node-var (continuation-next (block-start ,n-block))
713 ((eq (continuation-block ,cont-var) ,n-block)
714 (assert (continuation-next ,cont-var))
715 (continuation-next ,cont-var))
717 (let ((start (block-start ,n-block)))
718 (unless (eq (continuation-kind start)
721 (continuation-next start))))
722 `(continuation-next ,cont-var)))
723 (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
727 `(eq ,node-var (block-last ,n-block))
728 `(eq ,cont-var ,n-last-cont))
730 (defmacro do-nodes-backwards ((node-var cont-var block) &body body)
732 "Do-Nodes-Backwards (Node-Var Cont-Var Block) {Declaration}* {Form}*
733 Like Do-Nodes, only iterates in reverse order."
734 (let ((n-block (gensym))
738 `(let* ((,n-block ,block)
739 (,n-start (block-start ,n-block))
740 (,n-last (block-last ,n-block)))
741 (do* ((,cont-var (node-cont ,n-last) ,n-next)
742 (,node-var ,n-last (continuation-use ,cont-var))
743 (,n-next (node-prev ,node-var) (node-prev ,node-var)))
746 (when (eq ,n-next ,n-start)
749 ;;; The lexical environment is presumably already null...
750 (defmacro with-ir1-environment (node &rest forms)
752 "With-IR1-Environment Node Form*
753 Bind the IR1 context variables so that IR1 conversion can be done after the
754 main conversion pass has finished."
755 (let ((n-node (gensym)))
756 `(let* ((,n-node ,node)
757 (*current-component* (block-component (node-block ,n-node)))
758 (*lexenv* (node-lexenv ,n-node))
759 (*current-path* (node-source-path ,n-node)))
762 ;;; Bind the hashtables used for keeping track of global variables,
763 ;;; functions, &c. Also establish condition handlers.
764 (defmacro with-ir1-namespace (&body forms)
765 `(let ((*free-variables* (make-hash-table :test 'eq))
766 (*free-functions* (make-hash-table :test 'equal))
767 (*constants* (make-hash-table :test 'equal))
768 (*source-paths* (make-hash-table :test 'eq)))
769 (handler-bind ((compiler-error #'compiler-error-handler)
770 (style-warning #'compiler-style-warning-handler)
771 (warning #'compiler-warning-handler))
774 (defmacro lexenv-find (name slot &key test)
776 "LEXENV-FIND Name Slot {Key Value}*
777 Look up Name in the lexical environment namespace designated by Slot,
778 returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
779 may be used to determine the name equality predicate."
780 (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot) *lexenv*)
781 :test ,(or test '#'eq))))
783 (values (cdr ,n-res) t)
786 ;;; These functions are called by the expansion of the DEFPRINTER
787 ;;; macro to do the actual printing.
788 (declaim (ftype (function (symbol t stream &optional t) (values))
789 defprinter-prin1 defprinter-princ))
790 (defun defprinter-prin1 (name value stream &optional indent)
791 (declare (ignore indent))
792 (defprinter-prinx #'prin1 name value stream))
793 (defun defprinter-princ (name value stream &optional indent)
794 (declare (ignore indent))
795 (defprinter-prinx #'princ name value stream))
796 (defun defprinter-prinx (prinx name value stream)
797 (declare (type function prinx))
798 (write-char #\space stream)
800 (pprint-newline :linear stream))
801 (format stream ":~A " name)
802 (funcall prinx value stream)
805 ;; Define some kind of reasonable PRINT-OBJECT method for a STRUCTURE-OBJECT.
807 ;; NAME is the name of the structure class, and CONC-NAME is the same as in
810 ;; The SLOT-DESCS describe how each slot should be printed. Each SLOT-DESC can
811 ;; be a slot name, indicating that the slot should simply be printed. A
812 ;; SLOT-DESC may also be a list of a slot name and other stuff. The other stuff
813 ;; is composed of keywords followed by expressions. The expressions are
814 ;; evaluated with the variable which is the slot name bound to the value of the
815 ;; slot. These keywords are defined:
817 ;; :PRIN1 Print the value of the expression instead of the slot value.
818 ;; :PRINC Like :PRIN1, only princ the value
819 ;; :TEST Only print something if the test is true.
821 ;; If no printing thing is specified then the slot value is printed as PRIN1.
823 ;; The structure being printed is bound to STRUCTURE and the stream is bound to
825 (defmacro defprinter ((name &key (conc-name (concatenate 'simple-string
829 (flet ((sref (slot-name)
830 `(,(symbolicate conc-name slot-name) structure)))
832 (dolist (slot-desc slot-descs)
834 (prints `(defprinter-prin1 ',slot-desc ,(sref slot-desc) stream))
835 (let ((sname (first slot-desc))
838 (do ((option (rest slot-desc) (cddr option)))
841 `(let ((,sname ,(sref sname)))
844 `((defprinter-prin1 ',sname ,sname
848 (stuff `(defprinter-prin1 ',sname ,(second option)
851 (stuff `(defprinter-princ ',sname ,(second option)
853 (:test (setq test (second option)))
855 (error "bad DEFPRINTER option: ~S" (first option)))))))))
857 `(def!method print-object ((structure ,name) stream)
858 (print-unreadable-object (structure stream :type t)
859 (pprint-logical-block (stream nil)
860 ;;(pprint-indent :current 2 stream)
863 ;;;; the Event statistics/trace utility
865 ;;; FIXME: This seems to be useful for troubleshooting and
866 ;;; experimentation, not for ordinary use, so it should probably
867 ;;; become conditional on SB-SHOW.
869 (eval-when (:compile-toplevel :load-toplevel :execute)
871 (defstruct event-info
872 ;; The name of this event.
873 (name (required-argument) :type symbol)
874 ;; The string rescribing this event.
875 (description (required-argument) :type string)
876 ;; The name of the variable we stash this in.
877 (var (required-argument) :type symbol)
878 ;; The number of times this event has happened.
879 (count 0 :type fixnum)
880 ;; The level of significance of this event.
881 (level (required-argument) :type unsigned-byte)
882 ;; If true, a function that gets called with the node that the event
884 (action nil :type (or function null)))
886 ;;; A hashtable from event names to event-info structures.
887 (defvar *event-info* (make-hash-table :test 'eq))
889 ;;; Return the event info for Name or die trying.
890 (declaim (ftype (function (t) event-info) event-info-or-lose))
891 (defun event-info-or-lose (name)
892 (let ((res (gethash name *event-info*)))
894 (error "~S is not the name of an event." name))
899 (declaim (ftype (function (symbol) fixnum) event-count))
900 (defun event-count (name)
902 "Return the number of times that Event has happened."
903 (event-info-count (event-info-or-lose name)))
905 (declaim (ftype (function (symbol) (or function null)) event-action))
906 (defun event-action (name)
908 "Return the function that is called when Event happens. If this is null,
909 there is no action. The function is passed the node to which the event
910 happened, or NIL if there is no relevant node. This may be set with SETF."
911 (event-info-action (event-info-or-lose name)))
912 (declaim (ftype (function (symbol (or function null)) (or function null))
914 (defun %set-event-action (name new-value)
915 (setf (event-info-action (event-info-or-lose name))
917 (defsetf event-action %set-event-action)
919 (declaim (ftype (function (symbol) unsigned-byte) event-level))
920 (defun event-level (name)
922 "Return the non-negative integer which represents the level of significance
923 of the event Name. This is used to determine whether to print a message when
924 the event happens. This may be set with SETF."
925 (event-info-level (event-info-or-lose name)))
926 (declaim (ftype (function (symbol unsigned-byte) unsigned-byte) %set-event-level))
927 (defun %set-event-level (name new-value)
928 (setf (event-info-level (event-info-or-lose name))
930 (defsetf event-level %set-event-level)
932 ;;; Make an EVENT-INFO structure and stash it in a variable so we can
933 ;;; get at it quickly.
934 (defmacro defevent (name description &optional (level 0))
936 "Defevent Name Description
937 Define a new kind of event. Name is a symbol which names the event and
938 Description is a string which describes the event. Level (default 0) is the
939 level of significance associated with this event; it is used to determine
940 whether to print a Note when the event happens."
941 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
942 `(eval-when (:compile-toplevel :load-toplevel :execute)
944 (make-event-info :name ',name
945 :description ',description
948 (setf (gethash ',name *event-info*) ,var-name)
951 (declaim (type unsigned-byte *event-note-threshold*))
952 (defvar *event-note-threshold* 1
954 "This variable is a non-negative integer specifying the lowest level of
955 event that will print a note when it occurs.")
957 ;;; Increment the counter and do any action. Mumble about the event if
958 ;;; policy indicates.
959 (defmacro event (name &optional node)
962 Note that the event with the specified Name has happened. Node is evaluated
963 to determine the node to which the event happened."
964 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
966 (declaim (ftype (function (&optional unsigned-byte stream) (values)) event-statistics))
967 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
969 "Print a listing of events and their counts, sorted by the count. Events
970 that happened fewer than Min-Count times will not be printed. Stream is the
973 (maphash #'(lambda (k v)
975 (when (>= (event-info-count v) min-count)
978 (dolist (event (sort (info) #'> :key #'event-info-count))
979 (format stream "~6D: ~A~%" (event-info-count event)
980 (event-info-description event)))
984 (declaim (ftype (function nil (values)) clear-event-statistics))
985 (defun clear-event-statistics ()
986 (maphash #'(lambda (k v)
988 (setf (event-info-count v) 0))
992 ;;;; functions on directly-linked lists (linked through specialized
993 ;;;; NEXT operations)
995 #!-sb-fluid (declaim (inline find-in position-in map-in))
1003 (test-not nil not-p))
1005 "Find Element in a null-terminated List linked by the accessor function
1006 Next. Key, Test and Test-Not are the same as for generic sequence
1008 (when (and test-p not-p)
1009 (error "It's silly to supply both :TEST and :TEST-NOT arguments."))
1011 (do ((current list (funcall next current)))
1012 ((null current) nil)
1013 (unless (funcall test-not (funcall key current) element)
1015 (do ((current list (funcall next current)))
1016 ((null current) nil)
1017 (when (funcall test (funcall key current) element)
1018 (return current)))))
1020 (defun position-in (next
1026 (test-not nil not-p))
1028 "Return the position of Element (or NIL if absent) in a null-terminated List
1029 linked by the accessor function Next. Key, Test and Test-Not are the same as
1030 for generic sequence functions."
1031 (when (and test-p not-p)
1032 (error "It's silly to supply both :TEST and :TEST-NOT arguments."))
1034 (do ((current list (funcall next current))
1036 ((null current) nil)
1037 (unless (funcall test-not (funcall key current) element)
1039 (do ((current list (funcall next current))
1041 ((null current) nil)
1042 (when (funcall test (funcall key current) element)
1045 (defun map-in (next function list)
1047 "Map Function over the elements in a null-terminated List linked by the
1048 accessor function Next, returning a list of the results."
1050 (do ((current list (funcall next current)))
1052 (res (funcall function current)))
1055 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
1056 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
1058 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
1059 ;;; arrangement, in order to get it to work in cross-compilation. This
1060 ;;; duplication should be removed, perhaps by rewriting the macro in a more
1061 ;;; cross-compiler-friendly way, or perhaps just by using some (MACROLET ((FROB
1062 ;;; ..)) .. FROB .. FROB) form, or perhaps by completely eliminating this macro
1063 ;;; and its partner PUSH-IN, but I don't want to do it now, because the system
1064 ;;; isn't running yet, so it'd be too hard to check that my changes were
1065 ;;; correct -- WHN 19990806
1066 (def!macro deletef-in (next place item &environment env)
1067 (multiple-value-bind (temps vals stores store access)
1068 (get-setf-expansion place env)
1070 (error "multiple store variables for ~S" place))
1071 (let ((n-item (gensym))
1073 (n-current (gensym))
1075 `(let* (,@(mapcar #'list temps vals)
1078 (if (eq ,n-place ,n-item)
1079 (let ((,(first stores) (,next ,n-place)))
1081 (do ((,n-prev ,n-place ,n-current)
1082 (,n-current (,next ,n-place)
1083 (,next ,n-current)))
1084 ((eq ,n-current ,n-item)
1085 (setf (,next ,n-prev)
1086 (,next ,n-current)))))
1088 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
1090 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
1091 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
1093 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
1094 ;;; arrangement, in order to get it to work in cross-compilation. This
1095 ;;; duplication should be removed, perhaps by rewriting the macro in a more
1096 ;;; cross-compiler-friendly way, or perhaps just by using some (MACROLET ((FROB
1097 ;;; ..)) .. FROB .. FROB) form, or perhaps by completely eliminating this macro
1098 ;;; and its partner DELETEF-IN, but I don't want to do it now, because the
1099 ;;; system isn't running yet, so it'd be too hard to check that my changes were
1100 ;;; correct -- WHN 19990806
1101 (def!macro push-in (next item place &environment env)
1103 "Push Item onto a list linked by the accessor function Next that is stored in
1105 (multiple-value-bind (temps vals stores store access)
1106 (get-setf-expansion place env)
1108 (error "multiple store variables for ~S" place))
1109 `(let (,@(mapcar #'list temps vals)
1110 (,(first stores) ,item))
1111 (setf (,next ,(first stores)) ,access)
1114 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
1116 (defmacro position-or-lose (&rest args)
1117 `(or (position ,@args)
1118 (error "shouldn't happen?")))