| 1 |
|
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2003, Juan Jose Garcia-Ripoll |
| 5 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See the file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
(in-package #:compiler) |
| 10 |
|
| 11 |
(defun default-policy () |
| 12 |
(compute-policy `((space ,*space*) |
| 13 |
(safety ,*safety*) |
| 14 |
(debug ,*debug*) |
| 15 |
(speed ,*speed*) |
| 16 |
(compilation-speed ,*compilation-speed*)) |
| 17 |
0)) |
| 18 |
|
| 19 |
(defun cmp-env-policy (env) |
| 20 |
(or (first (cmp-env-search-declaration 'optimization env)) |
| 21 |
(default-policy))) |
| 22 |
|
| 23 |
(defun add-default-optimizations (env) |
| 24 |
(if (cmp-env-search-declaration 'optimization env) |
| 25 |
env |
| 26 |
(cmp-env-add-declaration 'optimization (list (default-policy)) env))) |
| 27 |
|
| 28 |
(defun cmp-env-add-optimizations (decl &optional (env *cmp-env*)) |
| 29 |
(let* ((old (cmp-env-policy env)) |
| 30 |
(new (compute-policy decl old))) |
| 31 |
(cmp-env-add-declaration 'optimization (list new) env))) |
| 32 |
|
| 33 |
(defun maybe-add-policy (decl &optional (env *cmp-env*)) |
| 34 |
(when (and (consp decl) |
| 35 |
(<= (list-length decl) 2) |
| 36 |
(gethash (first decl) *optimization-quality-switches*)) |
| 37 |
(let* ((name (first decl)) |
| 38 |
(value (if (or (endp (rest decl)) (second decl)) |
| 39 |
(if (standard-optimization-quality-p name) |
| 40 |
3 |
| 41 |
1) |
| 42 |
0)) |
| 43 |
(old-policy (cmp-env-policy env)) |
| 44 |
(new-policy (compute-policy (list (list name value)) old-policy))) |
| 45 |
(cmp-env-add-declaration 'optimization (list new-policy) env)))) |
| 46 |
|
| 47 |
(defun cmp-env-all-optimizations (&optional (env *cmp-env*)) |
| 48 |
(let ((o (cmp-env-policy env))) |
| 49 |
(list (policy-to-debug-level o) |
| 50 |
(policy-to-safety-level o) |
| 51 |
(policy-to-space-level o) |
| 52 |
(policy-to-speed-level o)))) |
| 53 |
|
| 54 |
(defun cmp-env-optimization (property &optional (env *cmp-env*)) |
| 55 |
(let ((o (cmp-env-policy env))) |
| 56 |
(case property |
| 57 |
(debug (policy-to-debug-level o)) |
| 58 |
(safety (policy-to-safety-level o)) |
| 59 |
(space (policy-to-space-level o)) |
| 60 |
(speed (policy-to-speed-level o))))) |
| 61 |
|
| 62 |
(defun safe-compile () |
| 63 |
(>= (cmp-env-optimization 'safety) 2)) |
| 64 |
|
| 65 |
(defun compiler-push-events () |
| 66 |
(>= (cmp-env-optimization 'safety) 3)) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
;; |
| 71 |
;; ERROR CHECKING POLICY |
| 72 |
;; |
| 73 |
|
| 74 |
(define-policy ext:assume-no-errors |
| 75 |
"All bets are off." |
| 76 |
(:off safety 1)) |
| 77 |
|
| 78 |
(define-policy-alias ext:assume-right-type |
| 79 |
"Don't insert optional runtime type checks for known types." |
| 80 |
(:alias ext:assume-no-errors)) |
| 81 |
|
| 82 |
(define-policy-alias ext:type-assertions |
| 83 |
"Generate type assertions when inlining accessors and other functions." |
| 84 |
(:anti-alias ext:assume-no-errors)) |
| 85 |
|
| 86 |
(define-policy ext:check-stack-overflow |
| 87 |
"Add a stack check to every function" |
| 88 |
(:on safety 2)) |
| 89 |
|
| 90 |
(define-policy ext:check-arguments-type |
| 91 |
"Generate CHECK-TYPE forms for function arguments with type declarations." |
| 92 |
(:on safety 1)) |
| 93 |
|
| 94 |
(define-policy ext:array-bounds-check |
| 95 |
"Check out of bounds access to arrays." |
| 96 |
(:on safety 1)) |
| 97 |
|
| 98 |
(define-policy ext:global-var-checking |
| 99 |
"Read the value of a global variable even if it is discarded, ensuring it is bound." |
| 100 |
(:on safety 3)) |
| 101 |
|
| 102 |
(define-policy ext:global-function-checking |
| 103 |
"Read the binding of a global function even if it is discarded." |
| 104 |
(:on safety 3)) |
| 105 |
|
| 106 |
(define-policy ext:check-nargs |
| 107 |
"Check that the number of arguments a function receives is within bounds." |
| 108 |
(:on safety 1) |
| 109 |
(:only-on ext:check-arguments-type)) |
| 110 |
|
| 111 |
(define-policy ext:the-is-checked |
| 112 |
"THE is equivalent to EXT:CHECKED-VALUE. Otherwise THE is equivalent to EXT:TRULY-THE." |
| 113 |
(:on safety 1)) |
| 114 |
|
| 115 |
;; |
| 116 |
;; INLINING POLICY |
| 117 |
;; |
| 118 |
|
| 119 |
(define-policy ext:assume-types-dont-change |
| 120 |
"Assume that type and class definitions will not change." |
| 121 |
(:off safety 1)) |
| 122 |
|
| 123 |
(define-policy ext:inline-slot-access |
| 124 |
"Inline access to structures and sealed classes." |
| 125 |
(:on speed 1) |
| 126 |
(:off debug 2) |
| 127 |
(:off safety 2)) |
| 128 |
|
| 129 |
(define-policy ext:inline-accessors |
| 130 |
"Inline access to object slots, including conses and arrays." |
| 131 |
(:off debug 2) |
| 132 |
(:off space 2)) |
| 133 |
|
| 134 |
(define-policy ext:inline-bit-operations |
| 135 |
"Inline LDB and similar functions." |
| 136 |
(:off space 2)) |
| 137 |
|
| 138 |
(define-policy-alias ext:open-code-aref/aset |
| 139 |
"Inline access to arrays." |
| 140 |
(:alias ext:inline-accessors)) |
| 141 |
|
| 142 |
(define-policy ext:evaluate-forms |
| 143 |
"Pre-evaluate a function that takes constant arguments." |
| 144 |
(:off debug 1)) |
| 145 |
|
| 146 |
(define-policy ext:use-direct-C-call |
| 147 |
"Emit direct calls to a function whose C name is known." |
| 148 |
(:off debug 2)) |
| 149 |
|
| 150 |
(define-policy ext:inline-type-checks |
| 151 |
"Expand TYPEP and similar forms in terms of simpler functions, such as FLOATP, INTGERP, STRINGP." |
| 152 |
(:off space 2)) |
| 153 |
|
| 154 |
(define-policy ext:inline-sequence-functions |
| 155 |
"Inline functions such as MAP, MEMBER, FIND, etc." |
| 156 |
(:off space 2)) |
| 157 |
|
| 158 |
(define-policy ext:inline-nargs-check |
| 159 |
"Inline checks for correct number of arguments for functions with fixed number of arguments." |
| 160 |
(:off space 2)) |
| 161 |
|
| 162 |
;; |
| 163 |
;; DEBUG POLICY |
| 164 |
;; |
| 165 |
|
| 166 |
(define-policy ext:debug-variable-bindings |
| 167 |
"Create a debug vector with the bindings of each LET/LET*/LAMBDA form." |
| 168 |
;; We can only create variable bindings when the function has an IHS frame!!! |
| 169 |
(:requires (policy-debug-ihs-frame env)) |
| 170 |
(:on debug 3)) |
| 171 |
|
| 172 |
(define-policy ext:debug-ihs-frame |
| 173 |
"Let the functions appear in backtraces." |
| 174 |
(:on debug 3)) |