| 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 |
;;;; CMPOPT. Optimization of library functions |
| 6 |
|
| 7 |
;;;; Copyright (c) 2008. Juan Jose Garcia-Ripol |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
|
| 11 |
(in-package "COMPILER") |
| 12 |
|
| 13 |
;;; |
| 14 |
;;; TYPEP |
| 15 |
;;; |
| 16 |
;;; Some of the type checks can be expanded inline if we know the name |
| 17 |
;;; of the type and it corresponds to either a Common-Lisp base type |
| 18 |
;;; or to some class. |
| 19 |
;;; |
| 20 |
|
| 21 |
(defun expand-in-interval-p (var interval) |
| 22 |
(declare (si::c-local)) |
| 23 |
(let ((forms '())) |
| 24 |
(destructuring-bind (&optional (lower-limit '*) (upper-limit '*)) |
| 25 |
interval |
| 26 |
(unless (eq lower-limit '*) |
| 27 |
(push (if (consp lower-limit) |
| 28 |
`(> ,var ,(first lower-limit)) |
| 29 |
`(>= ,var ,lower-limit)) |
| 30 |
forms)) |
| 31 |
(unless (eq upper-limit '*) |
| 32 |
(push (if (consp upper-limit) |
| 33 |
`(< ,var ,(first upper-limit)) |
| 34 |
`(<= ,var ,upper-limit)) |
| 35 |
forms))) |
| 36 |
forms)) |
| 37 |
|
| 38 |
(defun contains-compound-function-type (type) |
| 39 |
(and (consp type) |
| 40 |
(or (eq (first type) 'FUNCTION) |
| 41 |
(and (member (first type) '(NOT OR AND CONS)) |
| 42 |
(some #'contains-compound-function-type (rest type)))))) |
| 43 |
|
| 44 |
(defun expand-typep (form object type env) |
| 45 |
;; This function is reponsible for expanding (TYPEP object type) |
| 46 |
;; forms into a reasonable set of system calls. When it fails to |
| 47 |
;; match the compiler constraints on speed and space, it simply |
| 48 |
;; returns the original form. Note that for successful recursion we |
| 49 |
;; have to output indeed the ORIGINAL FORM, not some intermediate |
| 50 |
;; step. Otherwise the compiler macro will enter an infinite loop. |
| 51 |
(let* ((orig-type type) |
| 52 |
aux function |
| 53 |
first rest function) |
| 54 |
;; Type must be constant to optimize |
| 55 |
(if (constantp type env) |
| 56 |
(setf type (cmp-env-search-type (ext:constant-form-value type env) env)) |
| 57 |
(return-from expand-typep form)) |
| 58 |
(cond ;; compound function type specifier: signals an error |
| 59 |
((contains-compound-function-type type) |
| 60 |
(cmpwarn "~S is not a valid type specifier for TYPEP" type) |
| 61 |
form) |
| 62 |
;; Variable declared with a given type |
| 63 |
((and (symbolp object) |
| 64 |
(setf aux (cmp-env-search-var object env)) |
| 65 |
(subtypep (var-type aux) type *cmp-env*)) |
| 66 |
t) |
| 67 |
;; Simple ones |
| 68 |
((subtypep 'T type *cmp-env*) T) |
| 69 |
((eq type 'NIL) NIL) |
| 70 |
;; |
| 71 |
;; Detect inconsistencies in the provided type. If we run at low |
| 72 |
;; safety, we will simply assume the user knows what she's doing. |
| 73 |
((subtypep type NIL *cmp-env*) |
| 74 |
(cmpwarn "TYPEP form contains an empty type ~S and cannot be optimized" type) |
| 75 |
form) |
| 76 |
;; |
| 77 |
;; Derived types defined with DEFTYPE. These are expanded |
| 78 |
;; first so that we can sort out compound function types |
| 79 |
;; which are equal under si::type= to the atomic function |
| 80 |
;; type but not allowed in TYPEP. |
| 81 |
((setq function (si:get-sysprop (if (atom type) |
| 82 |
type |
| 83 |
(first type)) |
| 84 |
'SI::DEFTYPE-DEFINITION)) |
| 85 |
(expand-typep form object `(quote ,(funcall function (if (atom type) |
| 86 |
(list type) |
| 87 |
type) |
| 88 |
env)) |
| 89 |
env)) |
| 90 |
;; |
| 91 |
;; There exists a function which checks for this type? |
| 92 |
((setf function (si:get-sysprop type 'si::type-predicate)) |
| 93 |
`(,function ,object)) |
| 94 |
;; |
| 95 |
;; Similar as before, but we assume the user did not give us |
| 96 |
;; the right name, or gave us an equivalent type. |
| 97 |
((loop for (a-type . function-name) in si::+known-typep-predicates+ |
| 98 |
when (si::type= type a-type *cmp-env*) |
| 99 |
do (return `(,function-name ,object)))) |
| 100 |
;; |
| 101 |
;; No optimizations that take up too much space unless requested. |
| 102 |
((not (policy-inline-type-checks)) |
| 103 |
form) |
| 104 |
;; |
| 105 |
;; CONS types. They must be checked _before_ sequence types. We |
| 106 |
;; do not produce optimized forms because they can be recursive. |
| 107 |
((and (consp type) (eq (first type) 'CONS)) |
| 108 |
form) |
| 109 |
;; |
| 110 |
;; The type denotes a known class and we can check it |
| 111 |
#+clos |
| 112 |
((and (symbolp type) (setf aux (find-class type nil))) |
| 113 |
`(si::of-class-p ,object ',type)) |
| 114 |
;; |
| 115 |
;; There are no other atomic types to optimize |
| 116 |
((atom type) |
| 117 |
form) |
| 118 |
;; |
| 119 |
;; (TYPEP o '(NOT t)) => (NOT (TYPEP o 't)) |
| 120 |
((progn |
| 121 |
(setf rest (rest type) |
| 122 |
first (first type)) |
| 123 |
(eq first 'NOT)) |
| 124 |
`(not (typep ,object ',(first rest)))) |
| 125 |
;; |
| 126 |
;; (TYPEP o '(AND t1 t2 ...)) => (AND (TYPEP o 't1) (TYPEP o 't2) ...) |
| 127 |
;; (TYPEP o '(OR t1 t2 ...)) => (OR (TYPEP o 't1) (TYPEP o 't2) ...) |
| 128 |
((member first '(OR AND)) |
| 129 |
(let ((var (gensym))) |
| 130 |
`(let ((,var ,object)) |
| 131 |
(declare (:read-only ,var)) |
| 132 |
(,first ,@(loop for type in rest |
| 133 |
collect `(typep ,var ',type)))))) |
| 134 |
;; |
| 135 |
;; (TYPEP o '(MEMBER a1 a2 ...)) => (MEMBER o '(a1 a2 ...)) |
| 136 |
((eq first 'MEMBER) |
| 137 |
`(MEMBER ,object ',rest)) |
| 138 |
;; |
| 139 |
;; (INTEGER * *), etc |
| 140 |
((member first '(INTEGER RATIONAL FLOAT REAL SINGLE-FLOAT |
| 141 |
DOUBLE-FLOAT LONG-FLOAT)) |
| 142 |
(let ((var1 (gensym)) |
| 143 |
(var2 (gensym))) |
| 144 |
;; Small optimization: it is easier to check for fixnum |
| 145 |
;; than for integer. Use it when possible. |
| 146 |
(when (and (eq first 'integer) |
| 147 |
(subtypep type 'fixnum *cmp-env*)) |
| 148 |
(setf first 'fixnum)) |
| 149 |
`(LET ((,var1 ,object) |
| 150 |
(,var2 ,(coerce 0 first))) |
| 151 |
(declare (:read-only ,var1) |
| 152 |
(type ,first ,var2)) |
| 153 |
(AND (TYPEP ,var1 ',first) |
| 154 |
(locally (declare (optimize (speed 3) (safety 0) (space 0))) |
| 155 |
(setf ,var2 (ext:truly-the ,first ,var1)) |
| 156 |
(AND ,@(expand-in-interval-p var2 rest))))))) |
| 157 |
;; |
| 158 |
;; Compound COMPLEX types. |
| 159 |
((and (eq first 'COMPLEX) |
| 160 |
(= (list-length type) 2)) |
| 161 |
`(and (complexp ,object) |
| 162 |
(typep (realpart ,object) ',(second type)) |
| 163 |
(typep (imagpart ,object) ',(second type)))) |
| 164 |
;; |
| 165 |
;; (SATISFIES predicate) |
| 166 |
((and (eq first 'SATISFIES) |
| 167 |
(= (list-length type) 2) |
| 168 |
(symbolp (setf function (second type)))) |
| 169 |
`(,function ,object)) |
| 170 |
(t |
| 171 |
form)))) |
| 172 |
|
| 173 |
(define-compiler-macro typep (&whole form object type &optional e &environment env) |
| 174 |
(declare (ignore e)) |
| 175 |
(expand-typep form object type env)) |
| 176 |
|
| 177 |
;;; |
| 178 |
;;; DOLIST |
| 179 |
;;; |
| 180 |
;;; We overwrite the original macros introducing type declarations and |
| 181 |
;;; other possible type checks. |
| 182 |
;;; |
| 183 |
|
| 184 |
(define-compiler-macro dolist |
| 185 |
((var expression &optional output-form) &body body &environment env) |
| 186 |
(multiple-value-bind (declarations body) |
| 187 |
(si:process-declarations body nil) |
| 188 |
(let* ((filtered-declarations |
| 189 |
;; NB: we filter out `type' and `ignore' declarations from |
| 190 |
;; `dolist', because VAR may be used in the result form |
| 191 |
;; and its type changes to NIL. These will be placed in |
| 192 |
;; the output-form (if present). |
| 193 |
(cons `(ignorable ,var) |
| 194 |
(remove-if (lambda (clause) |
| 195 |
(and (consp clause) |
| 196 |
(or (eq (car clause) 'type) |
| 197 |
(eq (car clause) 'ignore)))) |
| 198 |
declarations))) |
| 199 |
(list-var (gensym)) |
| 200 |
(typed-var (if (policy-assume-no-errors env) |
| 201 |
list-var |
| 202 |
`(ext:truly-the cons ,list-var)))) |
| 203 |
`(block nil |
| 204 |
(let* ((,list-var ,expression)) |
| 205 |
(si::while ,list-var |
| 206 |
(let ((,var (first ,typed-var))) |
| 207 |
(declare ,@declarations) |
| 208 |
(tagbody ,@body)) |
| 209 |
(setq ,list-var (rest ,typed-var))) |
| 210 |
,(when output-form |
| 211 |
`(let ((,var nil)) |
| 212 |
(declare ,@filtered-declarations) |
| 213 |
,output-form))))))) |
| 214 |
|
| 215 |
;;; |
| 216 |
;;; COERCE |
| 217 |
;;; |
| 218 |
;;; Simple coercion rules are implemented using the following |
| 219 |
;;; templates. X is replaced by the coerced value, which can be a |
| 220 |
;;; lisp form. We use a LET form to avoid evaluating twice the same |
| 221 |
;;; form. |
| 222 |
;;; |
| 223 |
(defparameter +coercion-table+ |
| 224 |
'((integer . (let ((y x)) (check-type y integer) y)) |
| 225 |
(float . (float x)) |
| 226 |
(short-float . (float x 0.0s0)) |
| 227 |
(single-float . (float x 0.0f0)) |
| 228 |
(double-float . (float x 0.0d0)) |
| 229 |
(long-float . (float x 0.0l0)) |
| 230 |
#+complex-float |
| 231 |
(si:complex-single-float . (let ((y x)) |
| 232 |
(declare (:read-only y)) |
| 233 |
(complex (float (realpart y) 0.0f0) |
| 234 |
(float (imagpart y) 0.0f0)))) |
| 235 |
#+complex-float |
| 236 |
(si:complex-double-float . (let ((y x)) |
| 237 |
(declare (:read-only y)) |
| 238 |
(complex (float (realpart y) 0.0d0) |
| 239 |
(float (imagpart y) 0.0d0)))) |
| 240 |
#+complex-float |
| 241 |
(si:complex-long-float . (let ((y x)) |
| 242 |
(declare (:read-only y)) |
| 243 |
(complex (float (realpart y) 0.0l0) |
| 244 |
(float (imagpart y) 0.0l0)))) |
| 245 |
(complex . (let ((y x)) |
| 246 |
(declare (:read-only y)) |
| 247 |
(complex (realpart y) (imagpart y)))) |
| 248 |
(base-char . (character x)) |
| 249 |
(character . (character x)) |
| 250 |
(function . (si::coerce-to-function x)))) |
| 251 |
|
| 252 |
(defun expand-coerce (form value type env) |
| 253 |
(declare (si::c-local)) |
| 254 |
;; This function is reponsible for expanding (TYPEP object type) |
| 255 |
;; forms into a reasonable set of system calls. When it fails to |
| 256 |
;; match the compiler constraints on speed and space, it simply |
| 257 |
;; returns the original form. Note that for successful recursion we |
| 258 |
;; have to output indeed the ORIGINAL FORM, not some intermediate |
| 259 |
;; step. Otherwise the compiler macro will enter an infinite loop. |
| 260 |
(let* ((orig-type type) |
| 261 |
first rest) |
| 262 |
;; Type must be constant to optimize |
| 263 |
(if (constantp type env) |
| 264 |
(setf type (cmp-env-search-type (ext:constant-form-value type env) env)) |
| 265 |
(return-from expand-coerce form)) |
| 266 |
(cond ;; Trivial case |
| 267 |
((subtypep 't type *cmp-env*) |
| 268 |
value) |
| 269 |
;; |
| 270 |
;; Detect inconsistencies in the type form. |
| 271 |
((subtypep type 'nil *cmp-env*) |
| 272 |
(cmperror "Cannot COERCE an expression to an empty type.")) |
| 273 |
;; |
| 274 |
;; No optimizations that take up too much space unless requested. |
| 275 |
((not (policy-inline-type-checks)) |
| 276 |
form) |
| 277 |
;; |
| 278 |
;; Search for a simple template above, replacing X by the value. |
| 279 |
((loop for (a-type . template) in +coercion-table+ |
| 280 |
when (eq type a-type) |
| 281 |
do (return (subst value 'x template)))) |
| 282 |
;; |
| 283 |
;; Derived types defined with DEFTYPE. |
| 284 |
((and (atom type) |
| 285 |
(setq first (si:get-sysprop type 'SI::DEFTYPE-DEFINITION))) |
| 286 |
(expand-coerce form value `',(funcall first (list type) env) env)) |
| 287 |
;; |
| 288 |
;; CONS types are not coercible. |
| 289 |
((and (consp type) |
| 290 |
(eq (first type) 'CONS)) |
| 291 |
form) |
| 292 |
;; |
| 293 |
;; Search for a simple template above, but now assuming the user |
| 294 |
;; provided a more complex form of the same value. |
| 295 |
((loop for (a-type . template) in +coercion-table+ |
| 296 |
when (si::type= type a-type *cmp-env*) |
| 297 |
do (return (subst value 'x template)))) |
| 298 |
;; |
| 299 |
;; SEQUENCE types |
| 300 |
((subtypep type 'sequence *cmp-env*) |
| 301 |
(multiple-value-bind (elt-type length) |
| 302 |
(si::closest-sequence-type type) |
| 303 |
(if (or (eq length '*) (policy-assume-right-type)) |
| 304 |
(if (eq elt-type 'list) |
| 305 |
`(si::coerce-to-list ,value) |
| 306 |
`(si::coerce-to-vector ,value ',elt-type ',length |
| 307 |
,(and (subtypep type 'simple-array *cmp-env*) t))) |
| 308 |
form))) |
| 309 |
;; |
| 310 |
;; There are no other atomic types to optimize |
| 311 |
((atom type) |
| 312 |
form) |
| 313 |
;; |
| 314 |
;; (TYPEP o '(AND t1 t2 ...)) => (AND (TYPEP o 't1) (TYPEP o 't2) ...) |
| 315 |
((progn |
| 316 |
(setf rest (rest type) first (first type)) |
| 317 |
(eq first 'AND)) |
| 318 |
`(let ((x ,value)) |
| 319 |
,@(loop for i in rest |
| 320 |
collect `(setf x (coerce x ',i))) |
| 321 |
x)) |
| 322 |
;; |
| 323 |
;; (COMPLEX whatever) types |
| 324 |
((and (eq first 'complex) |
| 325 |
(= (length rest) 1)) |
| 326 |
`(let ((y ,value)) |
| 327 |
(declare (:read-only y)) |
| 328 |
(complex (coerce (realpart y) ',(first rest)) |
| 329 |
(coerce (imagpart y) ',(first rest))))) |
| 330 |
;; |
| 331 |
;; (INTEGER * *), etc We have to signal an error if the type |
| 332 |
;; does not match. However, if safety settings are low, we |
| 333 |
;; skip the interval test. |
| 334 |
((member first '(INTEGER RATIONAL FLOAT REAL SINGLE-FLOAT |
| 335 |
DOUBLE-FLOAT LONG-FLOAT)) |
| 336 |
(let ((unchecked (expand-coerce form value `',first env))) |
| 337 |
(if (policy-assume-no-errors) |
| 338 |
unchecked |
| 339 |
`(let ((x ,unchecked)) |
| 340 |
(declare (,first x)) |
| 341 |
(unless (and ,@(expand-in-interval-p 'x (rest type))) |
| 342 |
(si::do-check-type x ',type nil "coerced value")) |
| 343 |
x)))) |
| 344 |
;; |
| 345 |
;; We did not find a suitable expansion. |
| 346 |
(t |
| 347 |
form) |
| 348 |
))) |
| 349 |
|
| 350 |
(define-compiler-macro coerce (&whole form value type &environment env) |
| 351 |
(expand-coerce form value type env)) |
| 352 |
|
| 353 |
(define-compiler-macro princ (&whole whole expression &optional stream &environment env) |
| 354 |
(if (constantp expression env) |
| 355 |
(let ((value (ext:constant-form-value expression env))) |
| 356 |
(typecase value |
| 357 |
((eql #\newline) |
| 358 |
`(terpri ,stream)) |
| 359 |
((string 1) |
| 360 |
`(princ ,(aref value 0) ,stream)) |
| 361 |
(otherwise |
| 362 |
whole))) |
| 363 |
whole)) |