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 ;;; :MAYBE-INLINE is quite different from :INLINE. As explained
26 ;;; by APD on #lisp 2005-11-26: "MAYBE-INLINE lambda is
27 ;;; instantiated once per component, INLINE - for all
28 ;;; references (even under #'without FUNCALL)."
29 (deftype inlinep () '(member :inline :maybe-inline :notinline nil))
31 ;;;; source-hacking defining forms
33 ;;; Parse a DEFMACRO-style lambda-list, setting things up so that a
34 ;;; compiler error happens if the syntax is invalid.
36 ;;; Define a function that converts a special form or other magical
37 ;;; thing into IR1. LAMBDA-LIST is a defmacro style lambda
38 ;;; list. START-VAR, NEXT-VAR and RESULT-VAR are bound to the start and
39 ;;; result continuations for the resulting IR1. KIND is the function
40 ;;; kind to associate with NAME.
41 (defmacro def-ir1-translator (name (lambda-list start-var next-var result-var)
43 (let ((fn-name (symbolicate "IR1-CONVERT-" name))
44 (guard-name (symbolicate name "-GUARD")))
45 (with-unique-names (whole-var n-env)
46 (multiple-value-bind (body decls doc)
47 (parse-defmacro lambda-list whole-var body name "special form"
49 :error-fun 'compiler-error
52 (declaim (ftype (function (ctran ctran (or lvar null) t) (values))
54 (defun ,fn-name (,start-var ,next-var ,result-var ,whole-var
55 &aux (,n-env *lexenv*))
56 (declare (ignorable ,start-var ,next-var ,result-var))
61 ;; It's nice to do this for error checking in the target
62 ;; SBCL, but it's not nice to do this when we're running in
63 ;; the cross-compilation host Lisp, which owns the
64 ;; SYMBOL-FUNCTION of its COMMON-LISP symbols. These guard
65 ;; functions also provide the documentation for special forms.
67 (defun ,guard-name (&rest args)
68 ,@(when doc (list doc))
69 (declare (ignore args))
70 (error 'special-form-function :name ',name))
71 (let ((fun #',guard-name))
72 (setf (%simple-fun-arglist fun) ',lambda-list
73 (%simple-fun-name fun) ',name
74 (symbol-function ',name) fun)
75 (fmakunbound ',guard-name)))
76 ;; FIXME: Evidently "there can only be one!" -- we overwrite any
77 ;; other :IR1-CONVERT value. This deserves a warning, I think.
78 (setf (info :function :ir1-convert ',name) #',fn-name)
79 ;; FIXME: rename this to SPECIAL-OPERATOR, to update it to
81 (setf (info :function :kind ',name) :special-form)
84 ;;; (This is similar to DEF-IR1-TRANSLATOR, except that we pass if the
85 ;;; syntax is invalid.)
87 ;;; Define a macro-like source-to-source transformation for the
88 ;;; function NAME. A source transform may "pass" by returning a
89 ;;; non-nil second value. If the transform passes, then the form is
90 ;;; converted as a normal function call. If the supplied arguments are
91 ;;; not compatible with the specified LAMBDA-LIST, then the transform
92 ;;; automatically passes.
94 ;;; Source transforms may only be defined for functions. Source
95 ;;; transformation is not attempted if the function is declared
96 ;;; NOTINLINE. Source transforms should not examine their arguments.
97 ;;; If it matters how the function is used, then DEFTRANSFORM should
98 ;;; be used to define an IR1 transformation.
100 ;;; If the desirability of the transformation depends on the current
101 ;;; OPTIMIZE parameters, then the POLICY macro should be used to
102 ;;; determine when to pass.
103 (defmacro source-transform-lambda (lambda-list &body body)
104 (with-unique-names (whole-var n-env name)
105 (multiple-value-bind (body decls)
106 (parse-defmacro lambda-list whole-var body "source transform" "form"
108 :error-fun `(lambda (&rest stuff)
109 (declare (ignore stuff))
113 `(lambda (,whole-var &aux (,n-env *lexenv*))
117 (defmacro define-source-transform (name lambda-list &body body)
118 `(setf (info :function :source-transform ',name)
119 (source-transform-lambda ,lambda-list ,@body)))
121 ;;;; boolean attribute utilities
123 ;;;; We need to maintain various sets of boolean attributes for known
124 ;;;; functions and VOPs. To save space and allow for quick set
125 ;;;; operations, we represent the attributes as bits in a fixnum.
127 (deftype attributes () 'fixnum)
129 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
131 ;;; Given a list of attribute names and an alist that translates them
132 ;;; to masks, return the OR of the masks.
133 (defun compute-attribute-mask (names alist)
134 (collect ((res 0 logior))
136 (let ((mask (cdr (assoc name alist))))
138 (error "unknown attribute name: ~S" name))
144 ;;; Define a new class of boolean attributes, with the attributes
145 ;;; having the specified ATTRIBUTE-NAMES. NAME is the name of the
146 ;;; class, which is used to generate some macros to manipulate sets of
149 ;;; NAME-attributep attributes attribute-name*
150 ;;; Return true if one of the named attributes is present, false
151 ;;; otherwise. When set with SETF, updates the place Attributes
152 ;;; setting or clearing the specified attributes.
154 ;;; NAME-attributes attribute-name*
155 ;;; Return a set of the named attributes.
158 (def!macro !def-boolean-attribute (name &rest attribute-names)
160 (let ((translations-name (symbolicate "*" name "-ATTRIBUTE-TRANSLATIONS*"))
161 (test-name (symbolicate name "-ATTRIBUTEP"))
162 (decoder-name (symbolicate "DECODE-" name "-ATTRIBUTES")))
164 (do ((mask 1 (ash mask 1))
165 (names attribute-names (cdr names)))
167 (alist (cons (car names) mask)))
169 (eval-when (:compile-toplevel :load-toplevel :execute)
170 (defparameter ,translations-name ',(alist)))
171 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
172 "Automagically generated boolean attribute creation function.
173 See !DEF-BOOLEAN-ATTRIBUTE."
174 (compute-attribute-mask attribute-names ,translations-name))
175 (defmacro ,test-name (attributes &rest attribute-names)
176 "Automagically generated boolean attribute test function.
177 See !DEF-BOOLEAN-ATTRIBUTE."
178 `(logtest ,(compute-attribute-mask attribute-names
180 (the attributes ,attributes)))
181 ;; This definition transforms strangely under UNCROSS, in a
182 ;; way that DEF!MACRO doesn't understand, so we delegate it
183 ;; to a submacro then define the submacro differently when
184 ;; building the xc and when building the target compiler.
185 (!def-boolean-attribute-setter ,test-name
188 (defun ,decoder-name (attributes)
189 (loop for (name . mask) in ,translations-name
190 when (logtest mask attributes)
193 ;; It seems to be difficult to express in DEF!MACRO machinery what
194 ;; to do with target-vs-host GET-SETF-EXPANSION in here, so we just
195 ;; hack it by hand, passing a different GET-SETF-EXPANSION-FUN-NAME
196 ;; in the host DEFMACRO and target DEFMACRO-MUNDANELY cases.
197 (defun guts-of-!def-boolean-attribute-setter (test-name
200 get-setf-expansion-fun-name)
201 (declare (ignore attribute-names))
202 `(define-setf-expander ,test-name (place &rest attributes
204 "Automagically generated boolean attribute setter. See
205 !DEF-BOOLEAN-ATTRIBUTE."
206 #-sb-xc-host (declare (type sb!c::lexenv env))
207 ;; FIXME: It would be better if &ENVIRONMENT arguments were
208 ;; automatically declared to have type LEXENV by the
209 ;; hairy-argument-handling code.
210 (multiple-value-bind (temps values stores set get)
211 (,get-setf-expansion-fun-name place env)
213 (error "multiple store variables for ~S" place))
214 (let ((newval (sb!xc:gensym))
215 (n-place (sb!xc:gensym))
216 (mask (compute-attribute-mask attributes ,translations-name)))
217 (values `(,@temps ,n-place)
220 `(let ((,(first stores)
222 (logior ,n-place ,mask)
223 (logand ,n-place ,(lognot mask)))))
226 `(,',test-name ,n-place ,@attributes))))))
227 ;; We define the host version here, and the just-like-it-but-different
228 ;; target version later, after DEFMACRO-MUNDANELY has been defined.
229 (defmacro !def-boolean-attribute-setter (test-name
231 &rest attribute-names)
232 (guts-of-!def-boolean-attribute-setter test-name
235 'get-setf-expansion)))
237 ;;; Otherwise the source locations for DEFTRANSFORM, DEFKNOWN, &c
238 ;;; would be off by one toplevel form as their source locations are
239 ;;; determined before cross-compiling where the above PROGN is not
243 ;;; And now for some gratuitous pseudo-abstraction...
246 ;;; Return the union of all the sets of boolean attributes which are its
248 ;;; ATTRIBUTES-INTERSECTION
249 ;;; Return the intersection of all the sets of boolean attributes which
250 ;;; are its arguments.
252 ;;; True if the attributes present in ATTR1 are identical to
254 (defmacro attributes-union (&rest attributes)
256 (logior ,@(mapcar (lambda (x) `(the attributes ,x)) attributes))))
257 (defmacro attributes-intersection (&rest attributes)
259 (logand ,@(mapcar (lambda (x) `(the attributes ,x)) attributes))))
260 (declaim (ftype (function (attributes attributes) boolean) attributes=))
261 #!-sb-fluid (declaim (inline attributes=))
262 (defun attributes= (attr1 attr2)
265 ;;;; lambda-list parsing utilities
267 ;;;; IR1 transforms, optimizers and type inferencers need to be able
268 ;;;; to parse the IR1 representation of a function call using a
269 ;;;; standard function lambda-list.
271 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
273 ;;; Given a DEFTRANSFORM-style lambda-list, generate code that parses
274 ;;; the arguments of a combination with respect to that
275 ;;; lambda-list. BODY is the list of forms which are to be
276 ;;; evaluated within the bindings. ARGS is the variable that holds
277 ;;; list of argument lvars. ERROR-FORM is a form which is evaluated
278 ;;; when the syntax of the supplied arguments is incorrect or a
279 ;;; non-constant argument keyword is supplied. Defaults and other gunk
280 ;;; are ignored. The second value is a list of all the arguments
281 ;;; bound. We make the variables IGNORABLE so that we don't have to
282 ;;; manually declare them IGNORE if their only purpose is to make the
284 (defun parse-deftransform (lambda-list body args error-form)
285 (multiple-value-bind (req opt restp rest keyp keys allowp)
286 (parse-lambda-list lambda-list)
287 (let* ((min-args (length req))
288 (max-args (+ min-args (length opt)))
296 (binds `(,arg (nth ,(pos) ,args)))
300 (let ((var (if (atom arg) arg (first arg))))
302 (binds `(,var (nth ,(pos) ,args)))
307 (binds `(,rest (nthcdr ,(pos) ,args))))
310 (if (or (atom spec) (atom (first spec)))
311 (let* ((var (if (atom spec) spec (first spec)))
312 (key (keywordicate var)))
314 (binds `(,var (find-keyword-lvar ,n-keys ,key)))
316 (let* ((head (first spec))
320 (binds `(,var (find-keyword-lvar ,n-keys ,key)))
323 (let ((n-length (gensym))
324 (limited-legal (not (or restp keyp))))
326 `(let ((,n-length (length ,args))
327 ,@(when keyp `((,n-keys (nthcdr ,(pos) ,args)))))
329 ;; FIXME: should be PROPER-LIST-OF-LENGTH-P
331 `(<= ,min-args ,n-length ,max-args)
332 `(<= ,min-args ,n-length))
335 `((check-key-args-constant ,n-keys))
336 `((check-transform-keys ,n-keys ',(keywords))))))
339 (declare (ignorable ,@(vars)))
347 ;;; Define an IR1 transformation for NAME. An IR1 transformation
348 ;;; computes a lambda that replaces the function variable reference
349 ;;; for the call. A transform may pass (decide not to transform the
350 ;;; call) by calling the GIVE-UP-IR1-TRANSFORM function. LAMBDA-LIST
351 ;;; both determines how the current call is parsed and specifies the
352 ;;; LAMBDA-LIST for the resulting lambda.
354 ;;; We parse the call and bind each of the lambda-list variables to
355 ;;; the lvar which represents the value of the argument. When parsing
356 ;;; the call, we ignore the defaults, and always bind the variables
357 ;;; for unsupplied arguments to NIL. If a required argument is
358 ;;; missing, an unknown keyword is supplied, or an argument keyword is
359 ;;; not a constant, then the transform automatically passes. The
360 ;;; DECLARATIONS apply to the bindings made by DEFTRANSFORM at
361 ;;; transformation time, rather than to the variables of the resulting
362 ;;; lambda. Bound-but-not-referenced warnings are suppressed for the
363 ;;; lambda-list variables. The DOC-STRING is used when printing
364 ;;; efficiency notes about the defined transform.
366 ;;; Normally, the body evaluates to a form which becomes the body of
367 ;;; an automatically constructed lambda. We make LAMBDA-LIST the
368 ;;; lambda-list for the lambda, and automatically insert declarations
369 ;;; of the argument and result types. If the second value of the body
370 ;;; is non-null, then it is a list of declarations which are to be
371 ;;; inserted at the head of the lambda. Automatic lambda generation
372 ;;; may be inhibited by explicitly returning a lambda from the body.
374 ;;; The ARG-TYPES and RESULT-TYPE are used to create a function type
375 ;;; which the call must satisfy before transformation is attempted.
376 ;;; The function type specifier is constructed by wrapping (FUNCTION
377 ;;; ...) around these values, so the lack of a restriction may be
378 ;;; specified by omitting the argument or supplying *. The argument
379 ;;; syntax specified in the ARG-TYPES need not be the same as that in
380 ;;; the LAMBDA-LIST, but the transform will never happen if the
381 ;;; syntaxes can't be satisfied simultaneously. If there is an
382 ;;; existing transform for the same function that has the same type,
383 ;;; then it is replaced with the new definition.
385 ;;; These are the legal keyword options:
386 ;;; :RESULT - A variable which is bound to the result lvar.
387 ;;; :NODE - A variable which is bound to the combination node for the call.
388 ;;; :POLICY - A form which is supplied to the POLICY macro to determine
389 ;;; whether this transformation is appropriate. If the result
390 ;;; is false, then the transform automatically gives up.
392 ;;; - The name and argument/result types are actually forms to be
393 ;;; evaluated. Useful for getting closures that transform similar
396 ;;; - Don't actually instantiate a transform, instead just DEFUN
397 ;;; Name with the specified transform definition function. This
398 ;;; may be later instantiated with %DEFTRANSFORM.
400 ;;; - If supplied and non-NIL, note this transform as ``important,''
401 ;;; which means efficiency notes will be generated when this
402 ;;; transform fails even if INHIBIT-WARNINGS=SPEED (but not if
403 ;;; INHIBIT-WARNINGS>SPEED).
404 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
406 &key result policy node defun-only
408 &body body-decls-doc)
409 (when (and eval-name defun-only)
410 (error "can't specify both DEFUN-ONLY and EVAL-NAME"))
411 (multiple-value-bind (body decls doc) (parse-body body-decls-doc)
412 (let ((n-args (sb!xc:gensym))
413 (n-node (or node (sb!xc:gensym)))
414 (n-decls (sb!xc:gensym))
415 (n-lambda (sb!xc:gensym))
416 (decls-body `(,@decls ,@body)))
417 (multiple-value-bind (parsed-form vars)
418 (parse-deftransform lambda-list
420 `((unless (policy ,n-node ,policy)
421 (give-up-ir1-transform))
425 '(give-up-ir1-transform))
428 (let* ((,n-args (basic-combination-args ,n-node))
430 `((,result (node-lvar ,n-node)))))
431 (multiple-value-bind (,n-lambda ,n-decls)
433 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
435 `(lambda ,',lambda-list
436 (declare (ignorable ,@',vars))
440 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
442 ,(if eval-name name `',name)
444 ``(function ,,arg-types ,,result-type)
445 `'(function ,arg-types ,result-type))
448 ,(if important t nil))))))))
450 ;;;; DEFKNOWN and DEFOPTIMIZER
452 ;;; This macro should be the way that all implementation independent
453 ;;; information about functions is made known to the compiler.
455 ;;; FIXME: The comment above suggests that perhaps some of my added
456 ;;; FTYPE declarations are in poor taste. Should I change my
457 ;;; declarations, or change the comment, or what?
459 ;;; FIXME: DEFKNOWN is needed only at build-the-system time. Figure
460 ;;; out some way to keep it from appearing in the target system.
462 ;;; Declare the function NAME to be a known function. We construct a
463 ;;; type specifier for the function by wrapping (FUNCTION ...) around
464 ;;; the ARG-TYPES and RESULT-TYPE. ATTRIBUTES is an unevaluated list
465 ;;; of boolean attributes of the function. See their description in
466 ;;; (!DEF-BOOLEAN-ATTRIBUTE IR1). NAME may also be a list of names, in
467 ;;; which case the same information is given to all the names. The
468 ;;; keywords specify the initial values for various optimizers that
469 ;;; the function might have.
470 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
473 (when (member 'unsafe attributes)
474 (style-warn "Ignoring legacy attribute UNSAFE. Replaced by its inverse: DX-SAFE.")
475 (setf attributes (remove 'unsafe attributes)))
476 (when (and (intersection attributes '(any call unwind))
477 (intersection attributes '(movable)))
478 (error "function cannot have both good and bad attributes: ~S" attributes))
480 (when (member 'any attributes)
481 (setq attributes (union '(call unwind) attributes)))
482 (when (member 'flushable attributes)
483 (pushnew 'unsafely-flushable attributes))
485 `(%defknown ',(if (and (consp name)
486 (not (legal-fun-name-p name)))
489 '(sfunction ,arg-types ,result-type)
490 (ir1-attributes ,@attributes)
493 ;;; Create a function which parses combination args according to WHAT
494 ;;; and LAMBDA-LIST, where WHAT is either a function name or a list
495 ;;; (FUN-NAME KIND) and does some KIND of optimization.
497 ;;; The FUN-NAME must name a known function. LAMBDA-LIST is used
498 ;;; to parse the arguments to the combination as in DEFTRANSFORM. If
499 ;;; the argument syntax is invalid or there are non-constant keys,
500 ;;; then we simply return NIL.
502 ;;; The function is DEFUN'ed as FUNCTION-KIND-OPTIMIZER. Possible
503 ;;; kinds are DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If
504 ;;; a symbol is specified instead of a (FUNCTION KIND) list, then we
505 ;;; just do a DEFUN with the symbol as its name, and don't do anything
506 ;;; with the definition. This is useful for creating optimizers to be
507 ;;; passed by name to DEFKNOWN.
509 ;;; If supplied, NODE-VAR is bound to the combination node being
510 ;;; optimized. If additional VARS are supplied, then they are used as
511 ;;; the rest of the optimizer function's lambda-list. LTN-ANNOTATE
512 ;;; methods are passed an additional POLICY argument, and IR2-CONVERT
513 ;;; methods are passed an additional IR2-BLOCK argument.
514 (defmacro defoptimizer (what (lambda-list &optional (n-node (sb!xc:gensym))
517 (let ((name (if (symbolp what) what
518 (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
520 (let ((n-args (gensym)))
522 (defun ,name (,n-node ,@vars)
523 (declare (ignorable ,@vars))
524 (let ((,n-args (basic-combination-args ,n-node)))
525 ,(parse-deftransform lambda-list body n-args
526 `(return-from ,name nil))))
528 `((setf (,(let ((*package* (symbol-package 'sb!c::fun-info)))
529 (symbolicate "FUN-INFO-" (second what)))
530 (fun-info-or-lose ',(first what)))
533 ;;;; IR groveling macros
535 ;;; Iterate over the blocks in a component, binding BLOCK-VAR to each
536 ;;; block in turn. The value of ENDS determines whether to iterate
537 ;;; over dummy head and tail blocks:
538 ;;; NIL -- Skip Head and Tail (the default)
539 ;;; :HEAD -- Do head but skip tail
540 ;;; :TAIL -- Do tail but skip head
541 ;;; :BOTH -- Do both head and tail
543 ;;; If supplied, RESULT-FORM is the value to return.
544 (defmacro do-blocks ((block-var component &optional ends result) &body body)
545 (unless (member ends '(nil :head :tail :both))
546 (error "losing ENDS value: ~S" ends))
547 (let ((n-component (gensym))
549 `(let* ((,n-component ,component)
550 (,n-tail ,(if (member ends '(:both :tail))
552 `(component-tail ,n-component))))
553 (do ((,block-var ,(if (member ends '(:both :head))
554 `(component-head ,n-component)
555 `(block-next (component-head ,n-component)))
556 (block-next ,block-var)))
557 ((eq ,block-var ,n-tail) ,result)
559 ;;; like DO-BLOCKS, only iterating over the blocks in reverse order
560 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
561 (unless (member ends '(nil :head :tail :both))
562 (error "losing ENDS value: ~S" ends))
563 (let ((n-component (gensym))
565 `(let* ((,n-component ,component)
566 (,n-head ,(if (member ends '(:both :head))
568 `(component-head ,n-component))))
569 (do ((,block-var ,(if (member ends '(:both :tail))
570 `(component-tail ,n-component)
571 `(block-prev (component-tail ,n-component)))
572 (block-prev ,block-var)))
573 ((eq ,block-var ,n-head) ,result)
576 ;;; Iterate over the uses of LVAR, binding NODE to each one
578 (defmacro do-uses ((node-var lvar &optional result) &body body)
579 (with-unique-names (uses)
580 `(let ((,uses (lvar-uses ,lvar)))
582 (flet ((do-1-use (,node-var)
590 ;;; Iterate over the nodes in BLOCK, binding NODE-VAR to the each node
591 ;;; and LVAR-VAR to the node's LVAR. The only keyword option is
592 ;;; RESTART-P, which causes iteration to be restarted when a node is
593 ;;; deleted out from under us. (If not supplied, this is an error.)
595 ;;; In the forward case, we terminate when NODE does not have NEXT, so
596 ;;; that we do not have to worry about our termination condition being
597 ;;; changed when new code is added during the iteration. In the
598 ;;; backward case, we do NODE-PREV before evaluating the body so that
599 ;;; we can keep going when the current node is deleted.
601 ;;; When RESTART-P is supplied to DO-NODES, we start iterating over
602 ;;; again at the beginning of the block when we run into a ctran whose
603 ;;; block differs from the one we are trying to iterate over, either
604 ;;; because the block was split, or because a node was deleted out
605 ;;; from under us (hence its block is NIL.) If the block start is
606 ;;; deleted, we just punt. With RESTART-P, we are also more careful
607 ;;; about termination, re-indirecting the BLOCK-LAST each time.
608 (defmacro do-nodes ((node-var lvar-var block &key restart-p)
610 (with-unique-names (n-block n-start)
611 `(do* ((,n-block ,block)
612 (,n-start (block-start ,n-block))
614 (,node-var (ctran-next ,n-start)
616 `(let ((next (node-next ,node-var)))
620 ((eq (ctran-block next) ,n-block)
623 (let ((start (block-start ,n-block)))
624 (unless (eq (ctran-kind start)
627 (ctran-next start)))))
628 `(acond ((node-next ,node-var)
632 `((,lvar-var (when (valued-node-p ,node-var)
633 (node-lvar ,node-var))
634 (when (valued-node-p ,node-var)
635 (node-lvar ,node-var))))))
639 `((when (block-delete-p ,n-block)
642 ;;; Like DO-NODES, only iterating in reverse order. Should be careful
643 ;;; with block being split under us.
644 (defmacro do-nodes-backwards ((node-var lvar block &key restart-p) &body body)
645 (let ((n-block (gensym))
647 `(loop with ,n-block = ,block
648 for ,node-var = (block-last ,n-block) then
650 `(if (eq ,n-block (ctran-block ,n-prev))
652 (block-last ,n-block))
653 `(ctran-use ,n-prev))
654 for ,n-prev = (when ,node-var (node-prev ,node-var))
655 and ,lvar = (when (and ,node-var (valued-node-p ,node-var))
656 (node-lvar ,node-var))
658 `(and ,node-var (not (block-to-be-deleted-p ,n-block)))
663 (defmacro do-nodes-carefully ((node-var block) &body body)
664 (with-unique-names (n-block n-ctran)
665 `(loop with ,n-block = ,block
666 for ,n-ctran = (block-start ,n-block) then (node-next ,node-var)
667 for ,node-var = (and ,n-ctran (ctran-next ,n-ctran))
671 ;;; Bind the IR1 context variables to the values associated with NODE,
672 ;;; so that new, extra IR1 conversion related to NODE can be done
673 ;;; after the original conversion pass has finished.
674 (defmacro with-ir1-environment-from-node (node &rest forms)
675 `(flet ((closure-needing-ir1-environment-from-node ()
677 (%with-ir1-environment-from-node
679 #'closure-needing-ir1-environment-from-node)))
680 (defun %with-ir1-environment-from-node (node fun)
681 (declare (type node node) (type function fun))
682 (let ((*current-component* (node-component node))
683 (*lexenv* (node-lexenv node))
684 (*current-path* (node-source-path node)))
685 (aver-live-component *current-component*)
688 ;;; Bind the hashtables used for keeping track of global variables,
689 ;;; functions, etc. Also establish condition handlers.
690 (defmacro with-ir1-namespace (&body forms)
691 `(let ((*free-vars* (make-hash-table :test 'eq))
692 (*free-funs* (make-hash-table :test 'equal))
693 (*constants* (make-hash-table :test 'equal))
694 (*source-paths* (make-hash-table :test 'eq)))
695 (handler-bind ((compiler-error #'compiler-error-handler)
696 (style-warning #'compiler-style-warning-handler)
697 (warning #'compiler-warning-handler))
700 ;;; Look up NAME in the lexical environment namespace designated by
701 ;;; SLOT, returning the <value, T>, or <NIL, NIL> if no entry. The
702 ;;; :TEST keyword may be used to determine the name equality
704 (defmacro lexenv-find (name slot &key test)
705 (once-only ((n-res `(assoc ,name (,(let ((*package* (symbol-package 'lexenv-funs)))
706 (symbolicate "LEXENV-" slot))
708 :test ,(or test '#'eq))))
710 (values (cdr ,n-res) t)
713 (defmacro with-component-last-block ((component block) &body body)
714 (with-unique-names (old-last-block)
715 (once-only ((component component)
717 `(let ((,old-last-block (component-last-block ,component)))
719 (progn (setf (component-last-block ,component)
722 (setf (component-last-block ,component)
723 ,old-last-block))))))
726 ;;;; the EVENT statistics/trace utility
728 ;;; FIXME: This seems to be useful for troubleshooting and
729 ;;; experimentation, not for ordinary use, so it should probably
730 ;;; become conditional on SB-SHOW.
732 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
734 (defstruct (event-info (:copier nil))
735 ;; The name of this event.
736 (name (missing-arg) :type symbol)
737 ;; The string rescribing this event.
738 (description (missing-arg) :type string)
739 ;; The name of the variable we stash this in.
740 (var (missing-arg) :type symbol)
741 ;; The number of times this event has happened.
742 (count 0 :type fixnum)
743 ;; The level of significance of this event.
744 (level (missing-arg) :type unsigned-byte)
745 ;; If true, a function that gets called with the node that the event
747 (action nil :type (or function null)))
749 ;;; A hashtable from event names to event-info structures.
750 (defvar *event-info* (make-hash-table :test 'eq))
752 ;;; Return the event info for Name or die trying.
753 (declaim (ftype (function (t) event-info) event-info-or-lose))
754 (defun event-info-or-lose (name)
755 (let ((res (gethash name *event-info*)))
757 (error "~S is not the name of an event." name))
762 ;;; Return the number of times that EVENT has happened.
763 (declaim (ftype (function (symbol) fixnum) event-count))
764 (defun event-count (name)
765 (event-info-count (event-info-or-lose name)))
767 ;;; Return the function that is called when Event happens. If this is
768 ;;; null, there is no action. The function is passed the node to which
769 ;;; the event happened, or NIL if there is no relevant node. This may
770 ;;; be set with SETF.
771 (declaim (ftype (function (symbol) (or function null)) event-action))
772 (defun event-action (name)
773 (event-info-action (event-info-or-lose name)))
774 (declaim (ftype (function (symbol (or function null)) (or function null))
776 (defun %set-event-action (name new-value)
777 (setf (event-info-action (event-info-or-lose name))
779 (defsetf event-action %set-event-action)
781 ;;; Return the non-negative integer which represents the level of
782 ;;; significance of the event Name. This is used to determine whether
783 ;;; to print a message when the event happens. This may be set with
785 (declaim (ftype (function (symbol) unsigned-byte) event-level))
786 (defun event-level (name)
787 (event-info-level (event-info-or-lose name)))
788 (declaim (ftype (function (symbol unsigned-byte) unsigned-byte) %set-event-level))
789 (defun %set-event-level (name new-value)
790 (setf (event-info-level (event-info-or-lose name))
792 (defsetf event-level %set-event-level)
794 ;;; Define a new kind of event. NAME is a symbol which names the event
795 ;;; and DESCRIPTION is a string which describes the event. Level
796 ;;; (default 0) is the level of significance associated with this
797 ;;; event; it is used to determine whether to print a Note when the
799 (defmacro defevent (name description &optional (level 0))
800 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
801 `(eval-when (:compile-toplevel :load-toplevel :execute)
803 (make-event-info :name ',name
804 :description ',description
807 (setf (gethash ',name *event-info*) ,var-name)
810 ;;; the lowest level of event that will print a note when it occurs
811 (declaim (type unsigned-byte *event-note-threshold*))
812 (defvar *event-note-threshold* 1)
814 ;;; Note that the event with the specified NAME has happened. NODE is
815 ;;; evaluated to determine the node to which the event happened.
816 (defmacro event (name &optional node)
817 ;; Increment the counter and do any action. Mumble about the event if
819 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
821 ;;; Print a listing of events and their counts, sorted by the count.
822 ;;; Events that happened fewer than Min-Count times will not be
823 ;;; printed. Stream is the stream to write to.
824 (declaim (ftype (function (&optional unsigned-byte stream) (values)) event-statistics))
825 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
827 (maphash (lambda (k v)
829 (when (>= (event-info-count v) min-count)
832 (dolist (event (sort (info) #'> :key #'event-info-count))
833 (format stream "~6D: ~A~%" (event-info-count event)
834 (event-info-description event)))
838 (declaim (ftype (function nil (values)) clear-event-statistics))
839 (defun clear-event-statistics ()
840 (maphash (lambda (k v)
842 (setf (event-info-count v) 0))
846 ;;;; functions on directly-linked lists (linked through specialized
847 ;;;; NEXT operations)
849 #!-sb-fluid (declaim (inline find-in position-in))
851 ;;; Find ELEMENT in a null-terminated LIST linked by the accessor
852 ;;; function NEXT. KEY, TEST and TEST-NOT are the same as for generic
853 ;;; sequence functions.
860 (test-not #'eql not-p))
861 (declare (type function next key test test-not))
862 (when (and test-p not-p)
863 (error "It's silly to supply both :TEST and :TEST-NOT arguments."))
865 (do ((current list (funcall next current)))
867 (unless (funcall test-not (funcall key current) element)
869 (do ((current list (funcall next current)))
871 (when (funcall test (funcall key current) element)
874 ;;; Return the position of ELEMENT (or NIL if absent) in a
875 ;;; null-terminated LIST linked by the accessor function NEXT. KEY,
876 ;;; TEST and TEST-NOT are the same as for generic sequence functions.
877 (defun position-in (next
883 (test-not #'eql not-p))
884 (declare (type function next key test test-not))
885 (when (and test-p not-p)
886 (error "It's silly to supply both :TEST and :TEST-NOT arguments."))
888 (do ((current list (funcall next current))
891 (unless (funcall test-not (funcall key current) element)
893 (do ((current list (funcall next current))
896 (when (funcall test (funcall key current) element)
900 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
901 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
903 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
904 ;;; arrangement, in order to get it to work in cross-compilation. This
905 ;;; duplication should be removed, perhaps by rewriting the macro in a more
906 ;;; cross-compiler-friendly way, or perhaps just by using some (MACROLET ((FROB
907 ;;; ..)) .. FROB .. FROB) form, or perhaps by completely eliminating this macro
908 ;;; and its partner PUSH-IN, but I don't want to do it now, because the system
909 ;;; isn't running yet, so it'd be too hard to check that my changes were
910 ;;; correct -- WHN 19990806
911 (def!macro deletef-in (next place item &environment env)
912 (multiple-value-bind (temps vals stores store access)
913 (get-setf-expansion place env)
915 (error "multiple store variables for ~S" place))
916 (let ((n-item (gensym))
920 `(let* (,@(mapcar #'list temps vals)
923 (if (eq ,n-place ,n-item)
924 (let ((,(first stores) (,next ,n-place)))
926 (do ((,n-prev ,n-place ,n-current)
927 (,n-current (,next ,n-place)
929 ((eq ,n-current ,n-item)
930 (setf (,next ,n-prev)
931 (,next ,n-current)))))
933 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
935 ;;; Push ITEM onto a list linked by the accessor function NEXT that is
938 ;;; KLUDGE: This is expanded out twice, by cut-and-paste, in a
939 ;;; (DEF!MACRO FOO (..) .. CL:GET-SETF-EXPANSION ..)
941 ;;; (SB!XC:DEFMACRO FOO (..) .. SB!XC:GET-SETF-EXPANSION ..)
942 ;;; arrangement, in order to get it to work in cross-compilation. This
943 ;;; duplication should be removed, perhaps by rewriting the macro in a more
944 ;;; cross-compiler-friendly way, or perhaps just by using some (MACROLET ((FROB
945 ;;; ..)) .. FROB .. FROB) form, or perhaps by completely eliminating this macro
946 ;;; and its partner DELETEF-IN, but I don't want to do it now, because the
947 ;;; system isn't running yet, so it'd be too hard to check that my changes were
948 ;;; correct -- WHN 19990806
949 (def!macro push-in (next item place &environment env)
950 (multiple-value-bind (temps vals stores store access)
951 (get-setf-expansion place env)
953 (error "multiple store variables for ~S" place))
954 `(let (,@(mapcar #'list temps vals)
955 (,(first stores) ,item))
956 (setf (,next ,(first stores)) ,access)
959 ;;; #+SB-XC-HOST SB!XC:DEFMACRO version is in late-macros.lisp. -- WHN 19990806
961 (defmacro position-or-lose (&rest args)
962 `(or (position ,@args)
963 (error "shouldn't happen?")))
965 ;;; user-definable compiler io syntax
967 ;;; We use WITH-SANE-IO-SYNTAX to provide safe defaults, and provide
968 ;;; *COMPILER-PRINT-VARIABLE-ALIST* for user customization.
969 (defvar *compiler-print-variable-alist* nil
971 "an association list describing new bindings for special variables
972 to be used by the compiler for error-reporting, etc. Eg.
974 ((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))
976 The variables in the CAR positions are bound to the values in the CDR
977 during the execution of some debug commands. When evaluating arbitrary
978 expressions in the debugger, the normal values of the printer control
979 variables are in effect.
981 Initially empty, *COMPILER-PRINT-VARIABLE-ALIST* is Typically used to
982 specify bindings for printer control variables.")
984 (defmacro with-compiler-io-syntax (&body forms)
985 `(with-sane-io-syntax
987 (nreverse (mapcar #'car *compiler-print-variable-alist*))
988 (nreverse (mapcar #'cdr *compiler-print-variable-alist*))
991 ;;; Like DESTRUCTURING-BIND, but generates a COMPILER-ERROR on failure
992 (defmacro compiler-destructuring-bind (lambda-list thing context
994 (let ((whole-name (gensym "WHOLE")))
995 (multiple-value-bind (body local-decls)
996 (parse-defmacro lambda-list whole-name body nil
999 :doc-string-allowed nil
1001 :error-fun 'compiler-error)
1002 `(let ((,whole-name ,thing))
1003 (declare (type list ,whole-name))