0.7.11.11:
[sbcl.git] / src / compiler / policy.lisp
1 ;;;; compiler optimization policy stuff
2
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.
11
12 (in-package "SB!C")
13
14 ;;; a value for an optimization declaration
15 (def!type policy-quality () '(integer 0 3))
16
17 ;;; CMU CL used a special STRUCTURE-OBJECT type POLICY to represent
18 ;;; the state of optimization policy at any point in compilation. This
19 ;;; was a natural choice, but in SBCL it became a little troublesome
20 ;;; because of stupid technicalities involving the cold initialization
21 ;;; of structure LAYOUTs and structure accessors, so now we just use
22 ;;; alists instead.
23 (def!type policy () 'list)
24
25 (eval-when (#-sb-xc-host :compile-toplevel :load-toplevel :execute)
26   (defstruct policy-dependent-quality
27     dummy
28     name
29     expression
30     getter
31     values-documentation))
32
33 ;;; names of recognized optimization policy qualities
34 (defvar *policy-qualities*) ; (initialized at cold init)
35 (eval-when (:compile-toplevel :load-toplevel :execute)
36   (defvar *policy-dependent-qualities* nil)) ; alist of POLICY-DEPENDENT-QUALITYs
37
38 ;;; Is X the name of an optimization policy quality?
39 (defun policy-quality-name-p (x)
40   (or (memq x *policy-qualities*)
41       (assq x *policy-dependent-qualities*)))
42
43 ;;; *POLICY* holds the current global compiler policy information, as
44 ;;; an alist mapping from optimization quality name to quality value.
45 ;;; Inside the scope of declarations, new entries are added at the
46 ;;; head of the alist.
47 (declaim (type policy *policy*))
48 (defvar *policy*)          ; initialized in cold init
49
50 ;;; This is to be called early in cold init to set things up, and may
51 ;;; also be called again later in cold init in order to reset default
52 ;;; optimization policy back to default values after toplevel PROCLAIM
53 ;;; OPTIMIZE forms have messed with it.
54 (defun !policy-cold-init-or-resanify ()
55   (setf *policy-qualities*
56         '(;; ANSI standard qualities
57           compilation-speed
58           debug
59           safety
60           space
61           speed
62           ;; SBCL extensions
63           ;;
64           ;; FIXME: INHIBIT-WARNINGS is a misleading name for this.
65           ;; Perhaps BREVITY would be better. But the ideal name would
66           ;; have connotations of suppressing not warnings but only
67           ;; optimization-related notes, which is already mostly the
68           ;; behavior, and should probably become the exact behavior.
69           ;; Perhaps INHIBIT-NOTES?
70           inhibit-warnings))
71   (setf *policy*
72         (mapcar (lambda (name)
73                   ;; CMU CL didn't use 1 as the default for everything,
74                   ;; but since ANSI says 1 is the ordinary value, we do.
75                   (cons name 1))
76                 *policy-qualities*)))
77 ;;; On the cross-compilation host, we initialize immediately (not
78 ;;; waiting for "cold init", since cold init doesn't exist on
79 ;;; cross-compilation host).
80 #+sb-xc-host (!policy-cold-init-or-resanify)
81
82 ;;; Look up a named optimization quality in POLICY. This is only
83 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
84 ;;; it's an error if it's called for a quality which isn't defined.
85 (defun policy-quality (policy quality-name)
86   (let* ((acons (assoc quality-name policy))
87          (result (or (cdr acons) 1)))
88     result))
89
90 ;;; syntactic sugar for querying optimization policy qualities
91 ;;;
92 ;;; Evaluate EXPR in terms of the optimization policy associated with
93 ;;; THING. EXPR is a form which accesses optimization qualities by
94 ;;; referring to them by name, e.g. (> SPEED SPACE).
95 (defmacro policy (thing expr)
96   (let* ((n-policy (gensym "N-POLICY-"))
97          (binds (mapcar (lambda (name)
98                           `(,name (policy-quality ,n-policy ',name)))
99                         *policy-qualities*))
100          (dependent-binds
101           (loop for (name . info) in *policy-dependent-qualities*
102                collect `(,name (policy-quality ,n-policy ',name))
103                collect `(,name (if (= ,name 1)
104                                    ,(policy-dependent-quality-expression info)
105                                    ,name)))))
106     `(let* ((,n-policy (%coerce-to-policy ,thing))
107             ,@binds
108             ,@dependent-binds)
109        (declare (ignorable ,@*policy-qualities*
110                            ,@(mapcar #'car *policy-dependent-qualities*)))
111        ,expr)))
112
113 ;;; Dependent qualities
114 (defmacro define-optimization-quality
115     (name expression &optional documentation)
116   `(eval-when (:compile-toplevel :load-toplevel :execute)
117      (let ((acons (assoc ',name *policy-dependent-qualities*))
118            (item (make-policy-dependent-quality
119                   :name ',name
120                   :expression ',expression
121                   :getter (lambda (policy) (policy policy ,expression))
122                   :values-documentation ',documentation)))
123        (if acons
124            (setf (cdr acons) item)
125            (push `(,',name . ,item) *policy-dependent-qualities*)))
126      ',name))
127