0.7.1.3:
[sbcl.git] / src / pcl / low.lisp
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)
14
15 ;;;; This software is part of the SBCL system. See the README file for more
16 ;;;; information.
17
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.
23
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.
37
38 (in-package "SB-PCL")
39 \f
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed*
42   '(optimize (speed 3) (safety 0)))
43 ) ; EVAL-WHEN
44
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46   `(dotimes (,var (the fixnum ,count) ,result)
47      (declare (fixnum ,var))
48      ,@body))
49 \f
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.
55
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 sb-kernel: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))
76 \f
77 ;;;; PCL's view of funcallable instances
78
79 (sb-kernel:!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)
84   :boa-constructor %make-pcl-funcallable-instance
85   :superclass-name sb-kernel:funcallable-instance
86   :metaclass-name sb-kernel:random-pcl-class
87   :metaclass-constructor sb-kernel:make-random-pcl-class
88   :dd-type sb-kernel: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.
93   ;;
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)
97
98 (import 'sb-kernel:funcallable-instance-p)
99
100 ;;; This "works" on non-PCL FINs, which allows us to weaken
101 ;;; FUNCALLABLE-INSTANCE-P to return true for all FINs. This is also
102 ;;; necessary for bootstrapping to work, since the layouts for early
103 ;;; GFs are not initially initialized.
104 (defmacro funcallable-instance-data-1 (fin slot)
105   (ecase (eval slot)
106     (wrapper `(sb-kernel:%funcallable-instance-layout ,fin))
107     (slots `(sb-kernel:%funcallable-instance-info ,fin 0))))
108
109 ;;; FIXME: Now that we no longer try to make our CLOS implementation
110 ;;; portable to other implementations of Common Lisp, all the
111 ;;; funcallable instance wrapper logic here can go away in favor
112 ;;; of direct calls to native SBCL funcallable instance operations.
113 (defun set-funcallable-instance-fun (fin new-value)
114   (declare (type function new-value))
115   (aver (funcallable-instance-p fin))
116   (setf (sb-kernel:funcallable-instance-fun fin) new-value))
117 (defmacro fsc-instance-p (fin)
118   `(funcallable-instance-p ,fin))
119 (defmacro fsc-instance-class (fin)
120   `(wrapper-class (funcallable-instance-data-1 ,fin 'wrapper)))
121 (defmacro fsc-instance-wrapper (fin)
122   `(funcallable-instance-data-1 ,fin 'wrapper))
123 (defmacro fsc-instance-slots (fin)
124   `(funcallable-instance-data-1 ,fin 'slots))
125 \f
126 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
127 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
128 (defun clos-slots-ref (slots index)
129   (svref slots index))
130 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
131 (defun (setf clos-slots-ref) (new-value slots index)
132   (setf (svref slots index) new-value))
133
134 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
135 ;;; is only used to discriminate between functions (including FINs)
136 ;;; and normal instances, so we can return true on structures also. A
137 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
138 ;;; PCL-INSTANCE-P.
139 (defmacro std-instance-p (x)
140   `(sb-kernel:%instancep ,x))
141
142 ;; a temporary definition used for debugging the bootstrap
143 #+sb-show
144 (defun print-std-instance (instance stream depth)
145   (declare (ignore depth))      
146   (print-unreadable-object (instance stream :type t :identity t)
147     (let ((class (class-of instance)))
148       (when (or (eq class (find-class 'standard-class nil))
149                 (eq class (find-class 'funcallable-standard-class nil))
150                 (eq class (find-class 'built-in-class nil)))
151         (princ (early-class-name instance) stream)))))
152
153 ;;; This is the value that we stick into a slot to tell us that it is
154 ;;; unbound. It may seem gross, but for performance reasons, we make
155 ;;; this an interned symbol. That means that the fast check to see
156 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
157 ;;; That is considerably faster than looking at the value of a special
158 ;;; variable. Be careful, there are places in the code which actually
159 ;;; use ..SLOT-UNBOUND.. rather than this variable. So much for
160 ;;; modularity..
161 ;;;
162 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use
163 ;;; the SBCL built-in unbound value token instead. Perhaps if we did
164 ;;; so it would be a good idea to define collections of CLOS slots as
165 ;;; a new type of heap object, instead of using bare SIMPLE-VECTOR, in
166 ;;; order to avoid problems (in the debugger if nowhere else) with
167 ;;; SIMPLE-VECTORs some of whose elements are unbound tokens.
168 (defconstant +slot-unbound+ '..slot-unbound..)
169
170 (defmacro %allocate-static-slot-storage--class (no-of-slots)
171   `(make-array ,no-of-slots :initial-element +slot-unbound+))
172
173 (defmacro std-instance-class (instance)
174   `(wrapper-class* (std-instance-wrapper ,instance)))
175 \f
176 ;;; When given a function should give this function the name
177 ;;; NEW-NAME. Note that NEW-NAME is sometimes a list. Some lisps
178 ;;; get the upset in the tummy when they start thinking about
179 ;;; functions which have lists as names. To deal with that there is
180 ;;; SET-FUN-NAME-INTERN which takes a list spec for a function
181 ;;; name and turns it into a symbol if need be.
182 ;;;
183 ;;; When given a funcallable instance, SET-FUN-NAME *must*
184 ;;; side-effect that FIN to give it the name. When given any other
185 ;;; kind of function SET-FUN-NAME is allowed to return a new
186 ;;; function which is "the same" except that it has the name.
187 ;;;
188 ;;; In all cases, SET-FUN-NAME must return the new (or same)
189 ;;; function. (Unlike other functions to set stuff, it does not return
190 ;;; the new value.)
191 (defun set-fun-name (fcn new-name)
192   #+sb-doc
193   "Set the name of a compiled function object. Return the function."
194   (declare (special *boot-state* *the-class-standard-generic-function*))
195   (cond ((symbolp fcn)
196          (set-fun-name (symbol-function fcn) new-name))
197         ((funcallable-instance-p fcn)
198          (if (if (eq *boot-state* 'complete)
199                  (typep fcn 'generic-function)
200                  (eq (class-of fcn) *the-class-standard-generic-function*))
201              (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
202              (bug "unanticipated function type"))
203          fcn)
204         (t
205          ;; pw-- This seems wrong and causes trouble. Tests show
206          ;; that loading CL-HTTP resulted in ~5400 closures being
207          ;; passed through this code of which ~4000 of them pointed
208          ;; to but 16 closure-functions, including 1015 each of
209          ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
210          ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
211          ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
212          ;; Since the actual functions have been moved by PURIFY
213          ;; to memory not seen by GC, changing a pointer there
214          ;; not only clobbers the last change but leaves a dangling
215          ;; pointer invalid  after the next GC. Comments in low.lisp
216          ;; indicate this code need do nothing. Setting the
217          ;; function-name to NIL loses some info, and not changing
218          ;; it loses some info of potential hacking value. So,
219          ;; lets not do this...
220          #+nil
221          (let ((header (sb-kernel:%closure-fun fcn)))
222            (setf (sb-kernel:%simple-fun-name header) new-name))
223
224          ;; XXX Maybe add better scheme here someday.
225          fcn)))
226
227 (defun intern-fun-name (name)
228   (cond ((symbolp name) name)
229         ((listp name)
230          (intern (let ((*package* *pcl-package*)
231                        (*print-case* :upcase)
232                        (*print-pretty* nil)
233                        (*print-gensym* t))
234                    (format nil "~S" name))
235                  *pcl-package*))))
236 \f
237 ;;; FIXME: probably no longer needed after init
238 (defmacro precompile-random-code-segments (&optional system)
239   `(progn
240      (eval-when (:compile-toplevel)
241        (update-dispatch-dfuns)
242        (compile-iis-functions nil))
243      (precompile-function-generators ,system)
244      (precompile-dfun-constructors ,system)
245      (precompile-iis-functions ,system)
246      (eval-when (:load-toplevel)
247        (compile-iis-functions t))))
248 \f
249 ;;; This definition is for interpreted code.
250 (defun pcl-instance-p (x)
251   (typep (sb-kernel:layout-of x) 'wrapper))
252
253 ;;; CMU CL comment:
254 ;;;   We define this as STANDARD-INSTANCE, since we're going to
255 ;;;   clobber the layout with some standard-instance layout as soon as
256 ;;;   we make it, and we want the accessor to still be type-correct.
257 #|
258 (defstruct (standard-instance
259             (:predicate nil)
260             (:constructor %%allocate-instance--class ())
261             (:copier nil)
262             (:alternate-metaclass sb-kernel:instance
263                                   cl:standard-class
264                                   sb-kernel:make-standard-class))
265   (slots nil))
266 |#
267 (sb-kernel:!defstruct-with-alternate-metaclass standard-instance
268   :slot-names (slots)
269   :boa-constructor %make-standard-instance
270   :superclass-name sb-kernel:instance
271   :metaclass-name cl:standard-class
272   :metaclass-constructor sb-kernel:make-standard-class
273   :dd-type structure
274   :runtime-type-checks-p nil)
275
276 ;;; Both of these operations "work" on structures, which allows the above
277 ;;; weakening of STD-INSTANCE-P.
278 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
279 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
280
281 ;;; FIXME: These functions are called every place we do a
282 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
283 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
284 ;;; than just indiscriminately expanding them inline everywhere.
285 (declaim (inline get-slots get-slots-or-nil))
286 (declaim (ftype (function (t) simple-vector) get-slots))
287 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
288 (defun get-slots (instance)
289   (if (std-instance-p instance)
290       (std-instance-slots instance)
291       (fsc-instance-slots instance)))
292 (defun get-slots-or-nil (instance)
293   (when (pcl-instance-p instance)
294     (get-slots instance)))
295
296 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
297
298 (defmacro get-wrapper (inst)
299   (once-only ((wrapper `(wrapper-of ,inst)))
300     `(progn
301        (aver (typep ,wrapper 'wrapper))
302        ,wrapper)))
303
304 ;;; FIXME: could be an inline function or ordinary function (like many
305 ;;; other things around here)
306 (defmacro get-instance-wrapper-or-nil (inst)
307   (once-only ((wrapper `(wrapper-of ,inst)))
308     `(if (typep ,wrapper 'wrapper)
309          ,wrapper
310          nil)))
311 \f
312 ;;;; structure-instance stuff
313 ;;;;
314 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
315 ;;;; abstraction around our native structure representation doesn't
316 ;;;; seem to add anything useful, and could probably go away.
317
318 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
319
320 (defun get-structure-dd (type)
321   (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
322
323 (defun structure-type-included-type-name (type)
324   (let ((include (sb-kernel::dd-include (get-structure-dd type))))
325     (if (consp include)
326         (car include)
327         include)))
328
329 (defun structure-type-slot-description-list (type)
330   (nthcdr (length (let ((include (structure-type-included-type-name type)))
331                     (and include
332                          (sb-kernel:dd-slots (get-structure-dd include)))))
333           (sb-kernel:dd-slots (get-structure-dd type))))
334
335 (defun structure-slotd-name (slotd)
336   (sb-kernel:dsd-name slotd))
337
338 (defun structure-slotd-accessor-symbol (slotd)
339   (sb-kernel:dsd-accessor-name slotd))
340
341 (defun structure-slotd-reader-function (slotd)
342   (fdefinition (sb-kernel:dsd-accessor-name slotd)))
343
344 (defun structure-slotd-writer-function (slotd)
345   (unless (sb-kernel:dsd-read-only slotd)
346     (fdefinition `(setf ,(sb-kernel:dsd-accessor-name slotd)))))
347
348 (defun structure-slotd-type (slotd)
349   (sb-kernel:dsd-type slotd))
350
351 (defun structure-slotd-init-form (slotd)
352   (sb-kernel::dsd-default slotd))