Debian Codegraph @

src/cmp/cmppolicy.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
6 ;;;; Copyright (c) 2023, Daniel Kochmański
7 ;;;;
8 ;;;; See the file 'LICENSE' for the copyright details.
9 ;;;;
10
11 ;;;;
12 ;;;; CMPPOLICY -- Code generation choices
13 ;;;;
14
15 (in-package "COMPILER")
16
17 ;;;
18 ;;; ECL encodes the compiler policy an integer. Each bit represents a single
19 ;;; optimization choice. Lowest twenty bits encode the standard optimization
20 ;;; qualities DEBUG, SAFETY, SPEED, SPACE and COMPILATION-SPEED - four bits for
21 ;;; each level. Levels are mutually exclusive for a single quality. Then each
22 ;;; defined policy occupies one bit. For example:
23 ;;;
24 ;;; X Y Z COMPILATION-SPEED SPACE SPEED SAFETY DEBUG
25 ;;; 0 1 0 0010 0010 1000 0001 0010
26 ;;;
27 ;;; Represents the following optimization settings:
28 ;;;
29 ;;; (OPTIMIZE (DEBUG 1) (SAFETY 0) (SPEED 3) (COMPILATION-SPEED 2) Y)
30 ;;;
31 ;;; New optimization qualities are defined with DEFINE-POLICY. Such definition
32 ;;; adds one more bit tot he compilation policy and defines a function to test
33 ;;; whether the quality is applicable under the compilation policy of the env.
34 ;;; This functions first checks whether the quality bit is "1" and then may
35 ;;; perform additional tests defined with clauses :REQUIRES.
36 ;;;
37 ;;; Each optimization quality (level) has associated two numbers. When it is
38 ;;; declared in the environment the first number added to the compilation policy
39 ;;; with LOGIOR and the second number is removed from the compilation policy
40 ;;; with LOGANDC2. Thanks to that it is possible for declaration of one policy
41 ;;; to enable other policies associated with it. For example (DEBUG 1) may be:
42 ;;;
43 ;;; X Y Z COMPILATION-SPEED SPACE SPEED SAFETY DEBUG
44 ;;; 1 1 0 0000 0000 0000 0000 0010 "on"
45 ;;; 0 0 1 0000 0000 0000 0000 1101 "off"
46 ;;;
47 ;;; When (DEBUG 1) is declared then bits representing X, Y and (DEBUG 1) are set
48 ;;; to 1 and bits representing Z and other DEBUG levels are set to 0. Everything
49 ;;; else remains unchanged. These pairs are "optimization quality switches".
50 ;;;
51 ;;; When a new policy is defined it may contain multiple :ON and :OFF clauses
52 ;;; with an optional parameter representing the "cut off" level. For example:
53 ;;;
54 ;;; (define-policy W
55 ;;; ; (SAFETY 0) and (SAFETY 1) "off" flags for W = 1
56 ;;; ; (SAFETY 2) and (SAFETY 3) "on" flags for W = 1
57 ;;; (:on safety 2)
58 ;;; ; (DEBUG 0) and (DEBUG 1) "on" flags for W = 1
59 ;;; ; (DEBUG 2) and (DEBUG 3) "off" flags for W = 1
60 ;;; (:off debug 2))
61 ;;;
62 ;;; With this example declaring (SAFETY 2) will enable the policy W and
63 ;;; declaring (SAFETY 1) will disable it. Consider the following example:
64 ;;;
65 ;;; (locally (declare (safety 2) (debug 2))
66 ;;; (do-something))
67 ;;;
68 ;;; The optimization (SAFETY 2) enables the policy W while the optimization
69 ;;; (DEBUG 2) disables it. It is apparent from this example that the order in
70 ;;; which we apply quality switches to the compilation policy is important.
71 ;;; COMPUTE-POLICY prioritizes "off" flags over "on" flags so in this case the
72 ;;; policy W will be disabled.
73 ;;;
74 ;;; Only standard optimization qualities have levels. User defined policies may
75 ;;; be also references but the level must not be specified, i.e (:ON CHECK-FOO).
76 ;;;
77
78 (eval-when (:compile-toplevel :load-toplevel :execute)
79 (defconstant *standard-optimization-quality-names*
80 '(debug safety speed space compilation-speed)))
81
82 (defun standard-optimization-quality-p (name)
83 (member name *standard-optimization-quality-names* :test #'eq))
84
85 (eval-when (:compile-toplevel :execute)
86 (defvar *last-optimization-bit* 20)
87 (defvar *optimization-quality-switches*
88 (loop with hash = (make-hash-table :size 64 :test #'eq)
89 for name in *standard-optimization-quality-names*
90 for i from 0 by 4
91 for list = (loop with mask = (ash #b1111 i)
92 for level from 0 to 3
93 for bits = (ash 1 (+ level i))
94 collect (cons bits (logxor bits mask)))
95 do (setf (gethash name hash) list)
96 finally (return hash)))
97 ;; For the standard qualities we encode the lowest bit position.
98 (defvar *optimization-bits*
99 (loop with hash = (make-hash-table :size 64 :test #'eq)
100 for name in *standard-optimization-quality-names*
101 for i from 0 by 4
102 do (setf (gethash name hash) i)
103 finally (return hash))))
104
105 (eval-when (:load-toplevel :execute)
106 (defvar *last-optimization-bit* #.*last-optimization-bit*)
107 (defvar *optimization-quality-switches* #.*optimization-quality-switches*)
108 (defvar *optimization-bits* #.*optimization-bits*))
109
110 (defun take-optimization-bit (name)
111 (or (gethash name *optimization-bits*)
112 (setf (gethash name *optimization-bits*)
113 (incf *last-optimization-bit*))))
114
115 (defun optimization-quality-switches (type index)
116 (nth index (gethash type *optimization-quality-switches*)))
117
118 (defun compute-policy (arguments old-bits &aux (on 0) (off 0))
119 (flet ((get-flags (x)
120 (if (atom x)
121 (if (standard-optimization-quality-p x)
122 (optimization-quality-switches x 3)
123 (optimization-quality-switches x 1))
124 (destructuring-bind (name value) x
125 (when (typep value '(integer 0 3))
126 (optimization-quality-switches name value))))))
127 (dolist (x arguments)
128 (ext:if-let ((flags (get-flags x)))
129 (setf on (logior on (car flags))
130 off (logior off (cdr flags)))
131 (cmpwarn "Illegal or unknown OPTIMIZE proclamation ~s." x))))
132 (logandc2 (logior old-bits on) off))
133
134 (defun augment-policy-switch (on-off switches flag)
135 (ecase on-off
136 (:on (rplaca switches (logior (car switches) flag)))
137 (:off (rplacd switches (logior (cdr switches) flag)))))
138
139 (defun augment-standard-policy (quality level on-off flag)
140 (loop for i from 0 to 3
141 for bits = (optimization-quality-switches quality i)
142 do (if (< i level)
143 (ecase on-off
144 (:on (augment-policy-switch :off bits flag))
145 (:off (augment-policy-switch :on bits flag)))
146 (ecase on-off
147 (:on (augment-policy-switch :on bits flag))
148 (:off (augment-policy-switch :off bits flag))))))
149
150 (defun augment-extended-policy (quality on-off flag)
151 (let ((bits (optimization-quality-switches quality 1)))
152 (ecase on-off
153 (:only-on (augment-policy-switch :on bits flag))
154 (:only-off (augment-policy-switch :off bits flag)))))
155
156 (defun policy-function-name (base)
157 (intern (concatenate 'string "POLICY-" (symbol-name base))
158 (find-package "C")))
159
160 (defmacro define-policy (&whole whole name &rest conditions)
161 (let ((doc (and (stringp (car conditions)) (pop conditions)))
162 (test (ash 1 (take-optimization-bit name)))
163 (function-name (policy-function-name name)))
164 ;; Register as an optimization quality with its own flags.
165 (setf (gethash name *optimization-quality-switches*)
166 ;; switched off switched on | two levels
167 (list (cons 0 test) (cons test 0)))
168 ;; Scan the definition and propagate flags of dependent policies.
169 (loop with extra = '()
170 for case in conditions
171 do (case (car case)
172 ((:on :off)
173 (destructuring-bind (op quality level) case
174 (augment-standard-policy quality level op test)))
175 ((:only-on :only-off)
176 (destructuring-bind (op quality) case
177 (augment-extended-policy quality op test)))
178 (:requires
179 (destructuring-bind (op form) case
180 (declare (ignore op))
181 (push form extra)))
182 (otherwise
183 (error "Syntax error in macro~% ~A" `(define-policy ,@whole))))
184 finally
185 (return
186 `(defun ,function-name (&optional (env *cmp-env*))
187 ,@(and doc (list doc))
188 (let ((bits (cmp-env-policy env)))
189 (and (logtest bits ,test)
190 ,@extra)))))))
191
192 (defmacro define-policy-alias (name doc (op alias))
193 (let ((bits (gethash alias *optimization-quality-switches*)))
194 (ecase op
195 (:alias
196 (setf (gethash name *optimization-quality-switches*) bits)
197 `(defun ,(policy-function-name name) (&optional (env *cmp-env*))
198 ,doc
199 (,(policy-function-name alias) env)))
200 (:anti-alias
201 (setf (gethash name *optimization-quality-switches*) (reverse bits))
202 `(defun ,(policy-function-name name) (&optional (env *cmp-env*))
203 ,doc
204 (not (,(policy-function-name alias) env)))))))
205
206 (macrolet ((define-function (fun-name offset)
207 `(defun ,fun-name (policy)
208 (declare (ext:assume-right-type))
209 (loop for level from 0 to 3
210 when (logbitp (+ level ,offset) policy)
211 return level))))
212 (define-function policy-to-debug-level 0)
213 (define-function policy-to-safety-level 4)
214 (define-function policy-to-speed-level 8)
215 (define-function policy-to-space-level 12)
216 (define-function policy-to-compilation-speed-level 16))