0.7.13.pcl-class.5
[sbcl/simd.git] / src / pcl / low.lisp
blob871b1705c12749779fad7e07a0c27e3a46477a8a
1 ;;;; This file contains portable versions of low-level functions and macros
2 ;;;; which are ripe for implementation specific customization. None of the code
3 ;;;; in this file *has* to be customized for a particular Common Lisp
4 ;;;; implementation. Moreover, in some implementations it may not make any
5 ;;;; sense to customize some of this code.
6 ;;;;
7 ;;;; The original version was intended to support portable customization to
8 ;;;; lotso different Lisp implementations. This functionality is gone in the
9 ;;;; current version, and it now runs only under SBCL. (Now that ANSI Common
10 ;;;; Lisp has mixed CLOS into the insides of the system (e.g. error handling
11 ;;;; and printing) so deeply that it's not very meaningful to bootstrap Common
12 ;;;; Lisp without CLOS, the old functionality is of dubious use. -- WHN
13 ;;;; 19981108)
15 ;;;; This software is part of the SBCL system. See the README file for more
16 ;;;; information.
18 ;;;; This software is derived from software originally released by Xerox
19 ;;;; Corporation. Copyright and release statements follow. Later modifications
20 ;;;; to the software are in the public domain and are provided with
21 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
22 ;;;; information.
24 ;;;; copyright information from original PCL sources:
25 ;;;;
26 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
27 ;;;; All rights reserved.
28 ;;;;
29 ;;;; Use and copying of this software and preparation of derivative works based
30 ;;;; upon this software are permitted. Any distribution of this software or
31 ;;;; derivative works must comply with all applicable United States export
32 ;;;; control laws.
33 ;;;;
34 ;;;; This software is made available AS IS, and Xerox Corporation makes no
35 ;;;; warranty about the software, its performance or its conformity to any
36 ;;;; specification.
38 (in-package "SB-PCL")
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed*
42 '(optimize (speed 3) (safety 0)))
43 ) ; EVAL-WHEN
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46 `(dotimes (,var (the fixnum ,count) ,result)
47 (declare (fixnum ,var))
48 ,@body))
50 ;;;; early definition of WRAPPER
51 ;;;;
52 ;;;; Most WRAPPER stuff is defined later, but the DEFSTRUCT itself
53 ;;;; is here early so that things like (TYPEP .. 'WRAPPER) can be
54 ;;;; compiled efficiently.
56 ;;; Note that for SBCL, as for CMU CL, the WRAPPER of a built-in or
57 ;;; structure class will be some other kind of SB-KERNEL:LAYOUT, but
58 ;;; this shouldn't matter, since the only two slots that WRAPPER adds
59 ;;; are meaningless in those cases.
60 (defstruct (wrapper
61 (:include layout
62 ;; KLUDGE: In CMU CL, the initialization default
63 ;; for LAYOUT-INVALID was NIL. In SBCL, that has
64 ;; changed to :UNINITIALIZED, but PCL code might
65 ;; still expect NIL for the initialization
66 ;; default of WRAPPER-INVALID. Instead of trying
67 ;; to find out, I just overrode the LAYOUT
68 ;; default here. -- WHN 19991204
69 (invalid nil))
70 (:conc-name %wrapper-)
71 (:constructor make-wrapper-internal)
72 (:copier nil))
73 (instance-slots-layout nil :type list)
74 (class-slots nil :type list))
75 #-sb-fluid (declaim (sb-ext:freeze-type wrapper))
77 ;;;; PCL's view of funcallable instances
79 (!defstruct-with-alternate-metaclass pcl-funcallable-instance
80 ;; KLUDGE: Note that neither of these slots is ever accessed by its
81 ;; accessor name as of sbcl-0.pre7.63. Presumably everything works
82 ;; by puns based on absolute locations. Fun fun fun.. -- WHN 2001-10-30
83 :slot-names (clos-slots name hash-code)
84 :boa-constructor %make-pcl-funcallable-instance
85 :superclass-name funcallable-instance
86 :metaclass-name random-pcl-classoid
87 :metaclass-constructor make-random-pcl-classoid
88 :dd-type funcallable-structure
89 ;; Only internal implementation code will access these, and these
90 ;; accesses (slot readers in particular) could easily be a
91 ;; bottleneck, so it seems reasonable to suppress runtime type
92 ;; checks.
94 ;; (Except note KLUDGE above that these accessors aren't used at all
95 ;; (!) as of sbcl-0.pre7.63, so for now it's academic.)
96 :runtime-type-checks-p nil)
98 (import 'sb-kernel:funcallable-instance-p)
100 (defun set-funcallable-instance-function (fin new-value)
101 (declare (type function new-value))
102 (aver (funcallable-instance-p fin))
103 (setf (funcallable-instance-fun fin) new-value))
104 (defmacro fsc-instance-p (fin)
105 `(funcallable-instance-p ,fin))
106 (defmacro fsc-instance-wrapper (fin)
107 `(%funcallable-instance-layout ,fin))
108 ;;; FIXME: This seems to bear no relation at all to the CLOS-SLOTS
109 ;;; slot in the FUNCALLABLE-INSTANCE structure, above, which
110 ;;; (bizarrely) seems to be set to the NAME of the
111 ;;; FUNCALLABLE-INSTANCE. At least, the index 1 seems to return the
112 ;;; NAME, and the index 2 NIL. Weird. -- CSR, 2002-11-07
113 (defmacro fsc-instance-slots (fin)
114 `(%funcallable-instance-info ,fin 0))
115 (defmacro fsc-instance-hash (fin)
116 `(%funcallable-instance-info ,fin 3))
118 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
119 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
120 (defun clos-slots-ref (slots index)
121 (svref slots index))
122 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
123 (defun (setf clos-slots-ref) (new-value slots index)
124 (setf (svref slots index) new-value))
126 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
127 ;;; is only used to discriminate between functions (including FINs)
128 ;;; and normal instances, so we can return true on structures also. A
129 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
130 ;;; PCL-INSTANCE-P.
131 (defmacro std-instance-p (x)
132 `(%instancep ,x))
134 ;; a temporary definition used for debugging the bootstrap
135 #+sb-show
136 (defun print-std-instance (instance stream depth)
137 (declare (ignore depth))
138 (print-unreadable-object (instance stream :type t :identity t)
139 (let ((class (class-of instance)))
140 (when (or (eq class (find-class 'standard-class nil))
141 (eq class (find-class 'funcallable-standard-class nil))
142 (eq class (find-class 'built-in-class nil)))
143 (princ (early-class-name instance) stream)))))
145 ;;; This is the value that we stick into a slot to tell us that it is
146 ;;; unbound. It may seem gross, but for performance reasons, we make
147 ;;; this an interned symbol. That means that the fast check to see
148 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
149 ;;; That is considerably faster than looking at the value of a special
150 ;;; variable. Be careful, there are places in the code which actually
151 ;;; use ..SLOT-UNBOUND.. rather than this variable. So much for
152 ;;; modularity..
154 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use
155 ;;; the SBCL built-in unbound value token instead. Perhaps if we did
156 ;;; so it would be a good idea to define collections of CLOS slots as
157 ;;; a new type of heap object, instead of using bare SIMPLE-VECTOR, in
158 ;;; order to avoid problems (in the debugger if nowhere else) with
159 ;;; SIMPLE-VECTORs some of whose elements are unbound tokens.
160 (defconstant +slot-unbound+ '..slot-unbound..)
162 (defmacro %allocate-static-slot-storage--class (no-of-slots)
163 `(make-array ,no-of-slots :initial-element +slot-unbound+))
165 (defmacro std-instance-class (instance)
166 `(wrapper-class* (std-instance-wrapper ,instance)))
168 ;;; When given a function should give this function the name
169 ;;; NEW-NAME. Note that NEW-NAME is sometimes a list. Some lisps
170 ;;; get the upset in the tummy when they start thinking about
171 ;;; functions which have lists as names. To deal with that there is
172 ;;; SET-FUN-NAME-INTERN which takes a list spec for a function
173 ;;; name and turns it into a symbol if need be.
175 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
176 ;;; that FIN to give it the name. When given any other kind of
177 ;;; function SET-FUN-NAME is allowed to return a new function which is
178 ;;; "the same" except that it has the name.
180 ;;; In all cases, SET-FUN-NAME must return the new (or same)
181 ;;; function. (Unlike other functions to set stuff, it does not return
182 ;;; the new value.)
183 (defun set-fun-name (fcn new-name)
184 #+sb-doc
185 "Set the name of a compiled function object. Return the function."
186 (declare (special *boot-state* *the-class-standard-generic-function*))
187 (cond ((symbolp fcn)
188 (set-fun-name (symbol-function fcn) new-name))
189 ((funcallable-instance-p fcn)
190 (if (if (eq *boot-state* 'complete)
191 (typep fcn 'generic-function)
192 (eq (class-of fcn) *the-class-standard-generic-function*))
193 (setf (%funcallable-instance-info fcn 1) new-name)
194 (bug "unanticipated function type"))
195 fcn)
197 ;; pw-- This seems wrong and causes trouble. Tests show
198 ;; that loading CL-HTTP resulted in ~5400 closures being
199 ;; passed through this code of which ~4000 of them pointed
200 ;; to but 16 closure-functions, including 1015 each of
201 ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
202 ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
203 ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
204 ;; Since the actual functions have been moved by PURIFY
205 ;; to memory not seen by GC, changing a pointer there
206 ;; not only clobbers the last change but leaves a dangling
207 ;; pointer invalid after the next GC. Comments in low.lisp
208 ;; indicate this code need do nothing. Setting the
209 ;; function-name to NIL loses some info, and not changing
210 ;; it loses some info of potential hacking value. So,
211 ;; lets not do this...
212 #+nil
213 (let ((header (%closure-fun fcn)))
214 (setf (%simple-fun-name header) new-name))
216 ;; XXX Maybe add better scheme here someday.
217 fcn)))
219 (defun intern-fun-name (name)
220 (cond ((symbolp name) name)
221 ((listp name)
222 (intern (let ((*package* *pcl-package*)
223 (*print-case* :upcase)
224 (*print-pretty* nil)
225 (*print-gensym* t))
226 (format nil "~S" name))
227 *pcl-package*))))
229 ;;; FIXME: probably no longer needed after init
230 (defmacro precompile-random-code-segments (&optional system)
231 `(progn
232 (eval-when (:compile-toplevel)
233 (update-dispatch-dfuns))
234 (precompile-function-generators ,system)
235 (precompile-dfun-constructors ,system)
236 (precompile-ctors)))
238 ;;; This definition is for interpreted code.
239 (defun pcl-instance-p (x)
240 (typep (layout-of x) 'wrapper))
242 ;;; CMU CL comment:
243 ;;; We define this as STANDARD-INSTANCE, since we're going to
244 ;;; clobber the layout with some standard-instance layout as soon as
245 ;;; we make it, and we want the accessor to still be type-correct.
247 (defstruct (standard-instance
248 (:predicate nil)
249 (:constructor %%allocate-instance--class ())
250 (:copier nil)
251 (:alternate-metaclass instance
252 cl:standard-class
253 make-standard-class))
254 (slots nil))
256 (!defstruct-with-alternate-metaclass standard-instance
257 :slot-names (slots hash-code)
258 :boa-constructor %make-standard-instance
259 :superclass-name instance
260 :metaclass-name standard-classoid
261 :metaclass-constructor make-standard-classoid
262 :dd-type structure
263 :runtime-type-checks-p nil)
265 ;;; Both of these operations "work" on structures, which allows the above
266 ;;; weakening of STD-INSTANCE-P.
267 (defmacro std-instance-slots (x) `(%instance-ref ,x 1))
268 (defmacro std-instance-wrapper (x) `(%instance-layout ,x))
269 ;;; KLUDGE: This one doesn't "work" on structures. However, we
270 ;;; ensure, in SXHASH and friends, never to call it on structures.
271 (defmacro std-instance-hash (x) `(%instance-ref ,x 2))
273 ;;; FIXME: These functions are called every place we do a
274 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
275 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
276 ;;; than just indiscriminately expanding them inline everywhere.
277 (declaim (inline get-slots get-slots-or-nil))
278 (declaim (ftype (function (t) simple-vector) get-slots))
279 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
280 (defun get-slots (instance)
281 (if (std-instance-p instance)
282 (std-instance-slots instance)
283 (fsc-instance-slots instance)))
284 (defun get-slots-or-nil (instance)
285 ;; Suppress a code-deletion note. FIXME: doing the FIXME above,
286 ;; integrating PCL more with the compiler, would remove the need for
287 ;; this icky stuff.
288 (declare (optimize (inhibit-warnings 3)))
289 (when (pcl-instance-p instance)
290 (get-slots instance)))
292 (defmacro built-in-or-structure-wrapper (x) `(layout-of ,x))
294 (defmacro get-wrapper (inst)
295 (once-only ((wrapper `(wrapper-of ,inst)))
296 `(progn
297 (aver (typep ,wrapper 'wrapper))
298 ,wrapper)))
300 ;;; FIXME: could be an inline function or ordinary function (like many
301 ;;; other things around here)
302 (defmacro get-instance-wrapper-or-nil (inst)
303 (once-only ((wrapper `(wrapper-of ,inst)))
304 `(if (typep ,wrapper 'wrapper)
305 ,wrapper
306 nil)))
308 ;;;; support for useful hashing of PCL instances
309 (let ((hash-code 0))
310 (declare (fixnum hash-code))
311 (defun get-instance-hash-code ()
312 (if (< hash-code most-positive-fixnum)
313 (incf hash-code)
314 (setq hash-code 0))))
316 (defun sb-impl::sxhash-instance (x)
317 (cond
318 ((std-instance-p x) (std-instance-hash x))
319 ((fsc-instance-p x) (fsc-instance-hash x))
320 (t (bug "SXHASH-INSTANCE called on some weird thing: ~S" x))))
322 ;;;; structure-instance stuff
323 ;;;;
324 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
325 ;;;; abstraction around our native structure representation doesn't
326 ;;;; seem to add anything useful, and could probably go away.
328 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
330 (defun get-structure-dd (type)
331 (layout-info (classoid-layout (find-classoid type))))
333 (defun structure-type-included-type-name (type)
334 (let ((include (dd-include (get-structure-dd type))))
335 (if (consp include)
336 (car include)
337 include)))
339 (defun structure-type-slot-description-list (type)
340 (nthcdr (length (let ((include (structure-type-included-type-name type)))
341 (and include
342 (dd-slots (get-structure-dd include)))))
343 (dd-slots (get-structure-dd type))))
345 (defun structure-slotd-name (slotd)
346 (dsd-name slotd))
348 (defun structure-slotd-accessor-symbol (slotd)
349 (dsd-accessor-name slotd))
351 (defun structure-slotd-reader-function (slotd)
352 (fdefinition (dsd-accessor-name slotd)))
354 (defun structure-slotd-writer-function (slotd)
355 (unless (dsd-read-only slotd)
356 (fdefinition `(setf ,(dsd-accessor-name slotd)))))
358 (defun structure-slotd-type (slotd)
359 (dsd-type slotd))
361 (defun structure-slotd-init-form (slotd)
362 (dsd-default slotd))