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