[dcb7db]: src / compiler / generic / primtype.lisp Maximize Restore History

Download this file

primtype.lisp    379 lines (357 with data), 13.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
;;;; machine-independent aspects of the object representation and
;;;; primitive types
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB!VM")
;;;; primitive type definitions
(/show0 "primtype.lisp 17")
(!def-primitive-type t (descriptor-reg))
(/show0 "primtype.lisp 20")
(setf *backend-t-primitive-type* (primitive-type-or-lose t))
;;; primitive integer types that fit in registers
(/show0 "primtype.lisp 24")
(!def-primitive-type positive-fixnum (any-reg signed-reg unsigned-reg)
:type (unsigned-byte #.sb!vm:n-positive-fixnum-bits))
(/show0 "primtype.lisp 27")
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 32) '(and) '(or))
(!def-primitive-type unsigned-byte-31 (signed-reg unsigned-reg descriptor-reg)
:type (unsigned-byte 31))
(/show0 "primtype.lisp 31")
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 32) '(and) '(or))
(!def-primitive-type unsigned-byte-32 (unsigned-reg descriptor-reg)
:type (unsigned-byte 32))
(/show0 "primtype.lisp 35")
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(!def-primitive-type unsigned-byte-63 (signed-reg unsigned-reg descriptor-reg)
:type (unsigned-byte 63))
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(!def-primitive-type unsigned-byte-64 (unsigned-reg descriptor-reg)
:type (unsigned-byte 64))
(!def-primitive-type fixnum (any-reg signed-reg)
:type (signed-byte #.(1+ sb!vm:n-positive-fixnum-bits)))
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 32) '(and) '(or))
(!def-primitive-type signed-byte-32 (signed-reg descriptor-reg)
:type (signed-byte 32))
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(!def-primitive-type signed-byte-64 (signed-reg descriptor-reg)
:type (signed-byte 64))
(defvar *fixnum-primitive-type* (primitive-type-or-lose 'fixnum))
(/show0 "primtype.lisp 53")
(!def-primitive-type-alias tagged-num (:or positive-fixnum fixnum))
(!def-primitive-type-alias unsigned-num
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(:or unsigned-byte-64 unsigned-byte-63 positive-fixnum)
#!-#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(:or unsigned-byte-32 unsigned-byte-31 positive-fixnum))
(!def-primitive-type-alias signed-num
#!+#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(:or signed-byte-64 fixnum unsigned-byte-63 positive-fixnum)
#!-#.(cl:if (cl:= sb!vm::n-machine-word-bits 64) '(and) '(or))
(:or signed-byte-32 fixnum unsigned-byte-31 positive-fixnum))
;;; other primitive immediate types
(/show0 "primtype.lisp 68")
(!def-primitive-type character (character-reg any-reg))
;;; primitive pointer types
(/show0 "primtype.lisp 73")
(!def-primitive-type function (descriptor-reg))
(!def-primitive-type list (descriptor-reg))
(!def-primitive-type instance (descriptor-reg))
(/show0 "primtype.lisp 77")
(!def-primitive-type funcallable-instance (descriptor-reg))
;;; primitive other-pointer number types
(/show0 "primtype.lisp 81")
(!def-primitive-type bignum (descriptor-reg))
(!def-primitive-type ratio (descriptor-reg))
(!def-primitive-type complex (descriptor-reg))
(/show0 "about to !DEF-PRIMITIVE-TYPE SINGLE-FLOAT")
(!def-primitive-type single-float (single-reg descriptor-reg))
(/show0 "about to !DEF-PRIMITIVE-TYPE DOUBLE-FLOAT")
(!def-primitive-type double-float (double-reg descriptor-reg))
(/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-SINGLE-FLOAT")
(!def-primitive-type complex-single-float (complex-single-reg descriptor-reg)
:type (complex single-float))
(/show0 "about to !DEF-PRIMITIVE-TYPE COMPLEX-DOUBLE-FLOAT")
(!def-primitive-type complex-double-float (complex-double-reg descriptor-reg)
:type (complex double-float))
;;; primitive other-pointer array types
(/show0 "primtype.lisp 96")
(macrolet ((define-simple-array-primitive-types ()
`(progn
,@(map 'list
(lambda (saetp)
`(!def-primitive-type
,(saetp-primitive-type-name saetp)
(descriptor-reg)
:type (simple-array ,(saetp-specifier saetp) (*))))
*specialized-array-element-type-properties*))))
(define-simple-array-primitive-types))
;;; Note: The complex array types are not included, 'cause it is
;;; pointless to restrict VOPs to them.
;;; other primitive other-pointer types
(!def-primitive-type system-area-pointer (sap-reg descriptor-reg))
(!def-primitive-type weak-pointer (descriptor-reg))
;;; miscellaneous primitive types that don't exist at the LISP level
(!def-primitive-type catch-block (catch-block) :type nil)
;;;; PRIMITIVE-TYPE-OF and friends
;;; Return the most restrictive primitive type that contains OBJECT.
(/show0 "primtype.lisp 147")
(!def-vm-support-routine primitive-type-of (object)
(let ((type (ctype-of object)))
(cond ((not (member-type-p type)) (primitive-type type))
((equal (member-type-members type) '(nil))
(primitive-type-or-lose 'list))
(t
*backend-t-primitive-type*))))
;;; Return the primitive type corresponding to a type descriptor
;;; structure. The second value is true when the primitive type is
;;; exactly equivalent to the argument Lisp type.
;;;
;;; In a bootstrapping situation, we should be careful to use the
;;; correct values for the system parameters.
;;;
;;; We need an aux function because we need to use both
;;; !DEF-VM-SUPPORT-ROUTINE and DEFUN-CACHED.
(/show0 "primtype.lisp 188")
(!def-vm-support-routine primitive-type (type)
(primitive-type-aux type))
(/show0 "primtype.lisp 191")
(defun-cached (primitive-type-aux
:hash-function (lambda (x)
(logand (type-hash-value x) #x1FF))
:hash-bits 9
:values 2
:default (values nil :empty))
((type eq))
(declare (type ctype type))
(macrolet ((any () '(values *backend-t-primitive-type* nil))
(exactly (type)
`(values (primitive-type-or-lose ',type) t))
(part-of (type)
`(values (primitive-type-or-lose ',type) nil)))
(flet ((maybe-numeric-type-union (t1 t2)
(let ((t1-name (primitive-type-name t1))
(t2-name (primitive-type-name t2)))
(case t1-name
(positive-fixnum
(if (or (eq t2-name 'fixnum)
(eq t2-name
(ecase sb!vm::n-machine-word-bits
(32 'signed-byte-32)
(64 'signed-byte-64)))
(eq t2-name
(ecase sb!vm::n-machine-word-bits
(32 'unsigned-byte-31)
(64 'unsigned-byte-63)))
(eq t2-name
(ecase sb!vm::n-machine-word-bits
(32 'unsigned-byte-32)
(64 'unsigned-byte-64))))
t2))
(fixnum
(case t2-name
(#.(ecase sb!vm::n-machine-word-bits
(32 'signed-byte-32)
(64 'signed-byte-64))
t2)
(#.(ecase sb!vm::n-machine-word-bits
(32 'unsigned-byte-31)
(64 'unsigned-byte-63))
(primitive-type-or-lose
(ecase sb!vm::n-machine-word-bits
(32 'signed-byte-32)
(64 'signed-byte-64))))))
(#.(ecase sb!vm::n-machine-word-bits
(32 'signed-byte-32)
(64 'signed-byte-64))
(if (eq t2-name
(ecase sb!vm::n-machine-word-bits
(32 'unsigned-byte-31)
(64 'unsigned-byte-63)))
t1))
(#.(ecase sb!vm::n-machine-word-bits
(32 'unsigned-byte-31)
(64 'unsigned-byte-63))
(if (eq t2-name
(ecase sb!vm::n-machine-word-bits
(32 'unsigned-byte-32)
(64 'unsigned-byte-64)))
t2))))))
(etypecase type
(numeric-type
(let ((lo (numeric-type-low type))
(hi (numeric-type-high type)))
(case (numeric-type-complexp type)
(:real
(case (numeric-type-class type)
(integer
(cond ((and hi lo)
(dolist (spec
`((positive-fixnum 0 ,sb!xc:most-positive-fixnum)
,@(ecase sb!vm::n-machine-word-bits
(32
`((unsigned-byte-31
0 ,(1- (ash 1 31)))
(unsigned-byte-32
0 ,(1- (ash 1 32)))))
(64
`((unsigned-byte-63
0 ,(1- (ash 1 63)))
(unsigned-byte-64
0 ,(1- (ash 1 64))))))
(fixnum ,sb!xc:most-negative-fixnum
,sb!xc:most-positive-fixnum)
,(ecase sb!vm::n-machine-word-bits
(32
`(signed-byte-32 ,(ash -1 31)
,(1- (ash 1 31))))
(64
`(signed-byte-64 ,(ash -1 63)
,(1- (ash 1 63))))))
(if (or (< hi sb!xc:most-negative-fixnum)
(> lo sb!xc:most-positive-fixnum))
(part-of bignum)
(any)))
(let ((type (car spec))
(min (cadr spec))
(max (caddr spec)))
(when (<= min lo hi max)
(return (values
(primitive-type-or-lose type)
(and (= lo min) (= hi max))))))))
((or (and hi (< hi sb!xc:most-negative-fixnum))
(and lo (> lo sb!xc:most-positive-fixnum)))
(part-of bignum))
(t
(any))))
(float
(let ((exact (and (null lo) (null hi))))
(case (numeric-type-format type)
((short-float single-float)
(values (primitive-type-or-lose 'single-float)
exact))
((double-float)
(values (primitive-type-or-lose 'double-float)
exact))
(t
(any)))))
(t
(any))))
(:complex
(if (eq (numeric-type-class type) 'float)
(let ((exact (and (null lo) (null hi))))
(case (numeric-type-format type)
((short-float single-float)
(values (primitive-type-or-lose 'complex-single-float)
exact))
((double-float long-float)
(values (primitive-type-or-lose 'complex-double-float)
exact))
(t
(part-of complex))))
(part-of complex)))
(t
(any)))))
(array-type
(if (array-type-complexp type)
(any)
(let* ((dims (array-type-dimensions type))
(etype (array-type-specialized-element-type type))
(type-spec (type-specifier etype))
;; FIXME: We're _WHAT_? Testing for type equality
;; with a specifier and #'EQUAL? *BOGGLE*. --
;; CSR, 2003-06-24
(ptype (cdr (assoc type-spec *simple-array-primitive-types*
:test #'equal))))
(if (and (consp dims) (null (rest dims)) ptype)
(values (primitive-type-or-lose ptype)
(eq (first dims) '*))
(any)))))
(union-type
(if (type= type (specifier-type 'list))
(exactly list)
(let ((types (union-type-types type)))
(multiple-value-bind (res exact) (primitive-type (first types))
(dolist (type (rest types) (values res exact))
(multiple-value-bind (ptype ptype-exact)
(primitive-type type)
(unless ptype-exact (setq exact nil))
(unless (eq ptype res)
(let ((new-ptype
(or (maybe-numeric-type-union res ptype)
(maybe-numeric-type-union ptype res))))
(if new-ptype
(setq res new-ptype)
(return (any)))))))))))
(intersection-type
(let ((types (intersection-type-types type))
(res (any))
(exact nil))
(dolist (type types (values res exact))
(when (eq type (specifier-type 'function))
;; KLUDGE: Deal with (and function instance), both of which
;; have an exact primitive type.
(return (part-of function)))
(multiple-value-bind (ptype ptype-exact)
(primitive-type type)
(when ptype-exact
;; Apart from the previous kludge exact primitive
;; types should match, if indeed there are any. It
;; may be that this assumption isn't really safe,
;; but at least we'll see what breaks. -- NS 20041104
(aver (or (not exact) (eq ptype res)))
(setq exact t))
(when (or ptype-exact (and (not exact) (eq res (any))))
;; Try to find a narrower representation then
;; (any). Takes care of undecidable types in
;; intersections with decidable ones.
(setq res ptype))))))
(member-type
(let* ((members (member-type-members type))
(res (primitive-type-of (first members))))
(dolist (mem (rest members) (values res nil))
(let ((ptype (primitive-type-of mem)))
(unless (eq ptype res)
(let ((new-ptype (or (maybe-numeric-type-union res ptype)
(maybe-numeric-type-union ptype res))))
(if new-ptype
(setq res new-ptype)
(return (any)))))))))
(named-type
(ecase (named-type-name type)
((t *) (values *backend-t-primitive-type* t))
((nil) (any))))
(character-set-type
(let ((pairs (character-set-type-pairs type)))
(if (and (= (length pairs) 1)
(= (caar pairs) 0)
(= (cdar pairs) (1- sb!xc:char-code-limit)))
(exactly character)
(part-of character))))
(built-in-classoid
(case (classoid-name type)
((complex function instance
system-area-pointer weak-pointer)
(values (primitive-type-or-lose (classoid-name type)) t))
(funcallable-instance
(part-of function))
(cons-type
(part-of list))
(t
(any))))
(fun-type
(exactly function))
(classoid
(if (csubtypep type (specifier-type 'function))
(part-of function)
(part-of instance)))
(ctype
(if (csubtypep type (specifier-type 'function))
(part-of function)
(any)))))))
(/show0 "primtype.lisp end of file")