Debian Codegraph @

src/cmp/cmparray.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 ;;;; CMPARRAY. Optimizations related to arrays
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 ;;; MAKE-ARRAY
15 ;;;
16
17 (defun guess-array-element-type (element-type)
18 (if (and (setf element-type (extract-constant-value element-type))
19 (known-type-p element-type))
20 (upgraded-array-element-type element-type *cmp-env*)
21 '*))
22
23 (defun guess-array-dimensions-type (orig-dimensions &aux dimensions)
24 (and (consp orig-dimensions)
25 (eq (first dimensions) 'LIST)
26 (let ((l (list-length orig-dimensions)))
27 (when (and l (< -1 l array-rank-limit))
28 (return-from guess-array-dimensions-type
29 (make-list (1- l) :initial-element '*)))))
30 (let ((dimensions (extract-constant-value orig-dimensions :failed)))
31 (cond ((eq dimensions ':failed)
32 '*)
33 ((typep dimensions 'ext:array-index *cmp-env*)
34 (list dimensions))
35 ((and (listp dimensions)
36 (let ((rank (list-length dimensions)))
37 (and (numberp rank)
38 (< -1 rank array-rank-limit)
39 (every #'(lambda (x) (typep x 'ext:array-index *cmp-env*)) dimensions)
40 (< (apply '* dimensions) array-total-size-limit))))
41 dimensions)
42 (t
43 (cmpwarn "The first argument to MAKE-ARRAY~%~A~%is not a valid set of dimensions" orig-dimensions)
44 '*))))
45
46 (define-compiler-macro* make-array (&whole form dimensions &key (element-type t)
47 (initial-element nil initial-element-supplied-p)
48 (initial-contents nil initial-contents-supplied-p)
49 adjustable fill-pointer
50 displaced-to (displaced-index-offset 0))
51 ;; This optimization is always done unless we provide content. There
52 ;; is no speed, debug or space reason not to do it, unless the user
53 ;; specifies not to inline MAKE-ARRAY, but in that case the compiler
54 ;; macro should not be used.
55 (let* ((dimensions-type (guess-array-dimensions-type dimensions))
56 (guessed-element-type (guess-array-element-type element-type)))
57 (unless initial-contents-supplied-p
58 ;; If the type is known and we can assume it will not change, we
59 ;; replace it with the upgraded form.
60 (unless (eq guessed-element-type '*)
61 (setf element-type `',guessed-element-type))
62 ;; Now we choose between making a vector or making a general array.
63 ;; It only saves some time, since MAKE-PURE-ARRAY will call MAKE-VECTOR
64 ;; if a one-dimensional array is to be created.
65 (let ((function 'si:make-pure-array))
66 (when (and (listp dimensions-type)
67 (null (rest dimensions-type))
68 (integerp (first dimensions-type)))
69 (setf function 'si:make-vector
70 dimensions (first dimensions-type)))
71 (setf form
72 `(,function ,%element-type ,%dimensions ,%adjustable ,%fill-pointer
73 ,%displaced-to ,%displaced-index-offset)))
74 ;; Then we may fill the array with a given value
75 (when initial-element-supplied-p
76 (setf form `(si:fill-array-with-elt ,form ,%initial-element 0 nil)))
77 (setf form `(ext:truly-the (array ,guessed-element-type ,dimensions-type)
78 ,form))))
79 form)
80
81 ;;;
82 ;;; VECTOR-PUSH and VECTOR-PUSH-EXTEND
83 ;;;
84
85 (defun expand-vector-push (whole env extend &aux (args (rest whole)))
86 (declare (si::c-local)
87 (ignore env))
88 (ext:with-clean-symbols (value vector index dimension)
89 (when (or (eq (first args) 'value) ; No infinite recursion
90 (not (policy-open-code-aref/aset)))
91 (return-from expand-vector-push
92 whole))
93 (unless (<= 2
94 (length args)
95 (if extend 3 2))
96 (cmpwarn "Wrong number of arguments passed to function ~A in form: ~A" (first whole) whole)
97 (return-from expand-vector-push
98 `(si:simple-program-error
99 "Wrong number of arguments passed to function ~A in form: ~A" ',(first whole) ',whole)))
100 `(let* ((value ,(car args))
101 (vector ,(second args)))
102 (declare (:read-only value vector)
103 (optimize (safety 0)))
104 (optional-type-assertion vector vector)
105 (let ((index (fill-pointer vector))
106 (dimension (array-total-size vector)))
107 (declare (fixnum index dimension)
108 (:read-only index dimension))
109 (cond ((< index dimension)
110 (si:fill-pointer-set vector (ext:truly-the fixnum (+ 1 index)))
111 (si:aset vector index value)
112 index)
113 (t ,(if extend
114 `(vector-push-extend value vector ,@(cddr args))
115 nil)))))))
116
117 (define-compiler-macro vector-push (&whole whole &rest args &environment env)
118 (expand-vector-push whole env nil))
119
120 (define-compiler-macro vector-push-extend (&whole whole &rest args &environment env)
121 (expand-vector-push whole env t))
122
123 ;;;
124 ;;; AREF/ASET
125 ;;;
126
127 (define-compiler-macro aref (&whole form array &rest indices &environment env)
128 (if (policy-open-code-aref/aset env)
129 (expand-aref array indices env)
130 form))
131
132 (defun expand-aref (array indices env)
133 (ext:with-clean-symbols (%array)
134 `(let ((%array ,array))
135 (declare (:read-only %array)
136 (optimize (safety 0)))
137 (row-major-aref %array
138 ,(expand-row-major-index '%array indices env)))))
139
140 (define-compiler-macro si:aset (&whole form array &rest indices-and-value
141 &environment env)
142 (cond ((null indices-and-value)
143 (cmpwarn "Too few arguments to SI:ASET form~%~4I~A" form)
144 form)
145 ((policy-open-code-aref/aset env)
146 (let* ((indices (butlast indices-and-value))
147 (value (first (last indices-and-value))))
148 (expand-aset array indices value env)))
149 (t
150 form)))
151
152 (defun expand-aset (array indices value env)
153 (ext:with-unique-names (%array)
154 `(let* ((,%array ,array))
155 (declare (:read-only ,%array)
156 (optimize (safety 0)))
157 (si:row-major-aset ,%array ,(expand-row-major-index %array indices env) ,value))))
158
159 (define-compiler-macro array-row-major-index (&whole form array &rest indices &environment env)
160 (if (policy-open-code-aref/aset env)
161 (ext:with-clean-symbols (%array)
162 `(let ((%array ,array))
163 (declare (:read-only %array)
164 (optimize (safety 0)))
165 ,(expand-row-major-index '%array indices env)))
166 form))
167
168 (defun expand-zero-dim-index-check (a env)
169 (if (policy-type-assertions env)
170 `(progn
171 (optional-type-assertion ,a array)
172 (check-expected-rank ,a 0)
173 0)
174 0))
175
176 (defun expand-vector-index-check (a index env)
177 (flet ((expansion (a index)
178 `(progn
179 (optional-type-assertion ,a vector)
180 (check-vector-in-bounds ,a ,index)
181 ,index)))
182 (if (policy-type-assertions env)
183 (ext:with-clean-symbols (%array-index)
184 `(let ((%array-index ,index))
185 (declare (:read-only %array-index))
186 ,(expansion a '%array-index)))
187 index)))
188
189 (defun expand-row-major-index (a indices env)
190 (when (null indices)
191 (return-from expand-row-major-index
192 (expand-zero-dim-index-check a env)))
193 (when (null (rest indices))
194 (return-from expand-row-major-index
195 (expand-vector-index-check a (first indices) env)))
196 (let* ((expected-rank (length indices))
197 (check (policy-array-bounds-check env))
198 (dims (loop for i from 0
199 for index in indices
200 collect `(,(gentemp "DIM") (array-dimension-fast ,a ,i))))
201 (dim-names (mapcar #'first dims)))
202 (ext:with-clean-symbols (%ndx-var %output-var %dim-var)
203 `(let* (,@dims
204 (%output-var 0))
205 (declare (type ext:array-index %output-var ,@dim-names)
206 (ignorable ,@dim-names))
207 ,@(when (policy-type-assertions env)
208 `((optional-type-assertion ,a array)
209 (check-expected-rank ,a ,expected-rank)))
210 ,@(loop for i from 0
211 for l in indices
212 for index in indices
213 for dim-var in dim-names
214 when (plusp i)
215 collect `(setf %output-var
216 (ext:truly-the ext:array-index (* %output-var ,dim-var)))
217 collect `(let ((%ndx-var ,index))
218 (declare (ext:array-index %ndx-var))
219 ,(and check `(check-index-in-bounds ,a %ndx-var ,dim-var))
220 (setf %output-var
221 (ext:truly-the ext:array-index (+ %output-var %ndx-var)))))
222 %output-var))))
223
224 (defmacro check-expected-rank (a expected-rank)
225 `(ffi:c-inline
226 (,a ,expected-rank) (:object :fixnum) :void
227 "if (ecl_unlikely((#0)->array.rank != (#1)))
228 FEwrong_dimensions(#0,#1);"
229 :one-liner nil))
230
231 (defmacro check-index-in-bounds (array index limit)
232 `(ffi:c-inline
233 (,array ,index ,limit) (:object :fixnum :fixnum) :void
234 "if (ecl_unlikely((#1)>=(#2)))
235 FEwrong_index(ECL_NIL,#0,-1,ecl_make_fixnum(#1),#2);"
236 :one-liner nil))
237
238 (defmacro check-vector-in-bounds (vector index)
239 `(ffi:c-inline
240 (,vector ,index) (:object :fixnum) :void
241 "if (ecl_unlikely((#1)>=(#0)->vector.dim))
242 FEwrong_index(ECL_NIL,#0,-1,ecl_make_fixnum(#1),(#0)->vector.dim);"
243 :one-liner nil))
244
245 (defconstant +array-dimension-accessor+
246 '#.(loop for i from 0 below array-rank-limit
247 collect (format nil "(#0)->array.dims[~D]" i)))
248
249 (defun array-dimension-accessor (array n)
250 (let ((tails #.(apply 'vector
251 (loop for i from 0 below array-rank-limit
252 for c-code = (format nil "(#0)->array.dims[~D]" i)
253 collect `((:object) :fixnum ,c-code :one-liner t
254 :side-effects nil)))))
255 `(ffi:c-inline (,array) ,@(aref tails n))))
256
257 (defmacro array-dimension-fast (array n)
258 (if (typep n '(integer 0 #.(1- array-rank-limit)) *cmp-env*)
259 (array-dimension-accessor array n)
260 (error "In macro ARRAY-DIMENSION-FAST, the index is not a constant integer: ~A"
261 n)))