Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / macros.lisp
blob75a1bdf1a821b200f3e57e890d506d67c31b1e6c
1 ;;;; miscellaneous types and macros used in writing the compiler
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!C")
14 ;;;; source-hacking defining forms
16 ;;; Parse a DEFMACRO-style lambda-list, setting things up so that a
17 ;;; compiler error happens if the syntax is invalid.
18 ;;;
19 ;;; Define a function that converts a special form or other magical
20 ;;; thing into IR1. LAMBDA-LIST is a defmacro style lambda
21 ;;; list. START-VAR, NEXT-VAR and RESULT-VAR are bound to the start and
22 ;;; result continuations for the resulting IR1. KIND is the function
23 ;;; kind to associate with NAME.
24 ;;; FIXME - Translators which accept implicit PROGNs fail on improper
25 ;;; input, e.g. (LAMBDA (X) (CATCH X (BAZ) . 3))
26 ;;; This is because &REST is defined to allow a dotted tail in macros,
27 ;;; and we have no implementation-specific workaround to disallow it.
28 (defmacro def-ir1-translator (name (lambda-list start-var next-var result-var)
29 &body body)
30 (binding* ((fn-name (symbolicate "IR1-CONVERT-" name))
31 (whole-var (make-symbol "FORM"))
32 ((lambda-expr doc)
33 (make-macro-lambda nil lambda-list body :special-form name
34 :doc-string-allowed :external
35 :wrap-block nil)))
36 (declare (ignorable doc))
37 `(progn
38 (declaim (ftype (function (ctran ctran (or lvar null) t) (values))
39 ,fn-name))
40 (defun ,fn-name (,start-var ,next-var ,result-var ,whole-var)
41 (declare (ignorable ,start-var ,next-var ,result-var))
42 ;; The guard function is a closure, which can't have its lambda-list
43 ;; changed independently of other closures based on the same
44 ;; simple-fun. So change it in the ir1-translator since the actual
45 ;; lambda-list is not terribly useful.
46 (declare (lambda-list ,lambda-list))
47 (,lambda-expr ,whole-var *lexenv*)
48 (values))
49 #-sb-xc-host
50 (install-guard-function ',name '(:special ,name) ,(or #!+sb-doc doc))
51 ;; FIXME: Evidently "there can only be one!" -- we overwrite any
52 ;; other :IR1-CONVERT value. This deserves a warning, I think.
53 (setf (info :function :ir1-convert ',name) #',fn-name)
54 ;; FIXME: rename this to SPECIAL-OPERATOR, to update it to
55 ;; the 1990s?
56 (setf (info :function :kind ',name) :special-form)
57 ',name)))
59 ;;; (This is similar to DEF-IR1-TRANSLATOR, except that we pass if the
60 ;;; syntax is invalid.)
61 ;;;
62 ;;; Define a macro-like source-to-source transformation for the
63 ;;; function NAME. A source transform may "pass" by returning a
64 ;;; non-nil second value. If the transform passes, then the form is
65 ;;; converted as a normal function call. If the supplied arguments are
66 ;;; not compatible with the specified LAMBDA-LIST, then the transform
67 ;;; automatically passes.
68 ;;;
69 ;;; Source transforms may only be defined for functions. Source
70 ;;; transformation is not attempted if the function is declared
71 ;;; NOTINLINE. Source transforms should not examine their arguments.
72 ;;; A macro lambda list which destructures more than one level would
73 ;;; be legal but suspicious, as inner list parsing "examines" arguments.
74 ;;; If it matters how the function is used, then DEFTRANSFORM should
75 ;;; be used to define an IR1 transformation.
76 ;;;
77 ;;; If the desirability of the transformation depends on the current
78 ;;; OPTIMIZE parameters, then the POLICY macro should be used to
79 ;;; determine when to pass.
80 ;;;
81 ;;; Note that while a compiler-macro must deal with being invoked when its
82 ;;; whole form matches (FUNCALL <f> ...) so that it must skip over <f> to find
83 ;;; the arguments, a source-transform need not worry about that situation.
84 ;;; Any FUNCALL which is eligible for a source-transform calls the expander
85 ;;; with the form's head replaced by a compiler representation of the function.
86 ;;; Internalizing the name avoids semantic problems resulting from syntactic
87 ;;; substitution. One problem would be that a source-transform can exist for
88 ;;; a function named (SETF X), but ((SETF X) arg ...) would be bad syntax.
89 ;;; Even if we decided that it is ok within the compiler - just not in code
90 ;;; given to the compiler - the problem remains that changing (FUNCALL 'F x)
91 ;;; to (F x) is wrong when F also names a local functionoid.
92 ;;; Hence, either a #<GLOBAL-VAR> or #<DEFINED-FUN> appears in the form head.
93 ;;;
94 (defmacro define-source-transform (fun-name lambda-list &body body)
95 ;; FIXME: this is redundant with what will become MAKE-MACRO-LAMBDA
96 ;; except that it needs a "silently do nothing" mode, which may or may not
97 ;; be a generally exposed feature.
98 (binding*
99 (((forms decls) (parse-body body nil))
100 ((llks req opt rest keys aux env whole)
101 (parse-lambda-list
102 lambda-list
103 :accept
104 (logandc2 (logior (lambda-list-keyword-mask '&environment)
105 (lambda-list-keyword-mask 'destructuring-bind))
106 ;; Function lambda lists shouldn't take &BODY.
107 (lambda-list-keyword-mask '&body))
108 :context "a source transform"))
109 ((outer-decl inner-decls) (extract-var-decls decls (append env whole)))
110 ;; With general macros, the user can declare (&WHOLE W &ENVIRONMENT E ..)
111 ;; and then (DECLARE (IGNORE W E)) which is a problem if system code
112 ;; touches user variables. But this is all system code, so it's fine.
113 (lambda-whole (if whole (car whole) (make-symbol "FORM")))
114 (lambda-env (if env (car env) (make-symbol "ENV")))
115 (new-ll (if (or whole env) ; strip them out if present
116 (make-lambda-list llks nil req opt rest keys aux)
117 lambda-list)) ; otherwise use the original list
118 (args (make-symbol "ARGS")))
119 `(setf (info :function :source-transform ',fun-name)
120 (named-lambda (:source-transform ,fun-name)
121 (,lambda-whole ,lambda-env &aux (,args (cdr ,lambda-whole)))
122 ,@(if (not env) `((declare (ignore ,lambda-env))))
123 ,@outer-decl ; SPECIALs or something? I hope not.
124 (if ,(emit-ds-lambda-list-match args new-ll)
125 ;; The body can return 1 or 2 values, but consistently
126 ;; returning 2 values from the XEP is stylistically
127 ;; preferable, and produces shorter assembly code too.
128 (multiple-value-bind (call pass)
129 (binding* ,(expand-ds-bind new-ll args nil 'truly-the)
130 ,@inner-decls ,@forms)
131 (values call pass))
132 (values nil t))))))
134 ;;;; lambda-list parsing utilities
135 ;;;;
136 ;;;; IR1 transforms, optimizers and type inferencers need to be able
137 ;;;; to parse the IR1 representation of a function call using a
138 ;;;; standard function lambda-list.
140 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
142 ;;; Given a DEFTRANSFORM-style lambda-list, generate code that parses
143 ;;; the arguments of a combination with respect to that lambda-list.
144 ;;; NODE-VAR the variable that holds the combination node.
145 ;;; ERROR-FORM is a form which is evaluated when the syntax of the
146 ;;; supplied arguments is incorrect or a non-constant argument keyword
147 ;;; is supplied. Defaults and other gunk are ignored. The second value
148 ;;; is a list of all the arguments bound, which the caller should make
149 ;;; IGNORABLE if their only purpose is to make the syntax work.
150 (defun parse-deftransform (lambda-list node-var error-form)
151 (multiple-value-bind (llks req opt rest keys) (parse-lambda-list lambda-list)
152 (let* ((tail (make-symbol "ARGS"))
153 (dummies (make-gensym-list 2))
154 (all-dummies (cons tail dummies))
155 (keyp (ll-kwds-keyp llks))
156 ;; FIXME: the logic for keywordicating a keyword arg specifier
157 ;; is repeated in a as many places as there are calls to
158 ;; parse-lambda-just, and more.
159 (keys (mapcar (lambda (spec)
160 (multiple-value-bind (key var)
161 (parse-key-arg-spec spec)
162 (cons var key)))
163 keys))
164 final-mandatory-arg)
165 (collect ((binds))
166 (binds `(,tail (basic-combination-args ,node-var)))
167 ;; The way this code checks for mandatory args is to verify that
168 ;; the last positional arg is not null (it should be an LVAR).
169 ;; But somebody could pedantically declare IGNORE on the last arg
170 ;; so bind a dummy for it and then bind from the dummy.
171 (mapl (lambda (args)
172 (cond ((cdr args)
173 (binds `(,(car args) (pop ,tail))))
175 (setq final-mandatory-arg (pop dummies))
176 (binds `(,final-mandatory-arg (pop ,tail))
177 `(,(car args) ,final-mandatory-arg)))))
178 req)
179 ;; Optionals are pretty easy.
180 (dolist (arg opt)
181 (binds `(,(if (atom arg) arg (car arg)) (pop ,tail))))
182 ;; Now if min or max # of args is incorrect,
183 ;; or there are unacceptable keywords, bail out
184 (when (or req keyp (not rest))
185 (binds `(,(pop dummies) ; binding is for effect, not value
186 (unless (and ,@(if req `(,final-mandatory-arg))
187 ,@(if (and (not rest) (not keyp))
188 `((endp ,tail)))
189 ,@(if keyp
190 (if (ll-kwds-allowp llks)
191 `((check-key-args-constant ,tail))
192 `((check-transform-keys
193 ,tail ',(mapcar #'cdr keys))))))
194 ,error-form))))
195 (when rest
196 (binds `(,(car rest) ,tail)))
197 ;; Return list of bindings, the list of user-specified symbols,
198 ;; and the list of gensyms to be declared ignorable.
199 (values (append (binds)
200 (mapcar (lambda (k)
201 `(,(car k)
202 (find-keyword-lvar ,tail ',(cdr k))))
203 keys))
204 (sort (append (nset-difference (mapcar #'car (binds)) all-dummies)
205 (mapcar #'car keys))
206 #'string<)
207 (sort (intersection (mapcar #'car (binds)) (cdr all-dummies))
208 #'string<))))))
209 ) ; EVAL-WHEN
211 ;;;; DEFTRANSFORM
213 ;;; Define an IR1 transformation for NAME. An IR1 transformation
214 ;;; computes a lambda that replaces the function variable reference
215 ;;; for the call. A transform may pass (decide not to transform the
216 ;;; call) by calling the GIVE-UP-IR1-TRANSFORM function. LAMBDA-LIST
217 ;;; both determines how the current call is parsed and specifies the
218 ;;; LAMBDA-LIST for the resulting lambda.
220 ;;; We parse the call and bind each of the lambda-list variables to
221 ;;; the lvar which represents the value of the argument. When parsing
222 ;;; the call, we ignore the defaults, and always bind the variables
223 ;;; for unsupplied arguments to NIL. If a required argument is
224 ;;; missing, an unknown keyword is supplied, or an argument keyword is
225 ;;; not a constant, then the transform automatically passes. The
226 ;;; DECLARATIONS apply to the bindings made by DEFTRANSFORM at
227 ;;; transformation time, rather than to the variables of the resulting
228 ;;; lambda. Bound-but-not-referenced warnings are suppressed for the
229 ;;; lambda-list variables. The DOC-STRING is used when printing
230 ;;; efficiency notes about the defined transform.
232 ;;; Normally, the body evaluates to a form which becomes the body of
233 ;;; an automatically constructed lambda. We make LAMBDA-LIST the
234 ;;; lambda-list for the lambda, and automatically insert declarations
235 ;;; of the argument and result types. If the second value of the body
236 ;;; is non-null, then it is a list of declarations which are to be
237 ;;; inserted at the head of the lambda. Automatic lambda generation
238 ;;; may be inhibited by explicitly returning a lambda from the body.
240 ;;; The ARG-TYPES and RESULT-TYPE are used to create a function type
241 ;;; which the call must satisfy before transformation is attempted.
242 ;;; The function type specifier is constructed by wrapping (FUNCTION
243 ;;; ...) around these values, so the lack of a restriction may be
244 ;;; specified by omitting the argument or supplying *. The argument
245 ;;; syntax specified in the ARG-TYPES need not be the same as that in
246 ;;; the LAMBDA-LIST, but the transform will never happen if the
247 ;;; syntaxes can't be satisfied simultaneously. If there is an
248 ;;; existing transform for the same function that has the same type,
249 ;;; then it is replaced with the new definition.
251 ;;; These are the legal keyword options:
252 ;;; :RESULT - A variable which is bound to the result lvar.
253 ;;; :NODE - A variable which is bound to the combination node for the call.
254 ;;; :POLICY - A form which is supplied to the POLICY macro to determine
255 ;;; whether this transformation is appropriate. If the result
256 ;;; is false, then the transform automatically gives up.
257 ;;; :EVAL-NAME
258 ;;; - The name and argument/result types are actually forms to be
259 ;;; evaluated. Useful for getting closures that transform similar
260 ;;; functions.
261 ;;; :DEFUN-ONLY
262 ;;; - Don't actually instantiate a transform, instead just DEFUN
263 ;;; Name with the specified transform definition function. This
264 ;;; may be later instantiated with %DEFTRANSFORM.
265 ;;; :IMPORTANT
266 ;;; - If the transform fails and :IMPORTANT is
267 ;;; NIL, then never print an efficiency note.
268 ;;; :SLIGHTLY, then print a note if SPEED>=INHIBIT-WARNINGS.
269 ;;; T, then print a note if SPEED>INHIBIT-WARNINGS.
270 ;;; :SLIGHTLY is the default.
271 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
272 (result-type '*)
273 &key result policy node defun-only
274 eval-name (important :slightly))
275 &body body-decls-doc)
276 (declare (type (member nil :slightly t) important))
277 (when (and eval-name defun-only)
278 (error "can't specify both DEFUN-ONLY and EVAL-NAME"))
279 (multiple-value-bind (body decls doc) (parse-body body-decls-doc t)
280 (let ((n-node (or node (make-symbol "NODE")))
281 (n-decls (sb!xc:gensym))
282 (n-lambda (sb!xc:gensym)))
283 (multiple-value-bind (bindings vars)
284 (parse-deftransform lambda-list n-node
285 '(give-up-ir1-transform))
286 (let ((stuff
287 `((,n-node &aux ,@bindings
288 ,@(when result
289 `((,result (node-lvar ,n-node)))))
290 (declare (ignorable ,@(mapcar #'car bindings)))
291 (declare (lambda-list (node)))
292 ,@decls
293 ,@(and defun-only
295 `(,doc))
296 ,@(if policy
297 `((unless (policy ,n-node ,policy)
298 (give-up-ir1-transform))))
299 ;; What purpose does it serve to allow the transform's body
300 ;; to return decls as a second value? They would go in the
301 ;; right place if simply returned as part of the expression.
302 (multiple-value-bind (,n-lambda ,n-decls)
303 (progn ,@body)
304 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
305 ,n-lambda
306 `(lambda ,',lambda-list
307 (declare (ignorable ,@',vars))
308 ,@,n-decls
309 ,,n-lambda))))))
310 (if defun-only
311 `(defun ,name ,@stuff)
312 `(%deftransform
313 ,(if eval-name name `',name)
314 ,(if eval-name
315 ``(function ,,arg-types ,,result-type)
316 `'(function ,arg-types ,result-type))
317 (named-lambda ,(if eval-name "xform" `(deftransform ,name))
318 ,@stuff)
319 ,doc
320 ,important)))))))
322 (defmacro deftransforms (names (lambda-list &optional (arg-types '*)
323 (result-type '*)
324 &key result policy node (important :slightly))
325 &body body-decls-doc)
327 (let ((transform-name (symbolicate (car names) '-transform))
328 (type (list 'function arg-types result-type))
329 (doc (nth-value 2 (parse-body body-decls-doc t))))
330 `(progn
331 (deftransform ,transform-name
332 (,lambda-list ,arg-types ,result-type
333 :defun-only t
334 :result ,result :policy ,policy :node ,node)
335 ,@body-decls-doc)
336 ,@(loop for name in names
337 collect
338 `(%deftransform ',name ',type #',transform-name
339 ,doc
340 ,important)))))
342 ;;;; DEFKNOWN and DEFOPTIMIZER
344 ;;; This macro should be the way that all implementation independent
345 ;;; information about functions is made known to the compiler.
347 ;;; FIXME: The comment above suggests that perhaps some of my added
348 ;;; FTYPE declarations are in poor taste. Should I change my
349 ;;; declarations, or change the comment, or what?
351 ;;; FIXME: DEFKNOWN is needed only at build-the-system time. Figure
352 ;;; out some way to keep it from appearing in the target system.
354 ;;; Declare the function NAME to be a known function. We construct a
355 ;;; type specifier for the function by wrapping (FUNCTION ...) around
356 ;;; the ARG-TYPES and RESULT-TYPE. ATTRIBUTES is an unevaluated list
357 ;;; of boolean attributes of the function. See their description in
358 ;;; (!DEF-BOOLEAN-ATTRIBUTE IR1). NAME may also be a list of names, in
359 ;;; which case the same information is given to all the names. The
360 ;;; keywords specify the initial values for various optimizers that
361 ;;; the function might have.
362 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
363 &body keys)
364 #-sb-xc-host
365 (when (member 'unsafe attributes)
366 (style-warn "Ignoring legacy attribute UNSAFE. Replaced by its inverse: DX-SAFE.")
367 (setf attributes (remove 'unsafe attributes)))
368 (when (and (intersection attributes '(any call unwind))
369 (intersection attributes '(movable)))
370 (error "function cannot have both good and bad attributes: ~S" attributes))
372 (when (member 'any attributes)
373 (setq attributes (union '(unwind) attributes)))
374 (when (member 'flushable attributes)
375 (pushnew 'unsafely-flushable attributes))
376 (multiple-value-bind (foldable-call callable arg-types)
377 (make-foldable-call-check arg-types attributes)
378 `(%defknown ',(if (and (consp name)
379 (not (legal-fun-name-p name)))
380 name
381 (list name))
382 '(sfunction ,arg-types ,result-type)
383 (ir1-attributes ,@attributes)
384 (source-location)
385 :foldable-call-check ,foldable-call
386 :callable-check ,callable
387 ,@keys)))
389 (defun make-foldable-call-check (arg-types attributes)
390 (let ((call (member 'call attributes))
391 (fold (member 'foldable attributes)))
392 (if (not call)
393 (values nil nil arg-types)
394 (multiple-value-bind (llks required optional rest keys)
395 (parse-lambda-list
396 arg-types
397 :context :function-type
398 :accept (lambda-list-keyword-mask
399 '(&optional &rest &key &allow-other-keys))
400 :silent t)
401 (let (vars
402 call-vars
403 arg-count-specified)
404 (labels ((callable-p (x)
405 (member x '(callable function)))
406 (process-var (x &optional (name (gensym)))
407 (if (callable-p (if (consp x)
408 (car x)
410 (push (cons name
411 (cond ((consp x)
412 (setf arg-count-specified t)
413 (cadr x))))
414 call-vars)
415 (push name vars))
416 name)
417 (process-type (type)
418 (if (and (consp type)
419 (callable-p (car type)))
420 (car type)
421 type))
422 (callable-rest-p (x)
423 (and (consp x)
424 (callable-p (car x))
425 (eql (cadr x) '&rest))))
426 (let* (rest-var
427 (lambda-list
428 (cond ((find-if #'callable-rest-p required)
429 (setf rest-var (gensym))
430 `(,@(loop for var in required
431 collect (process-var var)
432 until (callable-rest-p var))
433 &rest ,rest-var))
435 `(,@(mapcar #'process-var required)
436 ,@(and optional
437 `(&optional ,@(mapcar #'process-var optional)))
438 ,@(and rest
439 `(&rest ,@(mapcar #'process-var rest)))
440 ,@(and (ll-kwds-keyp llks)
441 `(&key ,@(loop for (key type) in keys
442 for var = (gensym)
443 do (process-var type var)
444 collect `((,key ,var))))))))))
446 (assert call-vars)
447 (values
448 (and fold
449 `(lambda ,lambda-list
450 (declare (ignore ,@vars))
451 (and ,@(loop for (x) in call-vars
452 collect `(constant-fold-arg-p ,x)))))
453 (and arg-count-specified
454 `(lambda ,lambda-list
455 (declare (ignore ,@vars))
456 ,@(loop for (x . arg-count) in call-vars
457 when arg-count
458 collect (if (eq arg-count '&rest)
459 `(valid-callable-argument ,x (length ,rest-var))
460 `(valid-callable-argument ,x ,arg-count)))))
461 `(,@(mapcar #'process-type required)
462 ,@(and optional
463 `(&optional ,@(mapcar #'process-type optional)))
464 ,@(and (ll-kwds-restp llks)
465 `(&rest ,@rest))
466 ,@(and (ll-kwds-keyp llks)
467 `(&key
468 ,@(loop for (key type) in keys
469 collect `(,key ,(process-type type)))))
470 ,@(and (ll-kwds-allowp llks)
471 '(&allow-other-keys)))))))))))
473 ;;; Create a function which parses combination args according to WHAT
474 ;;; and LAMBDA-LIST, where WHAT is either a function name or a list
475 ;;; (FUN-NAME KIND) and does some KIND of optimization.
477 ;;; The FUN-NAME must name a known function. LAMBDA-LIST is used
478 ;;; to parse the arguments to the combination as in DEFTRANSFORM. If
479 ;;; the argument syntax is invalid or there are non-constant keys,
480 ;;; then we simply return NIL.
482 ;;; The function is DEFUN'ed as FUNCTION-KIND-OPTIMIZER. Possible
483 ;;; kinds are DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If
484 ;;; a symbol is specified instead of a (FUNCTION KIND) list, then we
485 ;;; just do a DEFUN with the symbol as its name, and don't do anything
486 ;;; with the definition. This is useful for creating optimizers to be
487 ;;; passed by name to DEFKNOWN.
489 ;;; If supplied, NODE-VAR is bound to the combination node being
490 ;;; optimized. If additional VARS are supplied, then they are used as
491 ;;; the rest of the optimizer function's lambda-list. LTN-ANNOTATE
492 ;;; methods are passed an additional POLICY argument, and IR2-CONVERT
493 ;;; methods are passed an additional IR2-BLOCK argument.
494 (defmacro defoptimizer (what (lambda-list
495 &optional (node (sb!xc:gensym) node-p)
496 &rest vars)
497 &body body)
498 (binding* ((name
499 (flet ((function-name (name)
500 (etypecase name
501 (symbol name)
502 ((cons (eql setf) (cons symbol null))
503 (symbolicate (car name) "-" (cadr name))))))
504 (if (symbolp what)
505 what
506 (symbolicate (function-name (first what))
507 "-" (second what) "-OPTIMIZER"))))
508 ((forms decls) (parse-body body nil))
509 ((var-decls more-decls) (extract-var-decls decls vars))
510 ;; In case the BODY declares IGNORE of the formal NODE var,
511 ;; we rebind it from N-NODE and never reference it from BINDS.
512 (n-node (make-symbol "NODE"))
513 ((binds lambda-vars gensyms)
514 (parse-deftransform lambda-list n-node
515 `(return-from ,name nil))))
516 (declare (ignore lambda-vars))
517 `(progn
518 ;; We can't stuff the BINDS as &AUX vars into the lambda list
519 ;; because there can be a RETURN-FROM in there.
520 (defun ,name (,n-node ,@vars)
521 ,@(if var-decls (list var-decls))
522 (let* (,@binds ,@(if node-p `((,node ,n-node))))
523 ;; Syntax requires naming NODE even if undesired if VARS
524 ;; are present, so in that case make NODE ignorable.
525 (declare (ignorable ,@(if (and vars node-p) `(,node))
526 ,@gensyms))
527 ,@more-decls ,@forms))
528 ,@(when (consp what)
529 `((setf (,(let ((*package* (symbol-package 'fun-info)))
530 (symbolicate "FUN-INFO-" (second what)))
531 (fun-info-or-lose ',(first what)))
532 #',name))))))
534 ;;;; IR groveling macros
536 ;;; Iterate over the blocks in a component, binding BLOCK-VAR to each
537 ;;; block in turn. The value of ENDS determines whether to iterate
538 ;;; over dummy head and tail blocks:
539 ;;; NIL -- Skip Head and Tail (the default)
540 ;;; :HEAD -- Do head but skip tail
541 ;;; :TAIL -- Do tail but skip head
542 ;;; :BOTH -- Do both head and tail
544 ;;; If supplied, RESULT-FORM is the value to return.
545 (defmacro do-blocks ((block-var component &optional ends result) &body body)
546 (unless (member ends '(nil :head :tail :both))
547 (error "losing ENDS value: ~S" ends))
548 (let ((n-component (gensym))
549 (n-tail (gensym)))
550 `(let* ((,n-component ,component)
551 (,n-tail ,(if (member ends '(:both :tail))
553 `(component-tail ,n-component))))
554 (do ((,block-var ,(if (member ends '(:both :head))
555 `(component-head ,n-component)
556 `(block-next (component-head ,n-component)))
557 (block-next ,block-var)))
558 ((eq ,block-var ,n-tail) ,result)
559 ,@body))))
560 ;;; like DO-BLOCKS, only iterating over the blocks in reverse order
561 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
562 (unless (member ends '(nil :head :tail :both))
563 (error "losing ENDS value: ~S" ends))
564 (let ((n-component (gensym))
565 (n-head (gensym)))
566 `(let* ((,n-component ,component)
567 (,n-head ,(if (member ends '(:both :head))
569 `(component-head ,n-component))))
570 (do ((,block-var ,(if (member ends '(:both :tail))
571 `(component-tail ,n-component)
572 `(block-prev (component-tail ,n-component)))
573 (block-prev ,block-var)))
574 ((eq ,block-var ,n-head) ,result)
575 ,@body))))
577 ;;; Iterate over the uses of LVAR, binding NODE to each one
578 ;;; successively.
579 (defmacro do-uses ((node-var lvar &optional result) &body body)
580 (with-unique-names (uses)
581 `(let ((,uses (lvar-uses ,lvar)))
582 (block nil
583 (flet ((do-1-use (,node-var)
584 ,@body))
585 (if (listp ,uses)
586 (dolist (node ,uses)
587 (do-1-use node))
588 (do-1-use ,uses)))
589 ,result))))
591 ;;; Iterate over the nodes in BLOCK, binding NODE-VAR to the each node
592 ;;; and LVAR-VAR to the node's LVAR. The only keyword option is
593 ;;; RESTART-P, which causes iteration to be restarted when a node is
594 ;;; deleted out from under us. (If not supplied, this is an error.)
596 ;;; In the forward case, we terminate when NODE does not have NEXT, so
597 ;;; that we do not have to worry about our termination condition being
598 ;;; changed when new code is added during the iteration. In the
599 ;;; backward case, we do NODE-PREV before evaluating the body so that
600 ;;; we can keep going when the current node is deleted.
602 ;;; When RESTART-P is supplied to DO-NODES, we start iterating over
603 ;;; again at the beginning of the block when we run into a ctran whose
604 ;;; block differs from the one we are trying to iterate over, either
605 ;;; because the block was split, or because a node was deleted out
606 ;;; from under us (hence its block is NIL.) If the block start is
607 ;;; deleted, we just punt. With RESTART-P, we are also more careful
608 ;;; about termination, re-indirecting the BLOCK-LAST each time.
609 (defmacro do-nodes ((node-var lvar-var block &key restart-p)
610 &body body)
611 (with-unique-names (n-block n-start)
612 `(do* ((,n-block ,block)
613 (,n-start (block-start ,n-block))
615 (,node-var (ctran-next ,n-start)
616 ,(if restart-p
617 `(let ((next (node-next ,node-var)))
618 (cond
619 ((not next)
620 (return))
621 ((eq (ctran-block next) ,n-block)
622 (ctran-next next))
624 (let ((start (block-start ,n-block)))
625 (unless (eq (ctran-kind start)
626 :block-start)
627 (return nil))
628 (ctran-next start)))))
629 `(acond ((node-next ,node-var)
630 (ctran-next it))
631 (t (return)))))
632 ,@(when lvar-var
633 `((,lvar-var (when (valued-node-p ,node-var)
634 (node-lvar ,node-var))
635 (when (valued-node-p ,node-var)
636 (node-lvar ,node-var))))))
637 (nil)
638 ,@body
639 ,@(when restart-p
640 `((when (block-delete-p ,n-block)
641 (return)))))))
643 ;;; Like DO-NODES, only iterating in reverse order. Should be careful
644 ;;; with block being split under us.
645 (defmacro do-nodes-backwards ((node-var lvar block &key restart-p) &body body)
646 (let ((n-block (gensym))
647 (n-prev (gensym)))
648 `(loop with ,n-block = ,block
649 for ,node-var = (block-last ,n-block) then
650 ,(if restart-p
651 `(if (eq ,n-block (ctran-block ,n-prev))
652 (ctran-use ,n-prev)
653 (block-last ,n-block))
654 `(ctran-use ,n-prev))
655 for ,n-prev = (when ,node-var (node-prev ,node-var))
656 and ,lvar = (when (and ,node-var (valued-node-p ,node-var))
657 (node-lvar ,node-var))
658 while ,(if restart-p
659 `(and ,node-var (not (block-to-be-deleted-p ,n-block)))
660 node-var)
661 do (progn
662 ,@body))))
664 (defmacro do-nodes-carefully ((node-var block) &body body)
665 (with-unique-names (n-block n-ctran)
666 `(loop with ,n-block = ,block
667 for ,n-ctran = (block-start ,n-block) then (node-next ,node-var)
668 for ,node-var = (and ,n-ctran (ctran-next ,n-ctran))
669 while ,node-var
670 do (progn ,@body))))
672 (defmacro do-nested-cleanups ((cleanup-var block &optional return-value)
673 &body body)
674 `(block nil
675 (map-nested-cleanups
676 (lambda (,cleanup-var) ,@body) ,block ,return-value)))
678 ;;; Bind the IR1 context variables to the values associated with NODE,
679 ;;; so that new, extra IR1 conversion related to NODE can be done
680 ;;; after the original conversion pass has finished.
681 (defmacro with-ir1-environment-from-node (node &rest forms)
682 `(flet ((closure-needing-ir1-environment-from-node ()
683 ,@forms))
684 (%with-ir1-environment-from-node
685 ,node
686 #'closure-needing-ir1-environment-from-node)))
688 (defmacro with-source-paths (&body forms)
689 (with-unique-names (source-paths)
690 `(let* ((,source-paths (make-hash-table :test 'eq))
691 (*source-paths* ,source-paths))
692 (unwind-protect
693 (progn ,@forms)
694 (clrhash ,source-paths)))))
696 ;;; Bind the hashtables used for keeping track of global variables,
697 ;;; functions, etc. Also establish condition handlers.
698 (defmacro with-ir1-namespace (&body forms)
699 `(let ((*free-vars* (make-hash-table :test 'eq))
700 (*free-funs* (make-hash-table :test 'equal))
701 (*constants* (make-hash-table :test 'equal)))
702 (unwind-protect
703 (progn ,@forms)
704 (clrhash *free-funs*)
705 (clrhash *free-vars*)
706 (clrhash *constants*))))
708 ;;; Look up NAME in the lexical environment namespace designated by
709 ;;; SLOT, returning the <value, T>, or <NIL, NIL> if no entry. The
710 ;;; :TEST keyword may be used to determine the name equality
711 ;;; predicate.
712 (defmacro lexenv-find (name slot &key test)
713 (once-only ((n-res `(assoc ,name (,(let ((*package* (symbol-package 'lexenv-funs)))
714 (symbolicate "LEXENV-" slot))
715 *lexenv*)
716 :test ,(or test '#'eq))))
717 `(if ,n-res
718 (values (cdr ,n-res) t)
719 (values nil nil))))
721 (defmacro with-component-last-block ((component block) &body body)
722 (with-unique-names (old-last-block)
723 (once-only ((component component)
724 (block block))
725 `(let ((,old-last-block (component-last-block ,component)))
726 (unwind-protect
727 (progn (setf (component-last-block ,component)
728 ,block)
729 ,@body)
730 (setf (component-last-block ,component)
731 ,old-last-block))))))
734 ;;;; the EVENT statistics/trace utility
736 ;;; FIXME: This seems to be useful for troubleshooting and
737 ;;; experimentation, not for ordinary use, so it should probably
738 ;;; become conditional on SB-SHOW.
740 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
742 (defstruct (event-info (:copier nil))
743 ;; The name of this event.
744 (name (missing-arg) :type symbol)
745 ;; The string rescribing this event.
746 (description (missing-arg) :type string)
747 ;; The name of the variable we stash this in.
748 (var (missing-arg) :type symbol)
749 ;; The number of times this event has happened.
750 (count 0 :type fixnum)
751 ;; The level of significance of this event.
752 (level (missing-arg) :type unsigned-byte)
753 ;; If true, a function that gets called with the node that the event
754 ;; happened to.
755 (action nil :type (or function null)))
757 ;;; A hashtable from event names to event-info structures.
758 (defvar *event-info* (make-hash-table :test 'eq))
760 ;;; Return the event info for Name or die trying.
761 (declaim (ftype (function (t) event-info) event-info-or-lose))
762 (defun event-info-or-lose (name)
763 (let ((res (gethash name *event-info*)))
764 (unless res
765 (error "~S is not the name of an event." name))
766 res))
768 ) ; EVAL-WHEN
770 ;;; Return the number of times that EVENT has happened.
771 (declaim (ftype (function (symbol) fixnum) event-count))
772 (defun event-count (name)
773 (event-info-count (event-info-or-lose name)))
775 ;;; Return the function that is called when Event happens. If this is
776 ;;; null, there is no action. The function is passed the node to which
777 ;;; the event happened, or NIL if there is no relevant node. This may
778 ;;; be set with SETF.
779 (declaim (ftype (function (symbol) (or function null)) event-action))
780 (defun event-action (name)
781 (event-info-action (event-info-or-lose name)))
782 (declaim (ftype (function (symbol (or function null)) (or function null))
783 %set-event-action))
784 (defun %set-event-action (name new-value)
785 (setf (event-info-action (event-info-or-lose name))
786 new-value))
787 (defsetf event-action %set-event-action)
789 ;;; Return the non-negative integer which represents the level of
790 ;;; significance of the event Name. This is used to determine whether
791 ;;; to print a message when the event happens. This may be set with
792 ;;; SETF.
793 (declaim (ftype (function (symbol) unsigned-byte) event-level))
794 (defun event-level (name)
795 (event-info-level (event-info-or-lose name)))
796 (declaim (ftype (function (symbol unsigned-byte) unsigned-byte) %set-event-level))
797 (defun %set-event-level (name new-value)
798 (setf (event-info-level (event-info-or-lose name))
799 new-value))
800 (defsetf event-level %set-event-level)
802 ;;; Define a new kind of event. NAME is a symbol which names the event
803 ;;; and DESCRIPTION is a string which describes the event. Level
804 ;;; (default 0) is the level of significance associated with this
805 ;;; event; it is used to determine whether to print a Note when the
806 ;;; event happens.
807 (defmacro defevent (name description &optional (level 0))
808 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
809 `(eval-when (:compile-toplevel :load-toplevel :execute)
810 (defvar ,var-name
811 (make-event-info :name ',name
812 :description ',description
813 :var ',var-name
814 :level ,level))
815 (setf (gethash ',name *event-info*) ,var-name)
816 ',name)))
818 ;;; the lowest level of event that will print a note when it occurs
819 (declaim (type unsigned-byte *event-note-threshold*))
820 (defvar *event-note-threshold* 1)
822 ;;; Note that the event with the specified NAME has happened. NODE is
823 ;;; evaluated to determine the node to which the event happened.
824 (defmacro event (name &optional node)
825 ;; Increment the counter and do any action. Mumble about the event if
826 ;; policy indicates.
827 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
829 ;;; Print a listing of events and their counts, sorted by the count.
830 ;;; Events that happened fewer than Min-Count times will not be
831 ;;; printed. Stream is the stream to write to.
832 (declaim (ftype (function (&optional unsigned-byte stream) (values)) event-statistics))
833 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
834 (collect ((info))
835 (maphash (lambda (k v)
836 (declare (ignore k))
837 (when (>= (event-info-count v) min-count)
838 (info v)))
839 *event-info*)
840 (dolist (event (sort (info) #'> :key #'event-info-count))
841 (format stream "~6D: ~A~%" (event-info-count event)
842 (event-info-description event)))
843 (values))
844 (values))
846 (declaim (ftype (function nil (values)) clear-event-statistics))
847 (defun clear-event-statistics ()
848 (maphash (lambda (k v)
849 (declare (ignore k))
850 (setf (event-info-count v) 0))
851 *event-info*)
852 (values))
854 ;;;; functions on directly-linked lists (linked through specialized
855 ;;;; NEXT operations)
857 #!-sb-fluid (declaim (inline find-in position-in))
859 ;;; Find ELEMENT in a null-terminated LIST linked by the accessor
860 ;;; function NEXT. KEY and TEST are the same as for generic sequence functions.
861 (defun find-in (next
862 element
863 list
864 &key
865 (key #'identity)
866 (test #'eql))
867 (declare (type function next key test))
868 (do ((current list (funcall next current)))
869 ((null current) nil)
870 (when (funcall test (funcall key current) element)
871 (return current))))
873 ;;; Return the position of ELEMENT (or NIL if absent) in a
874 ;;; null-terminated LIST linked by the accessor function NEXT.
875 ;;; KEY and TEST are the same as for generic sequence functions.
876 (defun position-in (next
877 element
878 list
879 &key
880 (key #'identity)
881 (test #'eql))
882 (declare (type function next key test))
883 (do ((current list (funcall next current))
884 (i 0 (1+ i)))
885 ((null current) nil)
886 (when (funcall test (funcall key current) element)
887 (return i))))
889 (defmacro deletef-in (next place item &environment env)
890 (multiple-value-bind (temps vals stores store access)
891 (#+sb-xc sb!xc:get-setf-expansion #-sb-xc get-setf-expansion place env)
892 (when (cdr stores)
893 (error "multiple store variables for ~S" place))
894 (let ((n-item (gensym))
895 (n-place (gensym))
896 (n-current (gensym))
897 (n-prev (gensym)))
898 `(let* (,@(mapcar #'list temps vals)
899 (,n-place ,access)
900 (,n-item ,item))
901 (if (eq ,n-place ,n-item)
902 (let ((,(first stores) (,next ,n-place)))
903 ,store)
904 (do ((,n-prev ,n-place ,n-current)
905 (,n-current (,next ,n-place)
906 (,next ,n-current)))
907 ((eq ,n-current ,n-item)
908 (setf (,next ,n-prev)
909 (,next ,n-current)))))
910 (values)))))
912 ;;; Push ITEM onto a list linked by the accessor function NEXT that is
913 ;;; stored in PLACE.
915 (defmacro push-in (next item place &environment env)
916 (multiple-value-bind (temps vals stores store access)
917 (#+sb-xc sb!xc:get-setf-expansion #-sb-xc get-setf-expansion place env)
918 (when (cdr stores)
919 (error "multiple store variables for ~S" place))
920 `(let (,@(mapcar #'list temps vals)
921 (,(first stores) ,item))
922 (setf (,next ,(first stores)) ,access)
923 ,store
924 (values))))
926 (defmacro position-or-lose (&rest args)
927 `(or (position ,@args)
928 (error "shouldn't happen?")))
930 ;;; user-definable compiler io syntax
932 ;;; We use WITH-SANE-IO-SYNTAX to provide safe defaults, and provide
933 ;;; *COMPILER-PRINT-VARIABLE-ALIST* for user customization.
934 (defvar *compiler-print-variable-alist* nil
935 #!+sb-doc
936 "an association list describing new bindings for special variables
937 to be used by the compiler for error-reporting, etc. Eg.
939 ((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))
941 The variables in the CAR positions are bound to the values in the CDR
942 during the execution of some debug commands. When evaluating arbitrary
943 expressions in the debugger, the normal values of the printer control
944 variables are in effect.
946 Initially empty, *COMPILER-PRINT-VARIABLE-ALIST* is Typically used to
947 specify bindings for printer control variables.")
949 (defmacro with-compiler-io-syntax (&body forms)
950 `(with-sane-io-syntax
951 (progv
952 (nreverse (mapcar #'car *compiler-print-variable-alist*))
953 (nreverse (mapcar #'cdr *compiler-print-variable-alist*))
954 ,@forms)))