Replace %CODE-ENTRY-POINTS with an array, remove %SIMPLE-FUN-NEXT.
[sbcl.git] / src / compiler / macros.lisp
blob2bd81bfa1b482e33743cad1b2a182ef6a4e98d7f
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 (not ,(emit-ds-lambda-list-match args new-ll))
125 (values nil t)
126 ;; The body can return 1 or 2 values, but consistently
127 ;; returning 2 values from the XEP is stylistically
128 ;; preferable, and produces shorter assembly code too.
129 (multiple-value-bind (call pass)
130 (binding* ,(expand-ds-bind new-ll args nil 'truly-the)
131 ,@inner-decls
132 (block ,(fun-name-block-name fun-name) ,@forms))
133 (values call pass)))))))
135 ;;;; lambda-list parsing utilities
136 ;;;;
137 ;;;; IR1 transforms, optimizers and type inferencers need to be able
138 ;;;; to parse the IR1 representation of a function call using a
139 ;;;; standard function lambda-list.
141 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
143 ;;; Given a DEFTRANSFORM-style lambda-list, generate code that parses
144 ;;; the arguments of a combination with respect to that lambda-list.
145 ;;; NODE-VAR the variable that holds the combination node.
146 ;;; ERROR-FORM is a form which is evaluated when the syntax of the
147 ;;; supplied arguments is incorrect or a non-constant argument keyword
148 ;;; is supplied. Defaults and other gunk are ignored. The second value
149 ;;; is a list of all the arguments bound, which the caller should make
150 ;;; IGNORABLE if their only purpose is to make the syntax work.
151 (defun parse-deftransform (lambda-list node-var error-form)
152 (multiple-value-bind (llks req opt rest keys) (parse-lambda-list lambda-list)
153 (let* ((tail (make-symbol "ARGS"))
154 (dummies (make-gensym-list 2))
155 (all-dummies (cons tail dummies))
156 (keyp (ll-kwds-keyp llks))
157 ;; FIXME: the logic for keywordicating a keyword arg specifier
158 ;; is repeated in a as many places as there are calls to
159 ;; parse-lambda-just, and more.
160 (keys (mapcar (lambda (spec)
161 (multiple-value-bind (key var)
162 (parse-key-arg-spec spec)
163 (cons var key)))
164 keys))
165 final-mandatory-arg)
166 (collect ((binds))
167 (binds `(,tail (basic-combination-args ,node-var)))
168 ;; The way this code checks for mandatory args is to verify that
169 ;; the last positional arg is not null (it should be an LVAR).
170 ;; But somebody could pedantically declare IGNORE on the last arg
171 ;; so bind a dummy for it and then bind from the dummy.
172 (mapl (lambda (args)
173 (cond ((cdr args)
174 (binds `(,(car args) (pop ,tail))))
176 (setq final-mandatory-arg (pop dummies))
177 (binds `(,final-mandatory-arg (pop ,tail))
178 `(,(car args) ,final-mandatory-arg)))))
179 req)
180 ;; Optionals are pretty easy.
181 (dolist (arg opt)
182 (binds `(,(if (atom arg) arg (car arg)) (pop ,tail))))
183 ;; Now if min or max # of args is incorrect,
184 ;; or there are unacceptable keywords, bail out
185 (when (or req keyp (not rest))
186 (binds `(,(pop dummies) ; binding is for effect, not value
187 (unless (and ,@(if req `(,final-mandatory-arg))
188 ,@(if (and (not rest) (not keyp))
189 `((endp ,tail)))
190 ,@(if keyp
191 (if (ll-kwds-allowp llks)
192 `((check-key-args-constant ,tail))
193 `((check-transform-keys
194 ,tail ',(mapcar #'cdr keys))))))
195 ,error-form))))
196 (when rest
197 (binds `(,(car rest) ,tail)))
198 ;; Return list of bindings, the list of user-specified symbols,
199 ;; and the list of gensyms to be declared ignorable.
200 (values (append (binds)
201 (mapcar (lambda (k)
202 `(,(car k)
203 (find-keyword-lvar ,tail ',(cdr k))))
204 keys))
205 (sort (append (nset-difference (mapcar #'car (binds)) all-dummies)
206 (mapcar #'car keys))
207 #'string<)
208 (sort (intersection (mapcar #'car (binds)) (cdr all-dummies))
209 #'string<))))))
210 ) ; EVAL-WHEN
212 ;;;; DEFTRANSFORM
214 ;;; Define an IR1 transformation for NAME. An IR1 transformation
215 ;;; computes a lambda that replaces the function variable reference
216 ;;; for the call. A transform may pass (decide not to transform the
217 ;;; call) by calling the GIVE-UP-IR1-TRANSFORM function. LAMBDA-LIST
218 ;;; both determines how the current call is parsed and specifies the
219 ;;; LAMBDA-LIST for the resulting lambda.
221 ;;; We parse the call and bind each of the lambda-list variables to
222 ;;; the lvar which represents the value of the argument. When parsing
223 ;;; the call, we ignore the defaults, and always bind the variables
224 ;;; for unsupplied arguments to NIL. If a required argument is
225 ;;; missing, an unknown keyword is supplied, or an argument keyword is
226 ;;; not a constant, then the transform automatically passes. The
227 ;;; DECLARATIONS apply to the bindings made by DEFTRANSFORM at
228 ;;; transformation time, rather than to the variables of the resulting
229 ;;; lambda. Bound-but-not-referenced warnings are suppressed for the
230 ;;; lambda-list variables. The DOC-STRING is used when printing
231 ;;; efficiency notes about the defined transform.
233 ;;; Normally, the body evaluates to a form which becomes the body of
234 ;;; an automatically constructed lambda. We make LAMBDA-LIST the
235 ;;; lambda-list for the lambda, and automatically insert declarations
236 ;;; of the argument and result types. If the second value of the body
237 ;;; is non-null, then it is a list of declarations which are to be
238 ;;; inserted at the head of the lambda. Automatic lambda generation
239 ;;; may be inhibited by explicitly returning a lambda from the body.
241 ;;; The ARG-TYPES and RESULT-TYPE are used to create a function type
242 ;;; which the call must satisfy before transformation is attempted.
243 ;;; The function type specifier is constructed by wrapping (FUNCTION
244 ;;; ...) around these values, so the lack of a restriction may be
245 ;;; specified by omitting the argument or supplying *. The argument
246 ;;; syntax specified in the ARG-TYPES need not be the same as that in
247 ;;; the LAMBDA-LIST, but the transform will never happen if the
248 ;;; syntaxes can't be satisfied simultaneously. If there is an
249 ;;; existing transform for the same function that has the same type,
250 ;;; then it is replaced with the new definition.
252 ;;; These are the legal keyword options:
253 ;;; :RESULT - A variable which is bound to the result lvar.
254 ;;; :NODE - A variable which is bound to the combination node for the call.
255 ;;; :POLICY - A form which is supplied to the POLICY macro to determine
256 ;;; whether this transformation is appropriate. If the result
257 ;;; is false, then the transform automatically gives up.
258 ;;; :EVAL-NAME
259 ;;; - The name and argument/result types are actually forms to be
260 ;;; evaluated. Useful for getting closures that transform similar
261 ;;; functions.
262 ;;; :DEFUN-ONLY
263 ;;; - Don't actually instantiate a transform, instead just DEFUN
264 ;;; Name with the specified transform definition function. This
265 ;;; may be later instantiated with %DEFTRANSFORM.
266 ;;; :IMPORTANT
267 ;;; - If the transform fails and :IMPORTANT is
268 ;;; NIL, then never print an efficiency note.
269 ;;; :SLIGHTLY, then print a note if SPEED>=INHIBIT-WARNINGS.
270 ;;; T, then print a note if SPEED>INHIBIT-WARNINGS.
271 ;;; :SLIGHTLY is the default.
272 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
273 (result-type '*)
274 &key result policy node defun-only
275 eval-name (important :slightly))
276 &body body-decls-doc)
277 (declare (type (member nil :slightly t) important))
278 (when (and eval-name defun-only)
279 (error "can't specify both DEFUN-ONLY and EVAL-NAME"))
280 (multiple-value-bind (body decls doc) (parse-body body-decls-doc t)
281 (let ((n-node (or node (make-symbol "NODE")))
282 (n-decls (sb!xc:gensym))
283 (n-lambda (sb!xc:gensym)))
284 (multiple-value-bind (bindings vars)
285 (parse-deftransform lambda-list n-node
286 '(give-up-ir1-transform))
287 (let ((stuff
288 `((,n-node &aux ,@bindings
289 ,@(when result
290 `((,result (node-lvar ,n-node)))))
291 (declare (ignorable ,@(mapcar #'car bindings)))
292 (declare (lambda-list (node)))
293 ,@decls
294 ,@(and defun-only
296 `(,doc))
297 ,@(if policy
298 `((unless (policy ,n-node ,policy)
299 (give-up-ir1-transform))))
300 ;; What purpose does it serve to allow the transform's body
301 ;; to return decls as a second value? They would go in the
302 ;; right place if simply returned as part of the expression.
303 (multiple-value-bind (,n-lambda ,n-decls)
304 (progn ,@body)
305 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
306 ,n-lambda
307 `(lambda ,',lambda-list
308 (declare (ignorable ,@',vars))
309 ,@,n-decls
310 ,,n-lambda))))))
311 (if defun-only
312 `(defun ,name ,@stuff)
313 `(%deftransform
314 ,(if eval-name name `',name)
315 ,(if eval-name
316 ``(function ,,arg-types ,,result-type)
317 `'(function ,arg-types ,result-type))
318 (named-lambda ,(if eval-name "xform" `(deftransform ,name))
319 ,@stuff)
320 ,doc
321 ,important)))))))
323 (defmacro deftransforms (names (lambda-list &optional (arg-types '*)
324 (result-type '*)
325 &key result policy node (important :slightly))
326 &body body-decls-doc)
328 (let ((transform-name (symbolicate (car names) '-transform))
329 (type (list 'function arg-types result-type))
330 (doc (nth-value 2 (parse-body body-decls-doc t))))
331 `(progn
332 (deftransform ,transform-name
333 (,lambda-list ,arg-types ,result-type
334 :defun-only t
335 :result ,result :policy ,policy :node ,node)
336 ,@body-decls-doc)
337 ,@(loop for name in names
338 collect
339 `(%deftransform ',name ',type #',transform-name
340 ,doc
341 ,important)))))
343 ;;;; DEFKNOWN and DEFOPTIMIZER
345 ;;; This macro should be the way that all implementation independent
346 ;;; information about functions is made known to the compiler.
348 ;;; FIXME: The comment above suggests that perhaps some of my added
349 ;;; FTYPE declarations are in poor taste. Should I change my
350 ;;; declarations, or change the comment, or what?
352 ;;; FIXME: DEFKNOWN is needed only at build-the-system time. Figure
353 ;;; out some way to keep it from appearing in the target system.
355 ;;; Declare the function NAME to be a known function. We construct a
356 ;;; type specifier for the function by wrapping (FUNCTION ...) around
357 ;;; the ARG-TYPES and RESULT-TYPE. ATTRIBUTES is an unevaluated list
358 ;;; of boolean attributes of the function. See their description in
359 ;;; (!DEF-BOOLEAN-ATTRIBUTE IR1). NAME may also be a list of names, in
360 ;;; which case the same information is given to all the names. The
361 ;;; keywords specify the initial values for various optimizers that
362 ;;; the function might have.
363 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
364 &body keys)
365 #-sb-xc-host
366 (when (member 'unsafe attributes)
367 (style-warn "Ignoring legacy attribute UNSAFE. Replaced by its inverse: DX-SAFE.")
368 (setf attributes (remove 'unsafe attributes)))
369 (when (and (intersection attributes '(any call unwind))
370 (intersection attributes '(movable)))
371 (error "function cannot have both good and bad attributes: ~S" attributes))
373 (when (member 'any attributes)
374 (setq attributes (union '(unwind) attributes)))
375 (when (member 'flushable attributes)
376 (pushnew 'unsafely-flushable attributes))
377 (multiple-value-bind (foldable-call callable arg-types)
378 (make-foldable-call-check arg-types attributes)
379 `(%defknown ',(if (and (consp name)
380 (not (legal-fun-name-p name)))
381 name
382 (list name))
383 '(sfunction ,arg-types ,result-type)
384 (ir1-attributes ,@attributes)
385 (source-location)
386 :foldable-call-check ,foldable-call
387 :callable-check ,callable
388 ,@keys)))
390 (defun make-foldable-call-check (arg-types attributes)
391 (let ((call (member 'call attributes))
392 (fold (member 'foldable attributes)))
393 (if (not call)
394 (values nil nil arg-types)
395 (multiple-value-bind (llks required optional rest keys)
396 (parse-lambda-list
397 arg-types
398 :context :function-type
399 :accept (lambda-list-keyword-mask
400 '(&optional &rest &key &allow-other-keys))
401 :silent t)
402 (let (vars
403 call-vars
404 arg-count-specified)
405 (labels ((callable-p (x)
406 (member x '(callable function)))
407 (process-var (x &optional (name (gensym)))
408 (if (callable-p (if (consp x)
409 (car x)
411 (push (cons name
412 (cond ((consp x)
413 (setf arg-count-specified t)
414 (cadr x))))
415 call-vars)
416 (push name vars))
417 name)
418 (process-type (type)
419 (if (and (consp type)
420 (callable-p (car type)))
421 (car type)
422 type))
423 (callable-rest-p (x)
424 (and (consp x)
425 (callable-p (car x))
426 (eql (cadr x) '&rest))))
427 (let* (rest-var
428 (lambda-list
429 (cond ((find-if #'callable-rest-p required)
430 (setf rest-var (gensym))
431 `(,@(loop for var in required
432 collect (process-var var)
433 until (callable-rest-p var))
434 &rest ,rest-var))
436 `(,@(mapcar #'process-var required)
437 ,@(and optional
438 `(&optional ,@(mapcar #'process-var optional)))
439 ,@(and rest
440 `(&rest ,@(mapcar #'process-var rest)))
441 ,@(and (ll-kwds-keyp llks)
442 `(&key ,@(loop for (key type) in keys
443 for var = (gensym)
444 do (process-var type var)
445 collect `((,key ,var))))))))))
447 (assert call-vars)
448 (values
449 (and fold
450 `(lambda ,lambda-list
451 (declare (ignore ,@vars))
452 (and ,@(loop for (x) in call-vars
453 collect `(constant-fold-arg-p ,x)))))
454 (and arg-count-specified
455 `(lambda ,lambda-list
456 (declare (ignore ,@vars))
457 ,@(loop for (x . arg-count) in call-vars
458 when arg-count
459 collect (if (eq arg-count '&rest)
460 `(valid-callable-argument ,x (length ,rest-var))
461 `(valid-callable-argument ,x ,arg-count)))))
462 `(,@(mapcar #'process-type required)
463 ,@(and optional
464 `(&optional ,@(mapcar #'process-type optional)))
465 ,@(and (ll-kwds-restp llks)
466 `(&rest ,@rest))
467 ,@(and (ll-kwds-keyp llks)
468 `(&key
469 ,@(loop for (key type) in keys
470 collect `(,key ,(process-type type)))))
471 ,@(and (ll-kwds-allowp llks)
472 '(&allow-other-keys)))))))))))
474 ;;; Create a function which parses combination args according to WHAT
475 ;;; and LAMBDA-LIST, where WHAT is either a function name or a list
476 ;;; (FUN-NAME KIND) and does some KIND of optimization.
478 ;;; The FUN-NAME must name a known function. LAMBDA-LIST is used
479 ;;; to parse the arguments to the combination as in DEFTRANSFORM. If
480 ;;; the argument syntax is invalid or there are non-constant keys,
481 ;;; then we simply return NIL.
483 ;;; The function is DEFUN'ed as FUNCTION-KIND-OPTIMIZER. Possible
484 ;;; kinds are DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If
485 ;;; a symbol is specified instead of a (FUNCTION KIND) list, then we
486 ;;; just do a DEFUN with the symbol as its name, and don't do anything
487 ;;; with the definition. This is useful for creating optimizers to be
488 ;;; passed by name to DEFKNOWN.
490 ;;; If supplied, NODE-VAR is bound to the combination node being
491 ;;; optimized. If additional VARS are supplied, then they are used as
492 ;;; the rest of the optimizer function's lambda-list. LTN-ANNOTATE
493 ;;; methods are passed an additional POLICY argument, and IR2-CONVERT
494 ;;; methods are passed an additional IR2-BLOCK argument.
495 (defmacro defoptimizer (what (lambda-list
496 &optional (node (sb!xc:gensym) node-p)
497 &rest vars)
498 &body body)
499 (binding* ((name
500 (flet ((function-name (name)
501 (etypecase name
502 (symbol name)
503 ((cons (eql setf) (cons symbol null))
504 (symbolicate (car name) "-" (cadr name))))))
505 (if (symbolp what)
506 what
507 (symbolicate (function-name (first what))
508 "-" (second what) "-OPTIMIZER"))))
509 ((forms decls) (parse-body body nil))
510 ((var-decls more-decls) (extract-var-decls decls vars))
511 ;; In case the BODY declares IGNORE of the formal NODE var,
512 ;; we rebind it from N-NODE and never reference it from BINDS.
513 (n-node (make-symbol "NODE"))
514 ((binds lambda-vars gensyms)
515 (parse-deftransform lambda-list n-node
516 `(return-from ,name nil))))
517 (declare (ignore lambda-vars))
518 `(progn
519 ;; We can't stuff the BINDS as &AUX vars into the lambda list
520 ;; because there can be a RETURN-FROM in there.
521 (defun ,name (,n-node ,@vars)
522 ,@(if var-decls (list var-decls))
523 (let* (,@binds ,@(if node-p `((,node ,n-node))))
524 ;; Syntax requires naming NODE even if undesired if VARS
525 ;; are present, so in that case make NODE ignorable.
526 (declare (ignorable ,@(if (and vars node-p) `(,node))
527 ,@gensyms))
528 ,@more-decls ,@forms))
529 ,@(when (consp what)
530 `((setf (,(let ((*package* (symbol-package 'fun-info)))
531 (symbolicate "FUN-INFO-" (second what)))
532 (fun-info-or-lose ',(first what)))
533 #',name))))))
535 ;;;; IR groveling macros
537 ;;; Iterate over the blocks in a component, binding BLOCK-VAR to each
538 ;;; block in turn. The value of ENDS determines whether to iterate
539 ;;; over dummy head and tail blocks:
540 ;;; NIL -- Skip Head and Tail (the default)
541 ;;; :HEAD -- Do head but skip tail
542 ;;; :TAIL -- Do tail but skip head
543 ;;; :BOTH -- Do both head and tail
545 ;;; If supplied, RESULT-FORM is the value to return.
546 (defmacro do-blocks ((block-var component &optional ends result) &body body)
547 (unless (member ends '(nil :head :tail :both))
548 (error "losing ENDS value: ~S" ends))
549 (let ((n-component (gensym))
550 (n-tail (gensym)))
551 `(let* ((,n-component ,component)
552 (,n-tail ,(if (member ends '(:both :tail))
554 `(component-tail ,n-component))))
555 (do ((,block-var ,(if (member ends '(:both :head))
556 `(component-head ,n-component)
557 `(block-next (component-head ,n-component)))
558 (block-next ,block-var)))
559 ((eq ,block-var ,n-tail) ,result)
560 ,@body))))
561 ;;; like DO-BLOCKS, only iterating over the blocks in reverse order
562 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
563 (unless (member ends '(nil :head :tail :both))
564 (error "losing ENDS value: ~S" ends))
565 (let ((n-component (gensym))
566 (n-head (gensym)))
567 `(let* ((,n-component ,component)
568 (,n-head ,(if (member ends '(:both :head))
570 `(component-head ,n-component))))
571 (do ((,block-var ,(if (member ends '(:both :tail))
572 `(component-tail ,n-component)
573 `(block-prev (component-tail ,n-component)))
574 (block-prev ,block-var)))
575 ((eq ,block-var ,n-head) ,result)
576 ,@body))))
578 ;;; Iterate over the uses of LVAR, binding NODE to each one
579 ;;; successively.
580 (defmacro do-uses ((node-var lvar &optional result) &body body)
581 (with-unique-names (uses)
582 `(let ((,uses (lvar-uses ,lvar)))
583 (block nil
584 (flet ((do-1-use (,node-var)
585 ,@body))
586 (if (listp ,uses)
587 (dolist (node ,uses)
588 (do-1-use node))
589 (do-1-use ,uses)))
590 ,result))))
592 ;;; Iterate over the nodes in BLOCK, binding NODE-VAR to the each node
593 ;;; and LVAR-VAR to the node's LVAR. The only keyword option is
594 ;;; RESTART-P, which causes iteration to be restarted when a node is
595 ;;; deleted out from under us. (If not supplied, this is an error.)
597 ;;; In the forward case, we terminate when NODE does not have NEXT, so
598 ;;; that we do not have to worry about our termination condition being
599 ;;; changed when new code is added during the iteration. In the
600 ;;; backward case, we do NODE-PREV before evaluating the body so that
601 ;;; we can keep going when the current node is deleted.
603 ;;; When RESTART-P is supplied to DO-NODES, we start iterating over
604 ;;; again at the beginning of the block when we run into a ctran whose
605 ;;; block differs from the one we are trying to iterate over, either
606 ;;; because the block was split, or because a node was deleted out
607 ;;; from under us (hence its block is NIL.) If the block start is
608 ;;; deleted, we just punt. With RESTART-P, we are also more careful
609 ;;; about termination, re-indirecting the BLOCK-LAST each time.
610 (defmacro do-nodes ((node-var lvar-var block &key restart-p)
611 &body body)
612 (with-unique-names (n-block n-start)
613 `(do* ((,n-block ,block)
614 (,n-start (block-start ,n-block))
616 (,node-var (ctran-next ,n-start)
617 ,(if restart-p
618 `(let ((next (node-next ,node-var)))
619 (cond
620 ((not next)
621 (return))
622 ((eq (ctran-block next) ,n-block)
623 (ctran-next next))
625 (let ((start (block-start ,n-block)))
626 (unless (eq (ctran-kind start)
627 :block-start)
628 (return nil))
629 (ctran-next start)))))
630 `(acond ((node-next ,node-var)
631 (ctran-next it))
632 (t (return)))))
633 ,@(when lvar-var
634 `((,lvar-var (when (valued-node-p ,node-var)
635 (node-lvar ,node-var))
636 (when (valued-node-p ,node-var)
637 (node-lvar ,node-var))))))
638 (nil)
639 ,@body
640 ,@(when restart-p
641 `((when (block-delete-p ,n-block)
642 (return)))))))
644 ;;; Like DO-NODES, only iterating in reverse order. Should be careful
645 ;;; with block being split under us.
646 (defmacro do-nodes-backwards ((node-var lvar block &key restart-p) &body body)
647 (let ((n-block (gensym))
648 (n-prev (gensym)))
649 `(loop with ,n-block = ,block
650 for ,node-var = (block-last ,n-block) then
651 ,(if restart-p
652 `(if (eq ,n-block (ctran-block ,n-prev))
653 (ctran-use ,n-prev)
654 (block-last ,n-block))
655 `(ctran-use ,n-prev))
656 for ,n-prev = (when ,node-var (node-prev ,node-var))
657 and ,lvar = (when (and ,node-var (valued-node-p ,node-var))
658 (node-lvar ,node-var))
659 while ,(if restart-p
660 `(and ,node-var (not (block-to-be-deleted-p ,n-block)))
661 node-var)
662 do (progn
663 ,@body))))
665 (defmacro do-nodes-carefully ((node-var block) &body body)
666 (with-unique-names (n-block n-ctran)
667 `(loop with ,n-block = ,block
668 for ,n-ctran = (block-start ,n-block) then (node-next ,node-var)
669 for ,node-var = (and ,n-ctran (ctran-next ,n-ctran))
670 while ,node-var
671 do (progn ,@body))))
673 (defmacro do-nested-cleanups ((cleanup-var block &optional return-value)
674 &body body)
675 `(block nil
676 (map-nested-cleanups
677 (lambda (,cleanup-var) ,@body) ,block ,return-value)))
679 ;;; Bind the IR1 context variables to the values associated with NODE,
680 ;;; so that new, extra IR1 conversion related to NODE can be done
681 ;;; after the original conversion pass has finished.
682 (defmacro with-ir1-environment-from-node (node &rest forms)
683 `(flet ((closure-needing-ir1-environment-from-node ()
684 ,@forms))
685 (%with-ir1-environment-from-node
686 ,node
687 #'closure-needing-ir1-environment-from-node)))
689 (defmacro with-source-paths (&body forms)
690 (with-unique-names (source-paths)
691 `(let* ((,source-paths (make-hash-table :test 'eq))
692 (*source-paths* ,source-paths))
693 (unwind-protect
694 (progn ,@forms)
695 (clrhash ,source-paths)))))
697 ;;; Bind the hashtables used for keeping track of global variables,
698 ;;; functions, etc. Also establish condition handlers.
699 (defmacro with-ir1-namespace (&body forms)
700 `(let ((*free-vars* (make-hash-table :test 'eq))
701 (*free-funs* (make-hash-table :test 'equal))
702 (*constants* (make-hash-table :test 'equal)))
703 (unwind-protect
704 (progn ,@forms)
705 (clrhash *free-funs*)
706 (clrhash *free-vars*)
707 (clrhash *constants*))))
709 ;;; Look up NAME in the lexical environment namespace designated by
710 ;;; SLOT, returning the <value, T>, or <NIL, NIL> if no entry. The
711 ;;; :TEST keyword may be used to determine the name equality
712 ;;; predicate.
713 (defmacro lexenv-find (name slot &key test)
714 (once-only ((n-res `(assoc ,name (,(let ((*package* (symbol-package 'lexenv-funs)))
715 (symbolicate "LEXENV-" slot))
716 *lexenv*)
717 :test ,(or test '#'eq))))
718 `(if ,n-res
719 (values (cdr ,n-res) t)
720 (values nil nil))))
722 (defmacro with-component-last-block ((component block) &body body)
723 (with-unique-names (old-last-block)
724 (once-only ((component component)
725 (block block))
726 `(let ((,old-last-block (component-last-block ,component)))
727 (unwind-protect
728 (progn (setf (component-last-block ,component)
729 ,block)
730 ,@body)
731 (setf (component-last-block ,component)
732 ,old-last-block))))))
735 ;;;; the EVENT statistics/trace utility
737 ;;; FIXME: This seems to be useful for troubleshooting and
738 ;;; experimentation, not for ordinary use, so it should probably
739 ;;; become conditional on SB-SHOW.
741 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
743 (defstruct (event-info (:copier nil))
744 ;; The name of this event.
745 (name (missing-arg) :type symbol)
746 ;; The string rescribing this event.
747 (description (missing-arg) :type string)
748 ;; The name of the variable we stash this in.
749 (var (missing-arg) :type symbol)
750 ;; The number of times this event has happened.
751 (count 0 :type fixnum)
752 ;; The level of significance of this event.
753 (level (missing-arg) :type unsigned-byte)
754 ;; If true, a function that gets called with the node that the event
755 ;; happened to.
756 (action nil :type (or function null)))
758 ;;; A hashtable from event names to event-info structures.
759 (defvar *event-info* (make-hash-table :test 'eq))
761 ;;; Return the event info for Name or die trying.
762 (declaim (ftype (function (t) event-info) event-info-or-lose))
763 (defun event-info-or-lose (name)
764 (let ((res (gethash name *event-info*)))
765 (unless res
766 (error "~S is not the name of an event." name))
767 res))
769 ) ; EVAL-WHEN
771 ;;; Return the number of times that EVENT has happened.
772 (declaim (ftype (function (symbol) fixnum) event-count))
773 (defun event-count (name)
774 (event-info-count (event-info-or-lose name)))
776 ;;; Return the function that is called when Event happens. If this is
777 ;;; null, there is no action. The function is passed the node to which
778 ;;; the event happened, or NIL if there is no relevant node. This may
779 ;;; be set with SETF.
780 (declaim (ftype (function (symbol) (or function null)) event-action))
781 (defun event-action (name)
782 (event-info-action (event-info-or-lose name)))
783 (declaim (ftype (function (symbol (or function null)) (or function null))
784 %set-event-action))
785 (defun %set-event-action (name new-value)
786 (setf (event-info-action (event-info-or-lose name))
787 new-value))
788 (defsetf event-action %set-event-action)
790 ;;; Return the non-negative integer which represents the level of
791 ;;; significance of the event Name. This is used to determine whether
792 ;;; to print a message when the event happens. This may be set with
793 ;;; SETF.
794 (declaim (ftype (function (symbol) unsigned-byte) event-level))
795 (defun event-level (name)
796 (event-info-level (event-info-or-lose name)))
797 (declaim (ftype (function (symbol unsigned-byte) unsigned-byte) %set-event-level))
798 (defun %set-event-level (name new-value)
799 (setf (event-info-level (event-info-or-lose name))
800 new-value))
801 (defsetf event-level %set-event-level)
803 ;;; Define a new kind of event. NAME is a symbol which names the event
804 ;;; and DESCRIPTION is a string which describes the event. Level
805 ;;; (default 0) is the level of significance associated with this
806 ;;; event; it is used to determine whether to print a Note when the
807 ;;; event happens.
808 (defmacro defevent (name description &optional (level 0))
809 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
810 `(eval-when (:compile-toplevel :load-toplevel :execute)
811 (defvar ,var-name
812 (make-event-info :name ',name
813 :description ',description
814 :var ',var-name
815 :level ,level))
816 (setf (gethash ',name *event-info*) ,var-name)
817 ',name)))
819 ;;; the lowest level of event that will print a note when it occurs
820 (declaim (type unsigned-byte *event-note-threshold*))
821 (defvar *event-note-threshold* 1)
823 ;;; Note that the event with the specified NAME has happened. NODE is
824 ;;; evaluated to determine the node to which the event happened.
825 (defmacro event (name &optional node)
826 ;; Increment the counter and do any action. Mumble about the event if
827 ;; policy indicates.
828 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
830 ;;; Print a listing of events and their counts, sorted by the count.
831 ;;; Events that happened fewer than Min-Count times will not be
832 ;;; printed. Stream is the stream to write to.
833 (declaim (ftype (function (&optional unsigned-byte stream) (values)) event-statistics))
834 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
835 (collect ((info))
836 (maphash (lambda (k v)
837 (declare (ignore k))
838 (when (>= (event-info-count v) min-count)
839 (info v)))
840 *event-info*)
841 (dolist (event (sort (info) #'> :key #'event-info-count))
842 (format stream "~6D: ~A~%" (event-info-count event)
843 (event-info-description event)))
844 (values))
845 (values))
847 (declaim (ftype (function nil (values)) clear-event-statistics))
848 (defun clear-event-statistics ()
849 (maphash (lambda (k v)
850 (declare (ignore k))
851 (setf (event-info-count v) 0))
852 *event-info*)
853 (values))
855 ;;;; functions on directly-linked lists (linked through specialized
856 ;;;; NEXT operations)
858 #!-sb-fluid (declaim (inline find-in position-in))
860 ;;; Find ELEMENT in a null-terminated LIST linked by the accessor
861 ;;; function NEXT. KEY and TEST are the same as for generic sequence functions.
862 (defun find-in (next
863 element
864 list
865 &key
866 (key #'identity)
867 (test #'eql))
868 (declare (type function next key test))
869 (do ((current list (funcall next current)))
870 ((null current) nil)
871 (when (funcall test (funcall key current) element)
872 (return current))))
874 ;;; Return the position of ELEMENT (or NIL if absent) in a
875 ;;; null-terminated LIST linked by the accessor function NEXT.
876 ;;; KEY and TEST are the same as for generic sequence functions.
877 (defun position-in (next
878 element
879 list
880 &key
881 (key #'identity)
882 (test #'eql))
883 (declare (type function next key test))
884 (do ((current list (funcall next current))
885 (i 0 (1+ i)))
886 ((null current) nil)
887 (when (funcall test (funcall key current) element)
888 (return i))))
890 (defmacro deletef-in (next place item &environment env)
891 (multiple-value-bind (temps vals stores store access)
892 (#+sb-xc sb!xc:get-setf-expansion #-sb-xc get-setf-expansion place env)
893 (when (cdr stores)
894 (error "multiple store variables for ~S" place))
895 (let ((n-item (gensym))
896 (n-place (gensym))
897 (n-current (gensym))
898 (n-prev (gensym)))
899 `(let* (,@(mapcar #'list temps vals)
900 (,n-place ,access)
901 (,n-item ,item))
902 (if (eq ,n-place ,n-item)
903 (let ((,(first stores) (,next ,n-place)))
904 ,store)
905 (do ((,n-prev ,n-place ,n-current)
906 (,n-current (,next ,n-place)
907 (,next ,n-current)))
908 ((eq ,n-current ,n-item)
909 (setf (,next ,n-prev)
910 (,next ,n-current)))))
911 (values)))))
913 ;;; Push ITEM onto a list linked by the accessor function NEXT that is
914 ;;; stored in PLACE.
916 (defmacro push-in (next item place &environment env)
917 (multiple-value-bind (temps vals stores store access)
918 (#+sb-xc sb!xc:get-setf-expansion #-sb-xc get-setf-expansion place env)
919 (when (cdr stores)
920 (error "multiple store variables for ~S" place))
921 `(let (,@(mapcar #'list temps vals)
922 (,(first stores) ,item))
923 (setf (,next ,(first stores)) ,access)
924 ,store
925 (values))))
927 (defmacro position-or-lose (&rest args)
928 `(or (position ,@args)
929 (error "shouldn't happen?")))
931 ;;; user-definable compiler io syntax
933 ;;; We use WITH-SANE-IO-SYNTAX to provide safe defaults, and provide
934 ;;; *COMPILER-PRINT-VARIABLE-ALIST* for user customization.
935 (defvar *compiler-print-variable-alist* nil
936 #!+sb-doc
937 "an association list describing new bindings for special variables
938 to be used by the compiler for error-reporting, etc. Eg.
940 ((*PRINT-LENGTH* . 10) (*PRINT-LEVEL* . 6) (*PRINT-PRETTY* . NIL))
942 The variables in the CAR positions are bound to the values in the CDR
943 during the execution of some debug commands. When evaluating arbitrary
944 expressions in the debugger, the normal values of the printer control
945 variables are in effect.
947 Initially empty, *COMPILER-PRINT-VARIABLE-ALIST* is Typically used to
948 specify bindings for printer control variables.")
950 (defmacro with-compiler-io-syntax (&body forms)
951 `(with-sane-io-syntax
952 (progv
953 (nreverse (mapcar #'car *compiler-print-variable-alist*))
954 (nreverse (mapcar #'cdr *compiler-print-variable-alist*))
955 ,@forms)))