0.8.10.29:
[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   
78 ;;; On the cross-compilation host, we initialize immediately (not
79 ;;; waiting for "cold init", since cold init doesn't exist on
80 ;;; cross-compilation host).
81 #+sb-xc-host (!policy-cold-init-or-resanify)
82
83 ;;; Look up a named optimization quality in POLICY. This is only
84 ;;; called by compiler code for known-valid QUALITY-NAMEs, e.g. SPEED;
85 ;;; it's an error if it's called for a quality which isn't defined.
86 (defun policy-quality (policy quality-name)
87   (aver (policy-quality-name-p quality-name))
88   (let* ((acons (assoc quality-name policy))
89          (result (or (cdr acons) 1)))
90     result))
91
92 ;;; syntactic sugar for querying optimization policy qualities
93 ;;;
94 ;;; Evaluate EXPR in terms of the optimization policy associated with
95 ;;; THING. EXPR is a form which accesses optimization qualities by
96 ;;; referring to them by name, e.g. (> SPEED SPACE).
97 (defmacro policy (thing expr)
98   (let* ((n-policy (gensym "N-POLICY-"))
99          (binds (mapcar (lambda (name)
100                           `(,name (policy-quality ,n-policy ',name)))
101                         *policy-qualities*))
102          (dependent-binds
103           (loop for (name . info) in *policy-dependent-qualities*
104                collect `(,name (policy-quality ,n-policy ',name))
105                collect `(,name (if (= ,name 1)
106                                    ,(policy-dependent-quality-expression info)
107                                    ,name)))))
108     `(let* ((,n-policy (%coerce-to-policy ,thing))
109             ,@binds
110             ,@dependent-binds)
111        (declare (ignorable ,@*policy-qualities*
112                            ,@(mapcar #'car *policy-dependent-qualities*)))
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            (push `(,',name . ,item) *policy-dependent-qualities*)))
128      ',name))