1.0.19.12: give a warning for newly deprecated stack-allcation optimization policies
[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 ;;; global policy restrictions
18 (defvar *policy-restrictions* nil)
19
20 (defun restrict-compiler-policy (&optional quality (min 0))
21   #!+sb-doc
22   "Assing a minimum value to an optimization quality. QUALITY is the name of
23 the optimization quality to restrict, and MIN (defaulting to zero) is the
24 minimum allowed value.
25
26 Returns the alist describing the current policy restrictions.
27
28 If QUALITY is NIL or not given, nothing is done.
29
30 Otherwise, if MIN is zero or not given, any existing restrictions of QUALITY
31 are removed. If MIN is between one and three inclusive, it becomes the new
32 minimum value for the optimization quality: any future proclamations or
33 declarations of the quality with a value less then MIN behave as if the value
34 was MIN instead.
35
36 This is intended to be used interactively, to facilitate recompiling large
37 bodies of code with eg. a known minimum safety.
38
39 EXPERIMENTAL INTERFACE: Subject to change."
40   (declare (type policy-quality min))
41   (when quality
42     (aver (policy-quality-name-p quality))
43     (if (zerop min)
44         (setf *policy-restrictions*
45               (remove quality *policy-restrictions* :key #'car))
46         (let ((cell (assoc quality *policy-restrictions*)))
47           (if cell
48               (setf (cdr cell) min)
49               (push (cons quality min) *policy-restrictions*)))))
50   *policy-restrictions*)
51
52 ;;; CMU CL used a special STRUCTURE-OBJECT type POLICY to represent
53 ;;; the state of optimization policy at any point in compilation. This
54 ;;; was a natural choice, but in SBCL it became a little troublesome
55 ;;; because of stupid technicalities involving the cold initialization
56 ;;; of structure LAYOUTs and structure accessors, so now we just use
57 ;;; alists instead.
58 (def!type policy () 'list)
59
60 (defstruct policy-dependent-quality
61   name
62   expression
63   getter
64   values-documentation)
65
66 ;;; names of recognized optimization policy qualities
67 (defvar *policy-qualities*) ; (initialized at cold init)
68 (defvar *policy-dependent-qualities* nil) ; alist of POLICY-DEPENDENT-QUALITYs
69
70 ;;; Is X the name of an optimization policy quality?
71 (defun policy-quality-name-p (x)
72   (or (memq x *policy-qualities*)
73       (assq x *policy-dependent-qualities*)))
74
75 ;;; Is it deprecated?
76 (defun policy-quality-deprecation-warning (quality spec)
77   (when (member quality '(stack-allocate-dynamic-extent stack-allocate-vector
78                           stack-allocate-value-cells))
79     (make-instance 'simple-reference-warning
80                    :format-control "~@<Ignoring deprecated optimization quality ~S in:~_ ~S~:>"
81                    :format-arguments (list quality spec)
82                    :references (list '(:sbcl :variable *stack-allocate-dynamic-extent*)
83                                      '(:sbcl :node "Dynamic-extent allocation")))))
84
85 ;;; *POLICY* holds the current global compiler policy information, as
86 ;;; an alist mapping from optimization quality name to quality value.
87 ;;; Inside the scope of declarations, new entries are added at the
88 ;;; head of the alist.
89 (declaim (type policy *policy*))
90 (defvar *policy*)          ; initialized in cold init
91
92 ;;; This is to be called early in cold init to set things up, and may
93 ;;; also be called again later in cold init in order to reset default
94 ;;; optimization policy back to default values after toplevel PROCLAIM
95 ;;; OPTIMIZE forms have messed with it.
96 (defun !policy-cold-init-or-resanify ()
97   (setf *policy-qualities*
98         '(;; ANSI standard qualities
99           compilation-speed
100           debug
101           safety
102           space
103           speed
104           ;; SBCL extensions
105           ;;
106           ;; FIXME: INHIBIT-WARNINGS is a misleading name for this.
107           ;; Perhaps BREVITY would be better. But the ideal name would
108           ;; have connotations of suppressing not warnings but only
109           ;; optimization-related notes, which is already mostly the
110           ;; behavior, and should probably become the exact behavior.
111           ;; Perhaps INHIBIT-NOTES?
112           inhibit-warnings))
113   (setf *policy*
114         (mapcar (lambda (name)
115                   ;; CMU CL didn't use 1 as the default for
116                   ;; everything, but since ANSI says 1 is the ordinary
117                   ;; value, we do.
118                   (cons name 1))
119                 *policy-qualities*))
120   (setf *policy-restrictions* nil)
121   ;; not actually POLICY, but very similar
122   (setf *handled-conditions* nil
123         *disabled-package-locks* nil))
124
125 ;;; On the cross-compilation host, we initialize immediately (not
126 ;;; waiting for "cold init", since cold init doesn't exist on
127 ;;; cross-compilation host).
128 #+sb-xc-host (!policy-cold-init-or-resanify)
129
130 ;;; Look up a named optimization quality in POLICY. This is only
131 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
132 ;;; it's an error if it's called for a quality which isn't defined.
133 (defun policy-quality (policy quality-name)
134   (aver (policy-quality-name-p quality-name))
135   (%policy-quality policy quality-name))
136
137 (defun %policy-quality (policy quality-name)
138   (let* ((acons (assoc quality-name policy))
139          (min (or (cdr (assoc quality-name *policy-restrictions*)) 0))
140          (result (or (cdr acons) 1)))
141     (max result min)))
142
143 ;;; syntactic sugar for querying optimization policy qualities
144 ;;;
145 ;;; Evaluate EXPR in terms of the optimization policy associated with
146 ;;; THING. EXPR is a form which accesses optimization qualities by
147 ;;; referring to them by name, e.g. (> SPEED SPACE).
148 (defmacro policy (thing expr)
149   (let* ((n-policy (gensym "N-POLICY-"))
150          (binds (mapcar (lambda (name)
151                           `(,name (policy-quality ,n-policy ',name)))
152                         *policy-qualities*))
153          (dependent-binds
154           (loop for (name . info) in *policy-dependent-qualities*
155                collect `(,name (let ((,name (policy-quality ,n-policy ',name)))
156                                  (if (= ,name 1)
157                                      ,(policy-dependent-quality-expression info)
158                                      ,name))))))
159     `(let* ((,n-policy (%coerce-to-policy ,thing)))
160        (declare (ignorable ,n-policy))
161        (symbol-macrolet (,@binds
162                          ,@dependent-binds)
163          ,expr))))
164
165 ;;; Dependent qualities
166 (defmacro define-optimization-quality
167     (name expression &optional values-documentation documentation)
168   (declare (ignorable documentation))
169   `(eval-when (:compile-toplevel :load-toplevel :execute)
170      (let ((acons (assoc ',name *policy-dependent-qualities*))
171            (item (make-policy-dependent-quality
172                   :name ',name
173                   :expression ',expression
174                   :getter (lambda (policy) (policy policy ,expression))
175                   :values-documentation ',values-documentation)))
176        (if acons
177            (setf (cdr acons) item)
178            (setf *policy-dependent-qualities*
179                  (nconc *policy-dependent-qualities* (list `(,',name . ,item))))))
180      #-sb-xc-host
181      ,@(when documentation `((setf (fdocumentation ',name 'optimize) ,documentation)))
182      ',name))