Debian Codegraph @

src/cmp/cmptype-arith.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) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 ;;;; CMPTYPE-ARITH -- Operations upon and among types
11
12 (in-package "COMPILER")
13
14 ;;; CL-TYPE is any valid type specification of Common Lisp.
15 ;;;
16 ;;; TYPE is a representation type used by ECL. TYPE is one of:
17 ;;;
18 ;;; T(BOOLEAN)
19 ;;;
20 ;;; FIXNUM CHARACTER SINGLE-FLOAT DOUBLE-FLOAT
21 ;;; (VECTOR T) STRING BIT-VECTOR (VECTOR FIXNUM)
22 ;;; (VECTOR SINGLE-FLOAT) (VECTOR DOUBLE-FLOAT)
23 ;;; (ARRAY T) (ARRAY BASE-CHAR) (ARRAY BIT)
24 ;;; (ARRAY FIXNUM)
25 ;;; (ARRAY SINGLE-FLOAT) (ARRAY DOUBLE-FLOAT)
26 ;;; STANDARD-OBJECT STRUCTURE-OBJECT
27 ;;; SYMBOL
28 ;;; UNKNOWN
29 ;;;
30 ;;; NIL
31 ;;;
32 ;;;
33 ;;; immediate-type:
34 ;;; FIXNUM int
35 ;;; CHARACTER char
36 ;;; SINGLE-FLOAT float
37 ;;; DOUBLE-FLOAT double
38
39 (deftype any () 't)
40
41 (defun member-type (type disjoint-supertypes)
42 (member type disjoint-supertypes :test #'(lambda (t1 t2) (subtypep t1 t2 *cmp-env*))))
43
44 ;;; Canonicalize the object type to a type recognized by the compiler.
45 ;;; Depends on the implementation of TYPECASE.
46 (defun object-type (thing)
47 (typecase thing
48 (FIXNUM 'FIXNUM)
49 (SHORT-FLOAT 'SHORT-FLOAT)
50 (SINGLE-FLOAT 'SINGLE-FLOAT)
51 (DOUBLE-FLOAT 'DOUBLE-FLOAT)
52 (LONG-FLOAT 'LONG-FLOAT)
53 (NULL 'NULL)
54 (SYMBOL 'SYMBOL)
55 ((OR BASE-CHAR STANDARD-CHAR CHARACTER EXTENDED-CHAR) 'CHARACTER)
56 (BASE-STRING 'BASE-STRING)
57 (STRING 'STRING)
58 (BIT-VECTOR 'BIT-VECTOR)
59 (VECTOR (list 'VECTOR (array-element-type thing)))
60 (ARRAY (list 'ARRAY (array-element-type thing)))
61 #+clos (STANDARD-OBJECT 'STANDARD-OBJECT)
62 #+clos (STRUCTURE-OBJECT 'STRUCTURE-OBJECT)
63 #+sse2 (EXT:SSE-PACK 'EXT:SSE-PACK)
64 #+sse2 (EXT:INT-SSE-PACK 'EXT:INT-SSE-PACK)
65 #+sse2 (EXT:FLOAT-SSE-PACK 'EXT:FLOAT-SSE-PACK)
66 #+sse2 (EXT:DOUBLE-SSE-PACK 'EXT:DOUBLE-SSE-PACK)
67 (t t)))
68
69 (defun valid-type-specifier (type)
70 (handler-case
71 (if (subtypep type 'T *cmp-env*)
72 (values t type)
73 (values nil nil))
74 (error ()
75 (values nil nil))))
76
77 (defun known-type-p (type)
78 (subtypep type T *cmp-env*))
79
80 (defun trivial-type-p (type)
81 (subtypep T type *cmp-env*))
82
83 (defun-cached type-and (t1 t2) type-specifier=
84 ;; FIXME! Should we allow "*" as type name???
85 (when (or (eq t1 t2) (eq t2 '*))
86 (return-from type-and t1))
87 (when (eq t1 '*)
88 (return-from type-and t2))
89 (si::with-type-database ()
90 (let ((tag1 (si::safe-canonical-type t1 *cmp-env*))
91 (tag2 (si::safe-canonical-type t2 *cmp-env*)))
92 (cond ((and (numberp tag1) (numberp tag2))
93 (setf tag1 (si::safe-canonical-type t1 *cmp-env*)
94 tag2 (si::safe-canonical-type t2 *cmp-env*))
95 (cond ((zerop (logand tag1 tag2)) ; '(AND t1 t2) = NIL
96 NIL)
97 ((zerop (logandc2 tag1 tag2)) ; t1 <= t2
98 t1)
99 ((zerop (logandc2 tag2 tag1)) ; t2 <= t1
100 t2)
101 (t
102 `(AND ,t1 ,t2))))
103 ((eq tag1 'CONS)
104 (cmpwarn "Unsupported CONS type ~S. Replacing it with T." t1)
105 t2)
106 ((eq tag2 'CONS)
107 (cmpwarn "Unsupported CONS type ~S. Replacing it with T." t2)
108 t1)
109 ((null tag1)
110 ;(setf c::*compiler-break-enable* t) (break)
111 (cmpnote "Unknown type ~S. Assuming it is T." t1)
112 t2)
113 (t
114 ;(setf c::*compiler-break-enable* t) (break)
115 (cmpnote "Unknown type ~S. Assuming it is T." t2)
116 t1)))))
117
118 (defun values-number-from-type (type)
119 (cond ((or (eq type 'T) (eq type '*))
120 (values 0 MULTIPLE-VALUES-LIMIT))
121 ((or (atom type) (not (eq (first type) 'VALUES)))
122 (values 1 1))
123 ((or (member '&rest type) (member '&optional type))
124 (values 0 MULTIPLE-VALUES-LIMIT))
125 (t
126 (let ((l (1- (length type))))
127 (values l l)))))
128
129 (defun-cached values-type-primary-type (type) type-specifier=
130 ;; Extract the type of the first value returned by this form. We are
131 ;; pragmatic and thus (VALUES) => NULL [CHECKME!]
132 (let (aux)
133 (cond ((or (atom type)
134 (not (eq (first type) 'VALUES)))
135 type)
136 ((null (setf aux (rest type)))
137 'NULL)
138 ((member (setf aux (first aux))
139 '(&optional &rest &allow-other-keys))
140 (setf aux (do-values-type-to-n-types type 1))
141 (if aux (first aux) 'null))
142 (t
143 aux))))
144
145 (defun-cached values-type-to-n-types (type length) type-specifier=
146 (when (plusp length)
147 (do-values-type-to-n-types type length)))
148
149 (defun do-values-type-to-n-types (type length)
150 (declare (si::c-local))
151 (multiple-value-bind (required optional rest)
152 (split-values-type type)
153 (let* ((optional (loop for i in optional
154 collect (if (eq i t) i `(or null ,i))))
155 (output (nconc required optional))
156 (l (length output)))
157 (if (< l length)
158 (nconc output (make-list (- length l)
159 :initial-element (if rest (first rest) t)))
160 (subseq output 0 length)))))
161
162 (defun split-values-type (type)
163 (if (or (atom type) (not (eq (first type) 'VALUES)))
164 (values (list type) nil nil nil)
165 (loop with required = '()
166 with optional-flag = nil
167 with optional = '()
168 with rest = '()
169 with a-o-k = nil
170 with l = (rest type)
171 while l
172 do (let ((typespec (pop l)))
173 (case typespec
174 (&allow-other-keys
175 (setf a-o-k t)
176 (when l
177 (cmperr "Syntax error in type expression ~S" type)))
178 (&optional
179 (if optional-flag
180 (push typespec optional)
181 (setf optional-flag t)))
182 (&rest
183 (when (or (null l)
184 (not (member (rest l) '(() (&allow-other-keys))
185 :test #'equal)))
186 (cmperr "Syntax error in type expression ~S" type))
187 (setf rest (list (car l))))
188 (otherwise
189 (if optional-flag
190 (push typespec optional)
191 (push typespec required)))))
192 finally
193 (return (values (nreverse required) (nreverse optional)
194 rest a-o-k)))))
195
196 (defun-cached values-type-or (t1 t2) type-specifier=
197 (when (or (eq t2 'T) (equalp t2 '(VALUES &REST T)))
198 (return-from values-type-or t2))
199 (when (or (eq t1 'T) (equalp t1 '(VALUES &REST T)))
200 (return-from values-type-or t1))
201 (unless t1
202 (return-from values-type-or t2))
203 (unless t2
204 (return-from values-type-or t1))
205 (multiple-value-bind (req1 opt1 rest1)
206 (split-values-type t1)
207 (multiple-value-bind (req2 opt2 rest2)
208 (split-values-type t2)
209 (let ((req '())
210 (opt '())
211 (rest '()))
212 (loop for t1 in req1
213 do (cond (req2
214 (push (type-or t1 (pop req2)) req))
215 (opt2
216 (push (type-or t1 (pop opt2)) opt))
217 (rest2
218 (push (type-or t1 (first rest2)) opt))
219 (t
220 (push t1 opt))))
221 (loop for t1 in opt1
222 do (cond (req2
223 (push (type-or t1 (pop req2)) opt))
224 (opt2
225 (push (type-or t1 (pop opt2)) opt))
226 (rest2
227 (push (type-or t1 (first rest2)) opt))
228 (t
229 (push t1 opt))))
230 (let ((t1 (if rest1 (first rest1) t)))
231 (loop for t2 in req2
232 do (push (type-or t1 t2) opt))
233 (loop for t2 in opt2
234 do (push (type-or t1 t2) opt))
235 (if rest2
236 (setf rest (list (type-or t1 (first rest2))))
237 (setf rest rest1)))
238 `(VALUES ,@(nreverse req)
239 ,@(and opt (cons '&optional (nreverse opt)))
240 ,@(and rest (cons '&optional rest)))))))
241
242 (defun-cached values-type-and (t1 t2) type-specifier=
243 (when (or (eq t2 'T) (equalp t2 '(VALUES &REST T)))
244 (return-from values-type-and t1))
245 (when (or (eq t1 'T) (equalp t1 '(VALUES &REST T)))
246 (return-from values-type-and t2))
247 (when (or (null t1) (null t2))
248 (return-from values-type-and nil))
249 (multiple-value-bind (req1 opt1 rest1)
250 (split-values-type t1)
251 (multiple-value-bind (req2 opt2 rest2)
252 (split-values-type t2)
253 (let ((req '())
254 (opt '())
255 (rest '()))
256 (loop for t1 in req1
257 do (cond (req2 (push (type-and t1 (pop req2)) req))
258 (opt2 (push (type-and t1 (pop opt2)) req))
259 (rest2 (push (type-and t1 (first rest2)) req))
260 (t (setf opt1 nil rest1 nil) (return))))
261 (loop for t1 in opt1
262 do (cond (req2 (push (type-and t1 (pop req2)) req))
263 (opt2 (push (type-and t1 (pop opt2)) opt))
264 (rest2 (push (type-and t1 (first rest2)) opt))
265 (t (setf opt1 nil rest1 nil) (return))))
266 (when rest1
267 (let ((t1 (first rest1)))
268 (loop for t2 in req2
269 do (push (type-and t1 t2) req))
270 (loop for t2 in opt2
271 do (push (type-and t1 t2) opt))
272 (when rest2
273 (setf rest (list (type-and t1 (first rest2)))))))
274 `(VALUES ,@(nreverse req)
275 ,@(and opt (cons '&optional (nreverse opt)))
276 ,@(and rest (cons '&optional rest)))))))
277
278 (defun-cached type-or (t1 t2) type-specifier=
279 ;; FIXME! Should we allow "*" as type name???
280 (when (or (eq t1 t2) (eq t2 '*))
281 (return-from type-or t1))
282 (when (eq t1 '*)
283 (return-from type-or t2))
284 (si::with-type-database ()
285 (let ((tag1 (si::safe-canonical-type t1 *cmp-env*))
286 (tag2 (si::safe-canonical-type t2 *cmp-env*)))
287 (cond ((and (numberp tag1) (numberp tag2))
288 (setf tag1 (si::safe-canonical-type t1 *cmp-env*)
289 tag2 (si::safe-canonical-type t2 *cmp-env*))
290 (cond ((zerop (logandc2 tag1 tag2)) ; t1 <= t2
291 t2)
292 ((zerop (logandc2 tag2 tag1)) ; t2 <= t1
293 t1)
294 (t
295 `(OR ,t1 ,t2))))
296 ((eq tag1 'CONS)
297 (cmpwarn "Unsupported CONS type ~S. Replacing it with T." t1)
298 T)
299 ((eq tag2 'CONS)
300 (cmpwarn "Unsupported CONS type ~S. Replacing it with T." t2)
301 T)
302 ((null tag1)
303 ;(break)
304 (cmpnote "Unknown type ~S" t1)
305 T)
306 (t
307 ;(break)
308 (cmpnote "Unknown type ~S" t2)
309 T)))))
310
311 (defun type>= (type1 type2 &optional env)
312 (subtypep type2 type1 env))
313
314 (defun type-false-p (type &optional env) (subtypep type 'null env))
315 (defun type-true-p (type &optional env) (subtypep type '(not null) env))