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