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